Microcontroller Advanced Kit -
Non Volatile RAM Memory - EEPROM
All Flash programmable
versions of the 8051 have a Flash memory space
that is used for storage of the program memory.
The memory is nonvolatile, meaning it does not
go away when you turn off the power. But the
values in this space can not be changed by the
chip itself. You must use a device programmer (like
our PG302) to erase these memory spaces and
write new data to them.
The Atmel AT89S8252 is
an 8051 compatible microcontroller that has the
normal Flash space for program memory (8K) and
also includes 2K of nonvolatile EEPROM space.
This memory space works the same as the Flash
space but can be written to and read from by the
chip itself during program execution. This
allows you to store data that does not get
erased when the power goes away. (Good for
California summers!)
The process of writing to the EEPROM can be a
little complicated. That explains why there is a
whole web page for it here.
First you need to define an extra register
that is not included in most 8051s. The WMCON
register is at memory location 96H. To simply
writing to the register we assign the name WMCON
to the memory location:
WMCON .EQU 096H
The register has 3 bits that we use to read
and write to the EEPROM.
EEMWE - Bit 4 - The EEPROM Data Memory Write
Enable Bit. This needs to be set to 1 to
write to the EEPROM. Set to 0 after writing
to EEPROM.
EEMEN - Bit 3 - Internal EEPROM Access
Enable. Set this to 1 to tell the chip to
access the EEPROM when you use the MOVX
instruction.
RDY/BSY - Bit 1 - Ready/Busy. This bit is
normally 1. When you write data to the
EEPROM this bit goes to 0. When it is 1
again you can write more data or read from
the EEPROM. (You can do other stuff while
you are waiting for the write operation to
complete.)
So the first thing we do is enable EEPROM access
with the following command:
MOV WMCON,
#00001010B
The bits go from Bit 7 on the left to Bit 0
on the right, before the B which signifies that
this is a binary number. The EEMEN bit is set to
1. The RDY/BSY bit should already be 1 and we
are basically leaving it at 1.
Next is our routine for writing a data value
to the EEPROM. This routine assumes that the
address location you are writing to has already
been moved into the variable ADDRESS and the
data value has been moved into the variable
DATAOUT. Look at the complete example at the end
of the page to see how variables are defined.
First we enable writing to the EEPROM with the
first MOV command. Then we move the address to
the data pointer, move the data value to the
accumulator (A) and write to the EEPROM using
the MOVX command. The next 3 lines form a loop
that continually check the RDY/BSY bit in WMCON
to check if the write operation has been
completed. The write operation can take as long
as 2.5 milliseconds. For most programs you would
probably go do something else (check football
scores maybe?) rather than just sitting around
waiting. Then the last line before the RETurn
command disables EEPROM writing.
WRITEEEPROM:
MOV WMCON,
#00011010B ; Enable Writing to EEPROM
MOV
DPH,
#00H ; Set up high byte of data
address
MOV
DPL,
ADDRESS ; Set up low byte of data
address
MOV
A, DATAOUT
; Move Data Out value to A
MOVX
@DPTR, A
; Write Data Value to EEPROM at address DPH:DPL
WRITEEEPROMWAIT:
MOV
A, WMCON
CJNE
A, #00011010B,
WRITEEEPROMWAIT ; Check if EEPROM write is
complete (Bit 1 will go to 1)
MOV WMCON,
#00001010B ; Reset Control Register to
normal operation (Memory Write Disabled)
RET
Reading from the EEPROM is a much simpler
operation. We simply move the address to the
data pointer, read from the EEPROM using the
MOVX command, and transfer the byte that was
read to the DATAIN variable.
READDATAIN:
;
MOV
DPH,
#00H ; High byte of address
MOV
DPL,
ADDRESS ; Set low byte of EEPROM
address
MOVX
A, @DPTR
; Read Data from EEPROM at Address DPH:DPL
MOV DATAIN,
A
; Move Data Value to DataIn
RET