Home
> AVR LEARNING > IIC EEPROM Interface
IIC EEPROM Interface
DescriptionI²C is an abbreviation of Inter Integrated Circuit and is a protocol for serial communication between Integrated Circuits, it is also called Two Wire Interface (TWI). The bus is used for communication between microcontrollers and peripheral devices like memories, temperature sensors and I/O expanders. An EEPROM is a Electrically Erasable and Programmable Read Only Memory.
The communication of the bus goes along two lines: SDA (Serial Data) and SCL (Serial Clock). Each I²C device has a unique 7-bit adress (Device Select Code). The most significant bits are fixed and assigned to Aa specific device catagory (e.g. b1010 is assigned to serial EEPROMS). The three less significant bits (A2,A1 and A0) are programmable and used to adress the device. The three bits allows eight different I2C adress combinations and therefore allowing up to eight different devices of that type to operate on the same I2C-bus. The 7-bit adressing allows up to 128 devices on the same bus. The I2C adress is send in the 1st byte, the lest significant bit of the first byte is used to indicate if the master is going to write(0) or read(1) from the slave.
The device that sends data along the bus is calledmaster, a device that receives the data is calledslave.The master starts the transmission with a start signal and stops the transmission with a stop signal on the SDA line. During the start and stop signals the SCL line has to be high. After the master has started the data-transmission with a startsignal, the master writes a device adress byte to the slave. Each databyte has to have a length of 8 bits. The slave has to acknowledge the reception of the databyte with a acknowledge-bit (ACK).
A write operation requires a device adress bytes, two adress bytes and the data-byte. Upon receive of the adress the EEPROM sends an ACK and then clocks in the data-byte. The EEPROM sends again an ACK and the microcontrollers sends a stop-signal to terminate the write sequence.
HardwareIn this example the AT24C32 EEPROM of Atmel is connected to a AT2313 microcontroller. There is also a LCD display hooked to the microcontroller to display the data. The 24C32 has 4096 bytes of memory. The SCL and SDA lines of the EEPROM are connected to PORTD.0 and PORTD.1 of the microcontroller. Below is the schematic how it is connected. Go also here to see how you can hook up a LCD module to the microcontroller.
SoftwareThe BASCOM-AVR compiler is used to make a program that writes and reads one byte from the EEPROM. AVR-BASCOM has several commands embedded to control the I2C bus. Below you can see the commands:
In BASCOM-AVR you first have to configure the ports you use for the SDA and SCL lines of the I2C bus. Then you send the device adress to select the EEPROM that is connected to the I2C bus. After that you send two bytes to the EEPROM to select the adress in the EEPROM to which you want to write the data. The last byte to send in a write sequence is the data byte.
Below you see the code for the example program. The program writes a byte from variable D_wr into the EEPROM at adress 0 and then reads the byte from the EEPROM at adress 0 and puts it into the variable D_rd, which is then displayed on the LCD module.
***************************************************************************** In the picture below you can see how a write sequence looks on a oscilloscope. The upper signal is the SCL line and the lower signal is the SDA line. You can easely recognise the device adress 1010 at the start of the sequence.
|
||||||||||||||
Categories: AVR LEARNING
Comments (0)
Trackbacks (0)
Leave a comment
Trackback