PIC16F84 - eeprom读写

时间:2012-03-09 09:00:30

标签: assembly pic mplab eeprom

我有一项任务是为PIC16F84微控制器编写eeprom读/写子程序。我写了这样的代码:

    EEPROM_Read
        clrw
        bsf     STATUS, RP0  ; Bank0
        movlw   DATA_EE_ADDR  ; Any location in Data EEPROM memory space
        movwf   EEADR        ; Address to read
        bsf     EECON1, RD   ; EE Read

        movlw   EEDATA
        bcf     STATUS, RP0  ; Bank0
        return

EEPROM_Write        
        bcf     INTCON,GIE      ;disable interrupts for EEPROM write
            banksel     EEADR           ;bank 2
            movfw       DATA_EE_ADDR        ;load write address to EEPROM address pointer
            movwf       EEADR
            movfw       DATA_EE_DATA        ;load write data to EEPROM data buffer
            movwf       EEDATA
            banksel     EECON1          ;bank 3
            bsf         EECON1,WREN         ;enable EEPROM write
            movlw       0x55 ;unlock codes
            movwf       EECON2
            movlw       0xAA
            movwf       EECON2
            bsf         EECON1,WR       ;enable EEPROM write
            btfsc       EECON1,WR       ;wait here until write completes
            goto        $-1
            bcf         EECON1,WREN     ;write complete, disable write mode
            banksel     0 ;bank 0
            movlw       0 ;are interrupts being used?
            xorwf       INTCON,W
            btfss       STATUS,Z
            bsf     INTCON,GIE      ;yes,enable interrupts
            return

是不是?我有EEProm_Read的问题。它似乎无法正常工作。

1 个答案:

答案 0 :(得分:0)

根据数据表信息,您的EEPROM读取代码是错误的,因为EEADR寄存器位于存储区0中,而不是代码盒中的存储区1中:

正确的代码是:

BCF    STATUS, RP0    ; Bank 0
MOVLW  DATA_EE_ADDR
MOVWF  EEADR          ; Address to read
BSF    STATUS, RP0    ; Bank 1
BSF    EECON1, RD     ; EE Read
BCF    STATUS, RP0    ; Bank 0
MOVF   EEDATA, W      ; W = EEDATA