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

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.

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.

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.

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 usingSTK2148-UltraLite Board then turned off SW7 switches to read Dataon 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

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

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

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

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?

Social

Advertisement

Amazon Associates Disclosure

BINARYUPDATES.COM is a participant in Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com or Amazon.in