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.