读写PIC16F877內部EEPROM的实例
2016-11-17 来源:eefocus
//**********************************************写入1字节到eerom********************************////// void write_eprom(unsigned char date,unsigned char addr) { while(wr==1);//等待写周期完成 eeadr=addr;//准备要写入的地址 eedata=date;//准备要写入的数据 eepgd=0;//to data memory wren=1;//允许写 gie=0; eecon2=0x55; eecon2=0xaa; wr=1;//启动写周期 while(wr==1);//等待写周期完成 gie=1; wren=0; } //****************************************************读取设定值********************************************** unsigned char read_eprom(unsigned char addr) { eeadr=addr; eepgd=0; rd=1; return eedata; } | ||
LIST P=16f877
#i nclude
;
;- RAM LOCATIONS------
;
; Reserve RAM location 0x70 for ICD
;
Data_EE_Addr equ 0X71
Data_EE_Data equ 0X72
;--------VECTORS------------
org 0x0
nop
goto START
;-------PROGRAM--------------
org 0X20
START
call INIT
;
;---- Test the WRITE EEPROM Routine ----
;
movlw 0x0
movwf Data_EE_Addr
movlw '0' ; Load a ASCII code '0' to W
movwf Data_EE_Data
EE_Write
call WRITE_EEPROM
incf Data_EE_Data,F ; 0x30+ N
incf Data_EE_Addr,F ; Point to next EEPROM Address
btfss Data_EE_Addr,5 ; Write 32 bytes ?
goto EE_Write
;
;---- Test the READ EEPROM Routine ----
;
movlw 0x20 ; Get back EEPROM data to RAM with start addr at 0x20
movwf FSR
;
movlw 0x0 ; Point to EEPROM start address at 0x00
movwf Data_EE_Addr
EE_Read
call READ_EEPROM ; Read EEPROM data with curent address to W registre
movwf INDF ; move W --> RAM
;
incf FSR,F
incf Data_EE_Addr,F ; Point to next EEPROM Address
btfss Data_EE_Addr,6 ; Read 32 bytes ?
goto EE_Read
;
GOTO $
;
;------ INTERNAL EEPROM READ ------
;
READ_EEPROM
banksel EEADR
movf Data_EE_Addr,W ; Load EEPROM Address into Pointer
movwf EEADR
;
; bcf INTCON,GIE
banksel EECON1
bcf EECON1,EEPGD
bsf EECON1,RD
;
banksel EEDATA
movf EEDATA,W
; bsf INTCON,GIE
;
banksel PORTA
return
;
;----INTERNAL EEPROM WRITE-----
;
WRITE_EEPROM
banksel EEADR
movf Data_EE_Addr,W ; Load EEPROM Address & Data into Register
movwf EEADR
movf Data_EE_Data,W
movwf EEDATA
;
banksel EECON1
bcf EECON1,EEPGD ; Selected Data EEPROM for Writing
bsf EECON1,WREN ; Enable write function
; bcf INTCON,GIE ; Disable Interrupt
movlw 0x55 ; Write Sequence control
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ; Start EEPROM write operation
;
; bsf INTCON,GIE ; Enable Interrupt (If used interrupt )
bcf EECON1,WREN ; Disable write EEPROM
;
btfsc EECON1,WR ; Waiting for writing completed (4mS)
goto $-1 ; During write period, must hold both EEADR & EEDATA until complete
;
banksel PORTA
return
;
;---------INITIAL----------------
INIT
BCF PIR2,EEIF
banksel PIE2
BCF PIE2,EEIE
RETURN
;--------------------------------
END