Path: Home => AVR-Overview => Hardware => EEPROM

; Demonstrates the use of the EEPROM

;
; During programming a counter location, defined in the EEPROM,
; is set to zero. At every restart of the processor this counter is incremented
; and its content is displayed in hex format on the LEDs.
; Please refer to the hints given on the end of this code to avoid
; some confusion.
;
.NOLIST
.INCLUDE "8515def.inc"
.LIST
;

; Define constants

;
.equ   cnt=$0000 ; Adress of the counter location in the EEPROM
;

; Define registers

.def   mpr=R16 ; Universal register
.def   neu=R17 ; Counter value interim storage
;

; Reset-/Interrupt-Vector

   RJMP   main ; Jump to main program
;
main:
   LDI   mpr,$FF ; all bits of Port B are output
   OUT   DDRB,mpr

; Program reads a byte in the EEPROM

   LDI   mpr,LOW(cnt) ; Set EEPROM adress
   OUT   EEARL,mpr ; to the EEPROM-adress port
   LDI   mpr,HIGH(cnt) ; Low/High-Byte separate
   OUT   EEARH,mpr ; EEPROM has 512 Byte locations
   SBI   EECR,EERE ; Set Read-Enable-Bit EERE
       ; in EEPROM-Control-Register EECR
   IN   neu,EEDR ; Read byte from EPROM-location
; Increment counter and write back to EEPROM
   INC   neu
wart:

; If EEPROM busy, wait first

   SBIC   EECR,1 ; Busy Bit 1 in EEPROM-Control-Register
   RJMP   wart ; not ready, wait
; The EEPROM-Adress is still correct, so we don't need to adjust it
; We just transfer the new data to the EEPROM-data register
   OUT   EEDR,neu ; New value to EEPROM-Data register
; The following two write commands must not be interrupted, because safe write operations
; are only valid if these come within four commands. Otherwise hardware skips the write operation.
; Interrupts must be disabled therefore (interrupts are not used here).
   CLI

; Now the two write commands:

   SBI   EECR,EEMWE ; Switches EEPROM Master Write Enable on
   SBI   EECR,EEWE ; Enables write operation in the EEPROM
; Within the following 1,5 milliseconds the byte is written to the EEPROM location.
; This is sensitive only in case you want to use the EEPROM for further operations.
; Not here: We write the inverted content of the counter to the LEDs on Port B
; and end the program in an indefinit loop.
   COM   neu ; invert
   OUT   PORTB,neu ; to Port B
loop:   RJMP   loop ; wait forever.

; Here starts the setting of the counter to zero during programming

; First we have to tell the assembler that the following code goes to the EEPROM segment
; and not to the code segment:
.ESEG

; Now the content of the EEPROM-segment:

.DB   $00 ; A Byte with a zero
; That's it.

; IMPORTANT HINTS
; During programming the content of the EEPROM-file TESTEEP.EEP
; will be loaded separately and programmed after the code is loaded.
; Don't forget this!
; During the programming sequence of the different locations for code
; and EEPROM content the software for the board releases the Reset pin
; of the processor, e.g. between programming and verification. As this
; short pause already causes the processor to restart and execute the
; code. Verification of the EEPROM content will therefore fail, because
; the counter is already incremented and does not match its original
; programmed value. Every read operation of the EEPROM content
; onboard will have the same effect.
; The execution of the restart command using the ISP software also
; causes multiple startups of the processor and increases the counter
; value, so don't expect to see correct counting values.
; Exact up-counts are only seen when switching the supply voltage
; of the board off and on.
; To avoid unwanted upcounting during program, verification and read
; operation would require setting a startup delay time, but this is a
; little bit too complex for a beginner.


Back to the Hardware page

©2002 by http://www.avr-asm-tutorial.net