Thursday, November 1, 2012

ATmega32's Internal EEPROM


The EEPROM is an area of read/write memory that is non-volatile i.e., the stored data is not lost even if you power off the device.

The ATmega32 has a inbuilt EEPROM in it . Before starting to learn about using the internal EEPROM, let's get to know about the risk's that involve in using a Internal EEPROM.

  1. Internal EEPROM might get corrupted while the power is being switched off.
  2. Supply voltages are likely to rise or fall slowly during power up or down, this causes the device to operate at a voltage lower than the specified minimums voltage for the clock frequency used. This might lead to an unintentional write of the EEPROM.
Although the EEPROM memory memory may be both read and write, it is seldom used for general variable storage. The reason being the EEPROM memory being very slow to write, it can take up to a millisecond to complete a write operation one byte of memory. So an extensive use of this memory would slow down the processor appreciably.In order to avoid this a specific procedure is to be followed. Also, EEPROM can only withstand a only a limited number of read and write cycles. So knowing the risk factors and disadvantages let's see how to use the internal EEPROM of ATmega32.

The ATmega32 contains 1 kilobyte of internal memory which has a endurance of about 100000 write/erase cycles. This memory can be totally controlled using three registers in ATmega32.
  1. The EEPROM address register - EEARH and EEARL (note: this is a 16 bit register)
  2. The EEPROM data register - EEDR
  3. The EEPROM control register - EECR
The EEARH and EEARL used to specify the address of the 1024 bytes of EEPROM memory. The EEARH is the higher byte of the register while EEARL is the lower byte of the register. So why a 16 bit register ?? (If you didn't get it please read the basics)


Anyway as there are 1024 bytes (2^10) and each should have a unique address, which implies 1024 addresses in binary system a minimum of 10 digits are needed in order to specify all the addresses.
The rest of the 6 bits are always read as zero. 

So, if you want to write some value to #15 address the EEARL should be set to 0b00001111 and the EEARH should be set to 0b00000000.

The EEDR register is linked to the data to be written or to be read form a specified address in the EEPROM i.e, during a read operation the data can be retrieved from the EEDR register and during the write process the EEDR register is to be set to the value that is to be written in a specified address inn the EEPROM.

The EECR register is the control register (can be understood by the name itself)



Here each bit of the register has a specific purpose

The EERE bit (bit 0 - EEPROM Read Enable) if this bit is set to high the data from the specified address set in EEAR is read.

IMP : when this read action takes place, the CPU is halted for four cycles before it executes the next step.

The EEWE ( bit 1 - EEPROM Write Enable) it is the counter bit of EERE for write operation.
But in order to make the EEWE bit work the EEMWE bit must be set, otherwise setting the EEWE bit to high doesn't effect anything.

About EERIE nothing much to say about it, can be clearly understood from the datasheet.

Here is a the C code for writing data into EEPROM

void EEPROM_write(unsigned int uiAddress, unsigned char 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);
}

C Code for Reading

unsigned char EEPROM_read(unsigned int 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;
}



No comments :

Post a Comment