AT24C32 is an i2c compatible serial EEPROM which can be programmed using a microcontroller.
The AT24C32 provides 32,768 bits of serial electrically erasable and programmable
read-only memory (EEPROM). The device’s cascadable feature allows up to 8 devices to share a common 2-
wire bus. The device is optimized for use in many industrial and commercial applications
where low power and low voltage operation are essential. The AT24C32/64 is
available in space-saving 8-pin JEDEC PDIP, 8-pin JEDEC SOIC, 8-pin EIAJ SOIC,
and 8-pin TSSOP (AT24C64) packages and is accessed via a 2-wire serial interface.
In addition, the entire family is available in 2.7V (2.7V to 5.5V) and 1.8V (1.8V to 5.5V)
versions.
/*
* main.c
*
* Created: 8/24/2022 10:53:05 PM
* Author: abhay
*/
#define F_CPU 16000000
#include <xc.h>
#include "util/delay.h"
#include "uart.h"
#include <stdio.h>
#define FALSE 0
#define TRUE 1
void EEOpen();
uint8_t EEWriteByte(uint16_t,uint8_t);
uint8_t EEReadByte(uint16_t address);
int main(void)
{
UART_Init();
EEOpen();
char buff[20];
sprintf(buff,"Hello EEPROM TEST \nBy: \t ABHAY");
UART_SendString(buff);
//Fill whole eeprom 32KB (32768 bytes)
//with number 7
uint16_t address;
char failed;
failed = 0 ;
for(address=0;address< (32768);address++)
{
sprintf(buff,"address = %d \n",address);
UART_SendString(buff);
if(EEWriteByte(address,5)==0)
{
//Write Failed
sprintf(buff,"write Failed %x \n",address);
UART_SendString(buff);
failed = 1;
break;
}
}
if(!failed)
{
//We have Done it !!!
sprintf(buff,"Write Success !\n");
UART_SendString(buff);
}
while(1)
{
//TODO:: Please write your application code
//Check if every location in EEPROM has
//number 7 stored
failed=0;
for(address=0;address < 32768 ; address++)
{
if(EEReadByte(address)!=5)
{
//Failed !
sprintf(buff,"Verify Failed %x \n",address);
UART_SendString(buff);
failed=1;
break;
}
}
if(!failed)
{
//We have Done it !!!
sprintf(buff,"Write Success !\n");
UART_SendString(buff);
}
}
}
void EEOpen()
{
//Set up TWI Module
TWBR0 = 5;
TWSR0 &= (~((1<<TWPS1)|(1<<TWPS0)));
}
uint8_t EEWriteByte(uint16_t address,uint8_t data)
{
do
{
//Put Start Condition on TWI Bus
TWCR0=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x08)
return FALSE;
//Now write SLA+W
//EEPROM @ 00h
TWDR0=0b10100000;
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
}while((TWSR0 & 0xF8) != 0x18);
//Now write ADDRH
TWDR0=(address>>8);
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x28)
return FALSE;
//Now write ADDRL
TWDR0=(address);
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x28)
return FALSE;
//Now write DATA
TWDR0=(data);
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x28)
return FALSE;
//Put Stop Condition on bus
TWCR0=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
//Wait for STOP to finish
while(TWCR0 & (1<<TWSTO));
//Wait untill Writing is complete
_delay_ms(1);
//Return TRUE
return TRUE;
}
uint8_t EEReadByte(uint16_t address)
{
uint8_t data;
//Initiate a Dummy Write Sequence to start Random Read
do
{
//Put Start Condition on TWI Bus
TWCR0=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x08)
return FALSE;
//Now write SLA+W
//EEPROM @ 00h
TWDR0=0b10100000;
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
}while((TWSR0 & 0xF8) != 0x18);
//Now write ADDRH
TWDR0=(address>>8);
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x28)
return FALSE;
//Now write ADDRL
TWDR0=(address);
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x28)
return FALSE;
//*************************DUMMY WRITE SEQUENCE END **********************
//Put Start Condition on TWI Bus
TWCR0=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x10)
return FALSE;
//Now write SLA+R
//EEPROM @ 00h
TWDR0=0b10100001;
//Initiate Transfer
TWCR0=(1<<TWINT)|(1<<TWEN);
//Poll Till Done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x40)
return FALSE;
//Now enable Reception of data by clearing TWINT
TWCR0=(1<<TWINT)|(1<<TWEN);
//Wait till done
while(!(TWCR0 & (1<<TWINT)));
//Check status
if((TWSR0 & 0xF8) != 0x58)
return FALSE;
//Read the data
data=TWDR0;
//Put Stop Condition on bus
TWCR0=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
//Wait for STOP to finish
while(TWCR0 & (1<<TWSTO));
//Return TRUE
return data;
}
Leave a Reply