I2C in LPC2148 ARM7 Microcontroller

In this post, we will learn about how to program I2C in LPC2148 ARM7 Microcontroller. Also, we will interface serial EEPROM 24LC512 to LPC2148 Microchip. We will test EEPROM by writing to and reading from it. I2C communication protocol is most famous protocol in embedded systems.

What is I2C and how it works?

I2C bus has become one of the most important microcontroller subsystem used for interfacing various IC devices with microcontroller. It is unique for its ability to maximize hardware efficiency & circuit simplicity. The I2C bus uses only 2-bidirectional data lines for communicating with the microcontroller. This bus is called Inter IC or I2C bus. All I2C-bus compatible devices incorporate an on-chip interface which allows them to communicate directly with each other via I2C-bus. I2C protocol specification can support up to 128 devices attached to the same bus. Today many I2C IC devices available in the market such as Serial EEPROM, I/O Expander, RTC, ADC, DAC, Sensors etc.

Typical I2C Master Slave Connection

The I2C protocol uses master and slave method, the master which is in most cases a microcontroller while the slave can be any I2C device such as Serial EEPROM, RTC etc. The I2C protocol uses only two signals: clock and data. The Clock referred as SCL (Serial Clock) while data is referred as SDA (Serial Data).

Free Course Signup

I2C in LPC2148 ARM7 Microcontroller
I2C Data Bit Transfer
In I2C protocol we have to use special combinations of signal conditions. Fundamentally, there are just two conditions START and STOP. A START condition is generated by master followed by 7-bit of address, then a Read/Write (R/W) bit. If a slave device detects an address match then it sends ACK by driving SDA low during the next clock cycle; if no slave recognizes the address then the SDA line will left alone and pulled up high. Once ACK received, data will be either sent to the slave device or read from the slave device (depending on the status of R/W bit).

Therefore, each byte is 9-bits in which 7-bits in which 7-bits for address and one R/W plus one ACK/NAK, or 8-bit data plus one ACK/NAK. The last data byte of a transaction should generally be followed by NAK, to indicate that it is intended to be the final byte. After this, either a STOP or a ReSTART should be issued by the master. Bus errors are rarely introduced when using a dedicated I2C peripheral on the master.

Features of I2C in LPC2148 ARM7 Microcontroller

  • LPC2148 supports two fast I2C-buses (I2C0 & I2C1).
  • I2C bus interface that may be configured as Master, Slave or Master/Slave.
  • Supports programmable clock to allow adjustment of multiple data speed: standard (100 kbps), fast (400 kbps) and high speed (3.4 Mbps).
  • Supports bi-directional data transfer between master and slave.
  • I2C protocol is useful where many devices connected on the bus. This helps to reduce the cost and complexity of circuit as more devices allowed to communicate through same bus.
  • The I2C bus may be used for test and diagnostic purposes.
  • Application: Interfaces to external I2C standard parts such as serial EEPROM, Ram, and LCDs etc.

Registers of I2C in LPC2148 ARM7 Microcontroller

Before we proceed any further and write C Program to interface EEPROM using I2C in LPC2148 ARM7 Microcontroller. Let’s have a look at registers which we need to configure in our code. The I2C peripheral interface is composed of seven registers. The functional details of those registers are provided down below:

Register Name Description & Function
I2C0CONSET I2C0 Control Set Register: This register control the setting of bits in the I2CON register that controls operation of the I2C interface. Writing a one to a bit of this register causes the corresponding bit in the I2C Control Register to be set. Writing a zero has no effect. I2C0CONSET contains the following control bits:

I2EN (Enable): is set to enable the I2C interface.
STA (Start): is set to enter master mode and send a START condition.
STO (Stop): sends STOP condition in master mode, and recover from an error in slave mode.
AA (Assert ACK): is set to request an acknowledge be returned from the slave device.
SI (Interrupt): is set to indicate a state change in the I2C Controller.

I2C0CONCLR I2C0 Control Clear Register: This register control clearing of bits in the I2CON register the controls operation of the I2C interface. Writing a one to a bit of this register causes the corresponding bit in the I2C control register to be cleared. Writing zero has no effect. I2C0CONCLR contains following control bits:
I2ENC: disables the I2C Controller.
STAC: clears START flag.
AAC: Clears the Assert ACK flag.
SIC: Clears the I2C interrupt flag.
I2C0STAT I2C0 Status Register: During I2C operation this register provides detailed status codes that allow software to determine the next action needed.
I2C0DAT I2C0 Data Register: During master or slave transmit mode, data to be transmitted is written to this register. During master or slave receive mode, data that has been received may be read from this register.
I2C0ADR I2C0 Slave Address Register: This register is readable & writable, and is only used when I2C interface is set to slave mode. In master mode, this register has no effect. I2C0ADR contains the 7-bit slave address for operation of the I2C interface in slave mode. The least significant bit (LSB) determine whether a slave respond to the general call address.
I2C0SCLH I2C0 SCH Duty Cycle Register High Half Word: This register determines the high time of the I2C Clock (contains the SCL high duty cycle count)
I2C0SCLL I2C0 SCL Duty Cycle Register High Half Word: This register determines the low time of the I2C Clock. (Contains the SCL low duty cycle count). I2C0SCLL and I2C0SCLH together determine the clock frequency generated by an I2C master and certain times used in slave mode.

For more details on i2C register and bit description, we recommend you to keep datasheet open UM10139 [Chapter: 14.7, Page No: 215]

Example Project:  Let’s interface serial EEPROM 24LC512 using I2C in LPC2148 ARM7 Microcontroller. The interfacing I2C-EEPROM with LPC2148 is very simple and straight forward. Here we will perform read, write and erase operations on EEPROM by using I2C and the value will be displayed on serial port using UART0. A delay is occurring in every single data read from EEPROM. The delay depends on compiler how it optimizes the loop as soon as you make changes in options the delay changes.

Circuit Setup: I2C in LPC2148 ARM7 Microcontroller

I2C in LPC2148 ARM7 Microcontroller
I2C in LPC2148 ARM7 Microcontroller
24LC512 EEPROM Connection to LPC2148
24LC512 EEPROM Connection to LPC2148

SOURCE CODE: I2C in LPC2148 ARM7 Microcontroller

To download entire project files Click Here

 Project       : I2CLPC2148
 File	       : main.c
 Date          : May 2016
 Target        : LPC2148
 Description   : Functions related to the I2C EEPROM. The program reads and write the data 
                 in EEPROM through I2C			  
#include <LPC214X.H>
#include <Stdio.h>
#include "Type.h"
#include "uart.h"
#include "I2C.h"
#include "TIMER.h"

Main function.

int main(void)
  uint8 write_buffer[20] = {'B', 'I', 'N', 'A', 'R', 'Y',0};
  uint8 read_buffer[20];
  uint32 delay;
  PINSEL0 |= 0x00000005 ;
  UART0_Init(); // Initialize UART0
  I2C_Init();		// Initialize I2C0
  TIMER_Init();	// Initialize Timer
  UART0_Write_Text("**** LPC2148 ARM7 I2C EEPROM Demo ****\n\n\r");
  UART0_Write_Text("Initialization done. \n\r\n");
    if (!I2C_WriteToEEPROM(0,  write_buffer, 20)) // write into EEPROM
      UART0_Write_Text("\nMemory write error.");
    if (!I2C_ReadFromEEPROM(0, read_buffer, 20))  // read from EEPROM
      UART0_Write_Text("\nMemory Read error..");
    UART0_Write_Text("\n\r The Read Data are: \t");
    UART0_Write_Text((char *)read_buffer);	  // display data on serial port				
  //return 0 ;

Download Project: Click Here

The project is fully tested and functioning. We only have to compile and download .hex file onto LPC2148 Microcontroller. To see output on computer we will have to configure PuTTY to establish serial communication to read data from EEPROM 24LC512.

IMPORTANT: After loading .hex file, Make sure that you’re not into ISP Mode. We will be using UART0 for programming as well as for reading data from 24LC512 EEPROM. In case if you’re using STK2148-UltraLite Board then turned off SW7 switches to read Data on PuTTY/HyperTerminal.

Once we have done with all settings, just open console. And we will start receiving string from EEPROM on PuTTY. Here is an output from our project:

output from i2c in lpc2148 program
Output from I2C in LPC2148 Program

The principal we learn on this I2C serial EEPROM device can be applied to other I2C devices as well, the differences is only on the terms used; on the serial EEPROM we use memory address for storing and retrieving the data, while on the other I2C devices such as Microchip MCP23008 8-bit I/O expander or Dallas DS1307 Real Time Clock we use register address for writing and reading the data.

That’s all you really need to know in order to use external I²C EEPROM chip. I hope this post will help you get program I2C in LPC2148 ARM7 Microcontroller to read, write and erase data of EEPROM. In our future posts, we will explore SPI Protocol of LPC2148 ARM7 Microcontroller. If you have any question then please feel free to leave a comment. Thanks!!!!

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM


Register | Lost your password?