Interfacing Arduino Uno to AT24C16A

Interfacing Arduino Uno to AT24C16A

Below is the interfacing snapshot for the AT24C16A with Arduino Uno board.


The below program is the register level programming code rather than suing the Wire I2C library.

--------------------

unsigned char data[10] = {0x00};

unsigned char count = 0;
unsigned char data1 = 0x00;
unsigned char data2 = 0x00;

void setup()
{
  Serial.begin(9600);

  pinMode(A4, OUTPUT);  //SDA
  digitalWrite(A4,HIGH);
  pinMode(A5, OUTPUT);  //SCL
  digitalWrite(A5,HIGH);

  delay(1000);
  delay(1000);

  //Writing to EEPROM

  TWBR = 0x01;  //division factor for bit rate generator
  TWCR = 0xA4; //start bit, TWINT set to 1 to clear interrupt flag, Enable I2C

  delay(10);

  while(TWSR != 0x08);  //START has been transmitted
 
  TWDR = 0xA0; //Address bit, address of 0x50 for the AT24C16A EEPROM

  TWCR = 0x84; //clear interrupt flag after start

  delay(10);

  while(TWSR != 0x18);  //SLA+W has been transmitted and ACK received

  TWCR = 0x84; //clear interrupt flag

  TWDR = 0x00; //Address to which data is to be written

  TWCR = 0x84; //clear interrupt flag

  delay(10);

  while(TWSR != 0x28);

  TWDR = 0x2F; //data to be written

  TWCR = 0x84; //clear interrupt flag

  delay(10);

  while(TWSR != 0x28);

  TWCR = 0x94; //STOP condition

  delay(10);

  Serial.print("Write completed");

  delay(5000);

  //Reading from EEPROM

  TWCR = 0xA4; //start bit, TWINT set to 1 to clear interrupt flag, Enable I2C

  delay(10);

  while(TWSR != 0x08);  //START has been transmitted
 
  Serial.print("START for READ completed");

  TWDR = 0xA1; //Address bit, address of 0x50 for the AT24C16A EEPROM

  TWCR = 0x84; //clear interrupt flag after start

  delay(10);

  while(TWSR != 0x40);

  Serial.print("Address of slave for READ completed");

  delay(10);

  data1 = TWDR;

  delay(10);

  TWCR = 0x84; //clear interrupt flag

  delay(10);

  data2 = TWDR;

  delay(10);

  TWCR = 0x94; //STOP condition

  delay(10);

  Serial.print("EEPROM READ and WRITE Completed");

}

void loop()
{
  Serial.print(data1);
  Serial.print('\n');
  Serial.print(data2);
  Serial.print('\n');
  delay(10000);
}

Post a Comment

0 Comments