Electronic systems today incorporate a number of peripheral ICs that have to communicate with each other and the outside world. To maximise hardware efficiency and simplify circuit design, Philips developed a simple bidirectional two-wire communication interface between components residing on the same circuit board. This interface is referred to as two-wire interface (TWI) or I2C (inter-integrated circuit).
Presented here is a circuit for interfacing AT24C02 EEPROM with P89V51RD2 microcontroller using I2C protocol. AT24C02 EEPROM is a non-volatile memory often used for interfacing with microcontrollers. That is, data is not lost even when power fails. A microcontroller can read the last saved data from the EEPROM when power resumes.
Circuit and working
Fig. 1 shows the circuit of EEPROM interface for beginners. It comprises a regulator 7805 (IC1), microcontroller P89V51RD2 (IC2), EEPROM AT24C02 (IC3), an LCD module and a few discrete components. The 230V AC mains is stepped down by transformer X1 to deliver a secondary output of 9 V, 500 mA. The transformer output is rectifiedby a full-wave rectifie comprising diodes D1 through D4, filtered by capacitor C1 and regulate by IC1. Capacitor C2 bypasses the ripples present in the regulated supply. LED1 acts as the power indica-tor and resistor R1 limits the current through LED1.
Microcontroller. The P89V51RD2 is an eight-bit 80C51 microcontroller with 64kB Flash and 1024 bytes of data RAM. It has three 16-bit timers/counters, programmable watchdog timer, eight interrupt sources with four priority levels, serial peripheral interface (SPI) and an enhanced UART, programmable counter array with PWM and capture/compare functions, four eight-bit input/output ports. It also has an on-chip oscillator and clock circuitry, which is operated with up to 40MHz crystal. It supports 12 clocks per machine cycle (default) and six clocks per machine cycle modes, which can be selected via the software. The Flash program memory supports both parallel programming and serial programming like insystem programming (ISP).
Data pins D0 through D7 of the LCD are connected to port pins P2.0 through P2.7 of IC2, respectively. Control pins register select (RS), read/write (R/W) and enable (E) are connected to port pins P3.5, P3.6 and P3.7, respectively. Preset VR1 is connected to pin 3 of the LCD for contrast control. Power-on reset is provided by the combination of resistor R2 and capacitor C3. Switch S17 is used for manual reset. An 11.0592MHz crystal along with two 33pF capacitors provides the basic clock frequency to microcontroller IC2.
A 16-key numeric keypad for address/data entry is connected to port P1 of the microcontroller. Here only eight pins are used for scanning and sensing the sixteen keys. The keypad is arranged in a 4×4 matrix. There are four scan lines, which are set in output mode. Four sensing keys are used as input lines to the microcontroller.
Memory. IC AT24C02 is an I2C-bus compatible 2kb EEPROM organised as 256×8-bit that can retain data for more than ten years. To obviate loss of the latest setting in case of power failure, the microcontroller can store the data in the EEPROM. The memory ensures that the microcontroller will read the last saved data from the EEPROM when power resumes. Using SCL and SDA lines of the EEPROM, the microcontroller can read and write the data to/from AT24C02 memory.
Some of the conditions to communicate through the I2C bus are:
Clock and data transition. The SDA line is normally pulled high with an external device. Data on the SDA line may change only during SCL’s low time period. Data changes during SCL line high time period will indicate a start or stop condition.
Start and Stop. A start condition is a high-to-low transition of SDA while SCL line is high. The stop condition is a low-to-high transition of SDA line while SCL line is high.
Acknowledge. All addresses and data words are serially transmitted to and from the EEPROM in eight-bit words. The EEPROM sends a zero to acknowledge that it has received each word. This happens during the ninth clock cycle.
To write data from the microcon-troller to the EEPROM, the following interface protocol must be followed:
1. Send start condition
2. Send chip address byte (say A0H) with read/write ‘0’ bit (‘0’ for write or ‘1’ for read) followed by an acknowledge bit
3. Send internal memory address byte (where you want to write) followed by an acknowledge bit
4. Send data byte followed by an acknowledge bit
5. Optionally, send any further data bytes
6. Send stop condition
To read data from the EEROM to the microcontroller, the following in-terface protocol must be followed:
1. Send start condition
2. Send chip address byte (say, A0H) along with read/write ‘0’ bit (‘0’ for write or ‘1’ for read) followed by an acknowledge bit
3. Send internal memory address byte (where you want to write) fol-lowed by an acknowledge bit
4. Send a start condition again (start repeated)
5. Send chip address byte (say, A1H) along with read/write ‘1’ bit (‘0’ for write or ‘1’ for read) followed by an acknowledge bit
6. Read data from the EEPROM fol-lowed by an acknowledge bit
7. Send stop condition
Working of the circuit is simple. You can save data at any location on the EEPROM using the user interface and read it again later. The data is never lost even if the power fails.
Construction and testing
An actual-size, singleside PCB for the circuit is shown in Fig. 2 and its component layout in Fig. 3. Assemble the circuit on a PCB as it minimises time and assembly errors. Carefully assemble the components and double-check for any overlooked error. Use IC base for the microcontroller and the EEPROM. Before inserting the IC, check the supply voltage. Using preset VR1 set the LCD contrast for proper display.
Download PCB anc component layout PDFs: click here
Test points are given in the table for checking. When the circuit is switched on, the ‘EFY EEPROM test’ message is displayed on the firs line of the LCD and ‘e read f save’ is displayed on the second line of the LCD. Press ‘F’ key if you want to save data in the EEPROM. Message ‘save your data’ is displayed on the LCD and then it prompts you to enter the address location where you want to save the data. You can enter the address, say, ‘22’ (or any other address from 00 to FE) of the memory location. Then ‘enter the data’ message appears. You can enter any data, say, 34, and press ‘E’ to save the data or ‘F’ not to save the data.
Now switch off the power and turn it back ‘on.’ Press ‘E’ key to read the data. Message ‘read your data’ appears on the LCD. Thereafter, the LCD display shows ‘enter the address’ message for entering the address (say, ‘22’) of the memory location. The LCD display now will show ‘stored data is: 34,’ where ‘34’ is the data you have entered in the above step.
The source program for the microcontroller is written in ‘C’ language and compiled using the Keil µVision4 compiler. The generated hex code is burnt into the microcontroller by using a suitable programmer. The program is well commented and easy to understand. When power is switched on or reset switch S17 is pressed momentarily, the microcontroller executes the main program.
First, it initailises the LCD and executes ‘while’ loop. ‘EFY EEPROM test’ and ‘e read f save’ messages are displayed on the firstand second lines of the LCD module, respectively. If all the columns of the switches are high, key_scan( ) function checks for pressing of ‘E’ and ‘F’ keys. If ‘E’ key is pressed, read_module( ) function executes. If ‘F’ key is pressed, save_module( ) function executes. Function read_module( ) displays corresponding messages and finallyreads the data from EEPROM using i2c_read( ) function. Reading of data from the EEPROM follows I2C protocol as stated above.
Similarly, save_module( ) function displays corresponding messages and finallysaves the data in the EEPROM using i2c_write( ) function. Writing data to the EEPROM follows I2C protocol as stated above.
The i2c.h, lcd.h and keyboard.h header filesare used for I2C protocol communication, LCD mode and 4×4 matrix keypad in the source program.
Download source code: click here
The author is a B.Tech in electronics and com-munication from Lovely Professional University