Description![]() ![]() ![]()
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.
Hardware
In 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.
Software
The 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.
***************************************************************************** ' * Title : EEPROM 24C32.bas ' * Last Updated : 05.03.2006 ' * Target device: At90s2313, 24C32 ' * Author : www.avrprojects.net ' * Program code : BASCOM-AVR ' * Hardware req. : ' * Description : ' * This application reads and write a byte to an 24c32 EEPROM connected to an ' * AT2313 microcontroller. ' **************************************************************************** Dim D_w As Byte , D_r As Byte Config Lcdpin = Pin , Db4 = Portb.3 , Db5 = Portb.2 , Db6 = Portb.1 , Db7 = Portb.0 , E = Portb.6 , Rs = Portb.7 Config Lcd = 16 * 2 Cls Cursor Off Config Scl = Portd.0 'assign the SCl line to PORTD.0 Config Sda = Portd.1 'assign the SDA line to PORTD.1 D_w = 100 '********** write byte to EEPROM *********************************************** I2cstart 'generate start I2cwbyte &B1010_0000 'send device address I2cwbyte 0 'H adress of EEPROM I2cwbyte 0 'L adress of EEPROM I2cwbyte D_w 'data to EEPROM I2cstop 'stop condition Waitms 10 '********** read byte from EEPROM ********************************************** I2cstart 'generate start I2cwbyte &B1010_0000 'send device adsress I2cwbyte 0 'H address of EEPROM I2cwbyte 0 'L address of EEPROM I2cstart 'repeated start I2cwbyte &B1010_0001 'slave address (read) I2crbyte D_r , Nack 'read byte from EEPROM I2cstop 'generate stop Lcd "D_w= " Lcd D_w 'show byte on LCD Lowerline Lcd "D_r= " Lcd D_r End
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.
| ||||||||||||||
30 Nov 2011
IIC EEPROM Interface
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Confused? Feel free to ask
Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.
Note:
Please do not spam Spam comments will be deleted immediately upon my review.