Home > AVR LEARNING > IIC EEPROM Interface

IIC EEPROM Interface

Description

I²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.

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:
BASCOM I2C commands
Command Description
Config SDA Configure the SDA line e.g. PORTB.0
Congif SCL Configure the SCL line e.g. PORTB.1
I2cstart generates a start condition
I2cstop generates a stop condition
I2cwbyte writes a byte to the target device
I2crbyte reads a byte from the target device
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.
Downloads
File Description File size
Download this file (I2C EEPROM Interface.bas)I2C EEPROM~.bas AVR BASCOM source file 2 Kb
Download this file (I2C EEPROM Interface.HEX)I2C EEPROM~.HEX hex file 2 Kb
Categories: AVR LEARNING
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment