There are two main memory spaces in Atmega32 microcontroller, the Data Memory and the Program Memory space. An additional EEPROM Memory is useful for data storage. It's non-volatile. Hence this controller has three memory space. They are linear and regular.
EEPROM memory space contains up to 1024 bytes of data. It's separated from data space. It's single byte readable and writable. It endurance is up to 100,000 write/erase cycles. Its access time is around 8.5ms for the internal RC clock of 1MHz.
During the device is in power-down sleep mode, the EEPROM write operation still continuous. But it's not completely power down. So the the programmer must verify the EEPROM first before setting the device to entirely power-down mode.
EEPROM corruption can occur whenever the supply voltage is too low, that the instruction execution is incorrect. We can prevent it by enabling the internal Brown-out Detector (BOD). If the BOD can not help ,we can add an additional VCC reset Protection circuit.
For low level programming like the Assembly language, the programmer must use the I/O and Peripheral Register. However in C programming for the AVR it's invisible for the programmer.
There are some registers that we have to use before accessing the EEPROM memory.
The EEPROM Address Register – EEARH and EEARL |
The EEPROM Data Register – EEDR |
The data register is single byte read/write.
The EEPROM Control Register – EECR |
This memory contains,
- Bits 7..4 – Reserved Bits
- Bit 3 – EERIE: EEPROM Ready Interrupt Enable
- Bit 2 – EEMWE: EEPROM Master Write Enable
- Bit 1 – EEWE: EEPROM Write Enable
- Bit 0 – EERE: EEPROM Read Enable
For AVR Lib-C for AVR device, we don't need to care about these registers. the programmer can use the eeprom.h library, and call its read and write function to access the EEPROM memory.
In this programming example we will use all these registers. The example below will show a simple way to access the internal EEPROM.
Program Simulation |
/* * m32Eeprom.c * * Created: 7/20/2022 6:43:00 PM * Author : desktop */ #include <avr/io.h> /*Write button connects to PD6*/ #define writeButton ((PIND&0x40)==0) /*Read button connects to PD7*/ #define readButton ((PIND&0x80)==0) /*EEPROM Write Function*/ void eepromWrite(unsigned int addr,unsigned char dat){ /*Wait for completion of previous write*/ while(EECR&(1<<EEWE)); /*Set up address and data registers*/ EEAR=addr; EEDR=dat; /*Write logical 1 to EEMWE*/ EECR|=(1<<EEMWE); /*Start eeprom write by setting EEWE*/ EECR|=(1<<EEWE); } /*EEPROM Read Function*/ unsigned char eepromRead(unsigned int addr){ /*Wait for completion of previous write*/ while(EECR&(1<<EEWE)); /*Set up address register*/ EEAR=addr; /*Start eeprom read by writing EERE*/ EECR|=(1<<EERE); /*Return data from data register*/ return EEDR; } int main(void) { /*PORTC Output*/ DDRC=0xFF; /*PA0...PA3 Input*/ DDRA=0xF0; /*PORTB Input*/ DDRB=0x00; /*Turn on porta and portb high*/ PORTA=0x0F; PORTB=0xFF; /*Turn on PD6 and PD7*/ PORTD=(1<<6)|(1<<7); while (1) { /*eeprom read task*/ if (readButton) { /*Wait until the button released*/ while(readButton); PORTC=eepromRead(PINA); } /*eeprom write task*/ if (writeButton) { /*Wait until the button released*/ while(writeButton); eepromWrite(PINA,PINB); } } }
Click here to download its source file.
PortA is used for address input. We use only four bits that contain up to 16 addresses. PortB is used for data input for internal EEPROM writing. Write button connect to PD6 pin while read button connect to PD7 pin. PortC connects to a bar graph LED to display the data reading from internal EEPROM.
If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.
No comments:
Post a Comment