Thursday, January 26, 2006

8051: Example 13

Handling EEPROM

This program writes data to on-chip EEPROM memory. In this case, the data is a hexadecimal number 23 which is to be written to the location with address 00.
To make sure that this number is correctly written, the same location of EEPROM is read 10mS later in order to compare these two numbers. If they match, F will be displayed. Otherwise, E will be displayed on the LED display (Error).
;************************************************************************
;* PROGRAM NAME: EEProm1.ASM
;* DESCRIPTION: Programming EEPROM at address 0000hex and displaying message
;* on LED display.
;************************************************************************

;BASIC DIRECTIVES

$MOD53
$TITLE(EEPROM1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING

WMCON     DATA     96H
EEMEN     EQU      00001000B              ; Access to internal EEPROM is enabled
EEMWE     EQU      00010000B              ; Write to EEPROM is enabled
TEMP      DATA     030H                   ; Define Auxiliary register

THE END   EQU      071H                   ; Display "F" 
ERROR     EQU      033H                   ; Display "E" 

;STACK
          DSEG     AT     03FH
STACK_START:       DS     040H

;RESET VECTORS
          CSEG     AT     0
          JMP      XRESET                 ; Reset vector

          ORG      100H

XRESET:   MOV      IE,#00                 ; All interrupts are disabled
          MOV      SP,#STACK_START

          MOV      DPTR,#0000H            ; Choose location address in EEPROM
          ORL      WMCON,#EEMEN           ; Access to EEPROM is enabled
          ORL      WMCON,#EEMWE           ; Write to EEPROM is enabled
          MOV      TEMP,#23H              ; Number written to EEPROM is moved to
          MOV      A,TEMP                 ; register TEMP and Accumulator
          MOVX     @DPTR,A                ; Write byte to EEPROM
          CALL     DELAY                  ; 10ms delay
          MOVX     A,@DPTR                ; Read the same location and compare to TEMP,
          CJNE     A,TEMP,ERROR           ; If they don't match, jump to label ERROR
          MOV      A,#KRAJ                ; Display F (correct)
          MOV      P1,A
          XRL      WMCON,#EEMWE           ; Write to EEPROM is disabled
          XRL      WMCON,#EEMEN           ; Access to EEPROM is disabled
LOOP1:    SJMP     LOOP1                  ; Remain here

ERROR:    MOV      A,#ERROR               ; Display E (error)
          MOV      P1,A
LOOP2:    SJMP     LOOP2

DELAY:    MOV      A,#0AH                 ; Delay
          MOV      R3,A
LOOP3:    NOP
LOOP4:    DJNZ     B,LOOP4
LOOP5:    DJNZ     B,LOOP5
          DJNZ     R3,LOOP3
          RET

          END                             ; End of program