I am using ATmega16 and want to write a character or any data to EEPROM of AVR? Is the following function call correct? How many other ways you can use the functions? Thanks
void EEPROM_write(unsigned int uiAddress, unsigned char ucData);
unsigned char EEPROM_read(unsigned int uiAddress);
void main(void)
{
EEPROM_write(0x10, 'A'); //write A to the add. 0x10 assuming there is such an add.
EEPROM_read(0x10); //
}
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
while(EECR && (1<<EEWE)); /* Wait for completion of previous write */
EEAR = uiAddress; /* Set up address and data registers */
EEDR = ucData;
EECR |= (1<<EEMWE); /* Write logical one to EEMWE */
EECR |= (1<<EEWE); /* Start eeprom write by setting EEWE */
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
while(EECR && (1<<EEWE)) ; /* Wait for completion of previous write */
EEAR = uiAddress; /* Set up address register */
EECR |= (1<<EERE); /* Start eeprom read by writing EERE */
return EEDR; /* Return data from data register */
}
To test if the write and read functions for EEPROM works, I devised the following little test. When I keep the function call of EEPROM_write(0x10, write); , I get the result I expect. But when I comment out this function. I do not read the expected character T. The character T should have already been saved there and I should get a reading from EEPROM of T. What is going on here. Thanks
void EEPROM_write(unsigned int uiAddress, unsigned char ucData);
unsigned char EEPROM_read(unsigned int uiAddress);
void transmit(unsigned char data);
unsigned char receive(void);
void InitUART(void);
void main(void)
{
char write='T';
char read;
InitUART();
//EEPROM_write(0x10, write);
read=EEPROM_read(0x10); //
transmit(read);
}
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
while(EECR && (1<<EEWE)); /* Wait for completion of previous write */
EEAR = uiAddress; /* Set up address and data registers */
EEDR = ucData;
EECR |= (1<<EEMWE); /* Write logical one to EEMWE */
EECR |= (1<<EEWE); /* Start eeprom write by setting EEWE */
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
while(EECR && (1<<EEWE)) ; /* Wait for completion of previous write */
EEAR = uiAddress; /* Set up address register */
EECR |= (1<<EERE); /* Start eeprom read by writing EERE */
return EEDR; /* Return data from data register */
}