Mastering Atmega32: Exploring EEPROM Programming

The Atmega32 microcontroller consists of two primary memory spaces: the Data Memory and the Program Memory, along with an additional EEPROM Memory designated for data storage. This EEPROM Memory is non-volatile, contributing to the controller’s three distinct memory spaces, all of which exhibit linear and regular properties.

The EEPROM memory space stores a maximum of 1024 bytes of data, separated from the data space. It allows for single-byte reading and writing operations and endures approximately 100,000 write/erase cycles. The access time for this space is approximately 8.5ms when using the internal RC clock set at 1MHz.

Even when the device enters the power-down sleep mode, the EEPROM write operation persists. However, it’s important to note that the device is not entirely powered down during this phase. Therefore, it’s advisable for the programmer to verify the EEPROM before transitioning the device into a complete power-down mode.

Instances of EEPROM corruption can arise due to insufficient supply voltage leading to erroneous instruction execution. To mitigate this, enabling the internal Brown-out Detector (BOD) can be effective. If the BOD fails to address the issue, an additional VCC reset protection circuit can be added as a preventive measure.

In Assembly language, low-level programming necessitates the direct use of I/O and Peripheral Registers. However, when employing C programming for the AVR, these registers remain concealed from the programmer.

Before accessing the EEPROM memory, specific registers need to be utilized.

The EEPROM Address Register – EEARH and EEARL

The address register consists of a 16-bit pair, but only 10 bits are utilized, resulting in a 10-bit (1024) address space. This configuration allows users to access a maximum of 1024 bytes within the EEPROM memory.

The EEPROM Data Register – EEDR
The EEPROM Control Register – EECR

This memory comprises:

Bits 7 to 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 on an AVR device, managing these registers isn’t necessary. Programmers can utilize the eeprom.h library, accessing the EEPROM memory through its read and write functions.

However, in this programming instance, we’ll utilize all these registers. The provided example will demonstrate a straightforward approach to accessing the internal EEPROM.

Program Simulation

The controller will read and write the internal EEPROM data. The data will display on PortC.

  1. /*
  2.  * m32Eeprom.c
  3.  *
  4.  * Created: 7/20/2022 6:43:00 PM
  5.  * Author : desktop
  6.  */
  7. #include <avr/io.h>
  8. /*Write button connects to PD6*/
  9. #define writeButton ((PIND&0x40)==0)
  10. /*Read button connects to PD7*/
  11. #define readButton ((PIND&0x80)==0)
  12. /*EEPROM Write Function*/
  13. void eepromWrite(unsigned int addr,unsigned char dat){
  14. /*Wait for completion of previous write*/
  15. while(EECR&(1<<EEWE));
  16. /*Set up address and data registers*/
  17. EEAR=addr;
  18. EEDR=dat;
  19. /*Write logical 1 to EEMWE*/
  20. EECR|=(1<<EEMWE);
  21. /*Start eeprom write by setting EEWE*/
  22. EECR|=(1<<EEWE);
  23. }
  24. /*EEPROM Read Function*/
  25. unsigned char eepromRead(unsigned int addr){
  26. /*Wait for completion of previous write*/
  27. while(EECR&(1<<EEWE));
  28. /*Set up address register*/
  29. EEAR=addr;
  30. /*Start eeprom read by writing EERE*/
  31. EECR|=(1<<EERE);
  32. /*Return data from data register*/
  33. return EEDR;
  34. }
  35. int main(void)
  36. {
  37. /*PORTC Output*/
  38. DDRC=0xFF;
  39. /*PA0…PA3 Input*/
  40. DDRA=0xF0;
  41. /*PORTB Input*/
  42. DDRB=0x00;
  43. /*Turn on porta and portb high*/
  44. PORTA=0x0F;
  45. PORTB=0xFF;
  46. /*Turn on PD6 and PD7*/
  47. PORTD=(1<<6)|(1<<7);
  48. while(1)
  49. {
  50. /*eeprom read task*/
  51. if(readButton)
  52. {
  53. /*Wait until the button released*/
  54. while(readButton);
  55. PORTC=eepromRead(PINA);
  56. }
  57. /*eeprom write task*/
  58. if(writeButton)
  59. {
  60. /*Wait until the button released*/
  61. while(writeButton);
  62. eepromWrite(PINA,PINB);
  63. }
  64. }
  65. }

PortA functions as the input for addresses. Specifically, we utilize four bits capable of accommodating up to 16 addresses. PortB, on the other hand, serves as the input for data used in internal EEPROM writing. The write button is linked to the PD6 pin, while the read button connects to the PD7 pin. Furthermore, PortC is interfaced with a bar graph LED to visually display the data retrieved from the internal EEPROM.

Should you require a conventional PCB designed for the ATMega32 micro-controller, you have the option to purchase my AVR Microcontroller project through PCBWay at an affordable rate. Click this link to obtain a complimentary $5 credit for new accounts.

 


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top