2012-03-09 150 views
0

我有一個任務來爲PIC16F84單片機寫入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的問題。它似乎並不正確。

回答

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