I am working on my college project which needs to store data in EEPROM of AtMega32. I am able to write and read data at any particular location of memory. But when I try to write data sequentially form address 0 to 1023 I am getting wrong values.
Here are the functions I have written.
Function definition to read and write data
#include "eeprom.h"
uint8_t EEPROMRead(uint16_t uiAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
return EEDR;
}
void EEPROMWrite(uint16_t uiAddress, uint8_t ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
}
Here is main function
static int epadr=0;
epread=EEPROMRead(epadr); //reading from address stored in epadr
printf("%d",epread); //printing values
if(epadr<=1023)
{
EEPROMWrite(epadr,high); //writing at address stored in epadr
epadr++; //increment address
}
}
if(epadr>1023)
printf("Memory Full\n");
I want to store data from location 0 to 1023. Please tell what is wrong with this code.
EEAR
,EECR
, andEEDR
etc variables? – Daiepadr
? Do you rely on zero init of static storage duration variables? Doing so is bad practice, particularly in embedded systems, that often have non-standard start-up code which skips zero initialization. – Lundinif(epadr<=1023)
only writes to address 0 and not from 0 to 1023, yeah? There's no loop. – Lundin