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
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).

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 NameDescription & Function
I2C0CONSETI2C0 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.

I2C0CONCLRI2C0 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.
I2C0STATI2C0 Status Register: During I2C operation this register provides detailed status codes that allow software to determine the next action needed.
I2C0DATI2C0 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.
I2C0ADRI2C0 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.
I2C0SCLHI2C0 SCH Duty Cycle Register High Half Word: This register determines the high time of the I2C Clock (contains the SCL high duty cycle count)
I2C0SCLLI2C0 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

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!!!!

Check out ARM7 LPC2148 Starter Kit for your Next Projects

Check out ARM7 LPC2148 Starter Kit for your Next Projects

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

10 comments

  1. what is the purpose of the program if you can’t explain it

    • Thank You ! for your comment. I hope you understand time it takes to test and document complete project. Writing blogs takes significant amount of time and I can’t afford to do more…..still I will try my best to post explanation soon….

      Good day and Enjoy…!!!!

  2. sarath chandu gaddam

    where is I2C.h file. please send the i2c.h file

  3. can u please explain the i2c.h file

  4. thank u. i downloaded it.

  5. i m not able interface ds1307 rtc with LPC2148. can u help?

  6. Hello
    thank you for the information could you please explain how we can connect a temperature sensor,wavelength sensor and weight sensor to lpc2148 board.
    EEPROM can be helpful to store information temporarily so that it can be used for printing application as well?

  7. please sir, explain me. i2c programming 2 or more peripharal device in lpc2148.

  8. B KRISHNA MURTHY

    Hai Umesh I am learning now Embedded c but I don’t know where to start

    can you please send me which topics I need to cover

Leave a Reply

Your email address will not be published. Required fields are marked *

Login

Register | Lost your password?