EEPROM read/write

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

EEPROM read/write

1,920 Views
hasan
Contributor I
Hi all,
 
I'm trying to write a data to an AT25HP512 EEPROM from the master QG8... And after writing, I wanna read the written data back... Using the assembly in CW...  Reading all datasheets and the stuff, I couldnt just ge over this. See, im just a beginner.. I really need some help over this. Can anybody tell me how to, or where can i get some assembly code examples for this application?
 
Best regards,
 
Has
Labels (1)
0 Kudos
2 Replies

421 Views
bigmac
Specialist III
Hello Hasan,
 
The following code should demonstrate the technique of writing a single byte, and reading a single byte from the EEPROM.  The EEPROM device is capable of writing and reading multiple byte sequences, but this would require additional buffers to be used.
 
Regards,
Mac
 
; RAM usage:
EEADDR    EQU    RAM    ; Word register for EEPROM address
 
; EEPROM op-codes:
WRSR      EQU    1      ; Write status register
WRITE     EQU    2      ; Write EEPROM
READ      EQU    3      ; Read EEPROM
WRDI      EQU    4      ; Write disable
RDSR      EQU    5      ; Read status register
WREN      EQU    6      ; Write enable
 
; SPI settings:
SPIC1VAL  EQU    %01011100      ; SPI master, no interrupts,
                                ; CPOL=1, CPHA=1
SPIC2VAL  EQU    $00
SPIBDVAL  EQU    $00            ; SPI_CLK divisor = 2
SS        EQU    5              ; /SS output signal at PTB5
 
***************************************************************
* INITIALISE SPI
***************************************************************
 
SPI_INIT: MOV    #SPIC1VAL,SPIC1
          MOV    #SPIC2VAL,SPIC2
          MOV    #SPIBDVAL,SPIBR
          BSET   SS,PTB         ; Initial state is high
          BSET   SS,DDRB        ; Set /SS for output
          RTS
 
***************************************************************
* SPI TRANSACTION
* On entry, ACC = send byte value (SPISND only)
* On exit, ACC = returned byte value, and
* CF = 1 if OK, otherwise CF = 0 if timeout
***************************************************************
 
SPIRX:   ; Receive byte from SPI
         CLRA
 
SPISND:  ; Send byte to SPI
         STA    SPID
 
         ; Wait for SPI Send/Rx
         LDA    #10            ; Timeout period 100 cycles
ST1:     BRSET  SPIRF,SPIS,ST2 ; Exit if SPI send complete
         NOP
         DBNZA  ST1            ; Loop if not timeout
         CLC
ST2:     LDA    SPID           ; Also clears flag
         RTS
 
***************************************************************
* WRITE BYTE TO EEPROM
* On entry, word register EEADDR contains EEPROM address,
* ACC = byte value to be written
* On exit, CF = 1 if OK, or CF = 0 if timeout
***************************************************************
 
EEWRITE: PSHA                  ; Save byte value to be written
         BCLR   SS,PTB         ; Set /SS active
         LDA    #WREN          ; Write enable op-code
         JSR    SPISND         ; Send byte to SPI
         BCC    EW2            ; Exit if timeout
         BSET   SS,PTB         ; Set /SS inactive
         BCLR   SS,PTB         ; Set /SS active
         LDA    #WRITE         ; Write op-code
         JSR    SPISND         ; Send byte to SPI
         BCC    EW2            ; Exit if timeout
         LDA    EEADDR         ; High address byte
         JSR    SPISND         ; Send byte to SPI
         BCC    EW2            ; Exit if timeout
         LDA    EEADDR+1       : Low address byte
         JSR    SPISND         ; Send byte to SPI
         BCC    EW2            ; Exit if timeout
         PULA
         JSR    SPISND         ; Send byte to SPI
         BCC    EW3            ; Exit if timeout
         BSET   SS,PTB         ; Set /SS inactive
 
         ; Wait for write complete (maximum 10ms delay)
         STA    SRS            ; Clear COP timer
EW1:     BCLR   SS,PTB         ; Set /SS active
         LDA    #RDSR          ; Read status register op-code
         JSR    SPISND         ; Send byte to SPI
         BCC    EW3            ; Exit if timeout
         BSET   SS,PTB         ; Set /SS inactive
         AND    #$01           ; Test RDY bit
         BNE    EW1            ; Loop if not ready
         RTS
 
EW2:     ; Timeout error occurred
         PULA                  ; Adjust stack
EW3:     BSET   SS,PTB         ; Set /SS inactive
         RTS
         
***************************************************************
* READ BYTE FROM EEPROM
* On entry, word register EEADDR contains EEPROM address
* On exit, ACC = byte value, CF = 1 if OK, or CF = 0 if timeout
***************************************************************
 
EEREAD:  BCLR   SS,PTB         ; Set /SS active
         LDA    #READ          ; Read op-code
         JSR    SPISND         ; Send byte to SPI
         BCC    ER1            ; Exit if timeout
         LDA    EEADDR         ; High address byte
         JSR    SPISND         ; Send byte to SPI
         BCC    ER1            ; Exit if timeout
         LDA    EEADDR+1       : Low address byte
         JSR    SPISND         ; Send byte to SPI
         BCC    ER1            ; Exit if timeout
         JSR    SPIRX          ; Receive byte from SPI
ER1:     BSET   SS,PTB         ; Set /SS inactive
         RTS

Message Edited by bigmac on 2006-09-22 01:26 PM

0 Kudos

421 Views
hasan
Contributor I
By the way, I will use the SPI module... :smileyhappy:
0 Kudos