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.
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).
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.
|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
SOURCE CODE: I2C in LPC2148 ARM7 Microcontroller
To download entire project files Click Here[crayon-5c6ad03dc9a3f912434749/]
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:
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!!!!