Organisation and Programming of EEPROM Data Memory of ATmega328

This note has been prepared after its presentation to the 3/2 students of AUST (Ahsanullah University of Science and Technology, Dhaka). Now, it is posted in the Arduino Forum for their (and others) online access and downloading. Any opinion, criticism, and suggestion from the readers would be highly appreciated to enrich the quality of the note.

6. Organization and Programming of EEPROM Data Memory of ATmega328 Microcontroller
(1) We have important data like Pass Word, Bank Account Number, Balance of a Prepaid Electrical Energy Meter and the similar which need to be stored in a special kind of ‘Data Memory’ so that they are not lost during power failure, are not stolen intentionally, and are not corrupted accidentally. Within the ATmega328 Microcontroller, we have a block of memory which exactly resembles this ‘Data Memory.’ This is a non-volatile data memory; it retains data (information) at the absence of power. Because, this data memory has been manufactured based on EEPROM Technology, we call it EEPROM Data Memory. EEPROM stands for Electrically Erasable Electrically Programmable Random Access Read Only Memory. A memory is called Random Access because, unlike serial memory (cassette tape), the average access time for any memory location is almost the same regardless of its address value. In ATmega328, we have another kind of data memory called SRAM (Static RAM) Data Memory; it is a volatile memory meaning that the memory loses its all information at the absence of power.

(2) The conceptual view of EEPROM Data Memory of ATmega328 is depicted below.

(3) There are small slots within the Data Memory called memory locations.
(4) It is a non-volatile memory meaning that the memory does not lose information at the absence of power.

(5) Every memory location has an identification number called address (adr).
(6) In ATmega328, the address is a 16-bit (4-digit hex) number
(7) There are in total 1024 memory location inside the data memory. Each memory location can hold 8-bit data called byte. In Computer Science 1024 is taken as K; so, the capacity of the data memory is 1K.

** **(8)** **
When chip erase is done on the ATmega328, all contents of this data memory get erased (all bits turn to LH) along with the contents of the Application Section, Boot Section, and Interrupt Vector Section. However, the contents of the data memory can be retained during chip erase by configuring the values of the Fuse Bits.

(9) The data memory can be erased by one of the following methods:
(a) Parallel Programming (using commercial ROM Programmer like TOP2012),
(b) Serial Programming (using In-system Programming Interface of Atme328), and
(c) Program Instruction when a byte location is automatically erased before being written by a new data.

(10) New data can be written into the data memory (while the ATmega328 is not doing any job) by one of the following methods.
(a) Parallel Programming (using commercial ROM Programmer like TOP2012),
(b) Serial Programming (using In-system Programming Interface of Atme328), and

(11) New data can also be written into the data memory (while the ATmega328 is doing a job) by the processor itself through the execution of program instructions, In this process, data writing takes place byte-by-byte. At the event of initiating a write process, the addressed location is automatically erased before being written by a new data. A self-timing function lets the user software know the end of the current write cycle.

(12) The data memory has endurance of at least 100,000 erase/write cycles.
(13) Unlike the code memory, the EEPROM Data Memory can get corrupted during power and power down occasions. To prevent the data from being corrupted, the ATmega328 incorporates the BOD Logic (Brown-out Detection). The BOD circuit continuously monitors the Vcc supply to check when the supply has arrived at the unsafe value for the MCU. The MCU immediately enters into reset state; as a result, the MCU cannot perform any erratic write operation on the EEPROM Data memory.

(14) The data memory has association with Fuse Bits to pre-define its operational modes like whether the contents would be preserved or not during chip erase and etc.

(15) The data memory has lock bits (security bits) which prevents others from accessing/copying its contents.

(16) The following IO registers are involved with the instruction-based programming of the data memory.
(a) EEPROM Address: EEARH, EEARL
(b) EEPROM Data Register: EEDR
(c) EEPROM Control Register: EECR

(17) The following are the experimentally verified steps which one must follow to write a data byte into an EEPROM location.
(a) Select the option of ‘Erasing and writing during same operation.’
(b) Assert address of the target memory location.
(c) Assert data for the target memory location.
(d) Enable only the EEMPE (EEPROM Master Programming Enable) bit by a transition from Low-to-High.
(e) Enable the EEPE (EEPROM Programming Enable) bit without disturbing the value of the previously set EEMPE bit. It is done by making a transition from Low-to-High.
(f) Wait until EEPE bit assumes LL state. (This indicates the end of writing.) After write operation, both the EEMPE and EERE bits automatically assume LL states.

(18) Assembly Codes (ATmega32) for the steps of Section-17.

        LA: ; this option is not available in ATmeg32; it is available in ATmega328.
 LB: ldi r16, 0x10
 out EEARH, r16
 ldi r16, 0x00
 out EEARL, r16 ; the target location address: 0100h
 LC: ldi r16, 0x05 ; data is 05h
 LD: ldi r16, 0x04 ; 0000 0100
 0ut EECR, r16
 nop
 LE: ldi r16, 0x06 ; 0000 0110
 out EECR, r16
 LF: sbis EECR, EEPE ; the status of EEPE-bit is being pulled. It can also be known via interrupt
 rjmp LG ; write done
 rjmp LF ; write operation still in progress
 LG: ;--------------------

(19) C Codes for the steps of Section-17/18 without including the EEPROM.h file (ATmega328).

   LA: EECR= 0x00;
 LB: EEARH = 0x10;
 EEARL = 0x00;
 LC: EEDR = 0x05;
 LD: EECR = 0x04;
 ;
 LE: EECR = 0x06;
 LF: while(bitRead(EECR, 1) !=LOW)
 ;
 LG: ;--
 
[b](20)[/b] C Codes for the steps of Section-17/18/19 including the EEPROM.h file.
Let us review the purpose of a header file in C Programming. Codes of Section-19 are considered as C codes as these codes can be processed by a C Compiler. However, the program contains registers’ names which are assembly level activities, and they do not fit well with the spirit of a HLL like C. The purpose of a header file is to hide all these low level activities to the programmer; it allows a programmer to view almost all objects from a higher level perspectives meaning making these objects be appearing as plain English-like statements. The EEPROM.h file contains the definitions of the high level functions, methods, statements, and expressions that are to be used in the programming algorithms of EEPROM Data Memory of ATmega328. Few functions and methods are shown below along with their closely matched assembly equivalents. Let us remember that a Compiler is generally composed of three passes. The 1st pass performs the pre-processing where the symbolic definitions are replaced by their numerical equivalents by appending the textual contents of the header files; the 2nd pass produces the intermediate assembly codes; the 3rd pass makes the re-locatable machine codes.   

Arduino C Functions/Methods Assembly Equivalents
[i]EEPROM.write (adr, val); all lines of Section-18/19[/i]

[b](21)[/b] The following are the experimentally verified steps which one must follow to read a data byte from an EEPROM location.
(a) Assert address of the target memory location.
(b) Enable EERE (EEPROM Read Enable Bit) by making transition from Low-to-High
     (c)     Read data from the EEDR (EEPROM Data Register).
     (d) Disable EERE bit.
  
[b](22)[/b] Assembly Codes (ATmega328) for the steps of Section-21.
[code]
 LA: ldi r16, 0x10
 out EEARH, r16
 ldi r16, 0x00
 out EEARL, r16 ; the target location address: 0100h
 LB: ldi r16, 0x01 ; Bit-1 is the EERE (read enable) bit
 out EECR, r16
 LC: in r16, EEDR ; data in r16 register
        LD: ldi r16, 0x00 ; RE negated
 out EECR, r16
 LE: ;--------------

(23) C Codes for the steps of Section-17/18 without including the EEPROM.h file.

        LA: EEARH = 0x10;
 EEARL = 0x00;
 LB: EECR = 0x01 ; Enable bit for EERE 
 ; ; wait for a while
 LC: byte x = EEDR;
 LD: EECR = 0x00;
 LE: ;----------------------

(24) C Codes for the steps of Section-17/18/19 including the EEPROM.h file.
Arduino C Functions/Methods Assembly Equivalents
byte x = EEPROM.read(adr); all lines of Section-22/23

qw (1).pdf (178 KB)