读写PIC16F877內部EEPROM的实例
扫描二维码
随时随地手机看文章
//**********************************************写入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 ----
;
movlw0x0
movwfData_EE_Addr
movlw'0'; Load a ASCII code "0" to W
movwfData_EE_Data
EE_Write
callWRITE_EEPROM
incfData_EE_Data,F; 0x30+ N
incfData_EE_Addr,F ; Point to next EEPROM Address
btfssData_EE_Addr,5; Write 32 bytes ?
gotoEE_Write
;
;---- Test the READ EEPROM Routine ----
;
movlw0x20; Get back EEPROM data to RAM with start addr at 0x20
movwfFSR
;
movlw0x0; Point to EEPROM start address at 0x00
movwfData_EE_Addr
EE_Read
callREAD_EEPROM; Read EEPROM data with curent address to W registre
movwfINDF; move W --> RAM
;
incfFSR,F
incfData_EE_Addr,F ; Point to next EEPROM Address
btfssData_EE_Addr,6; Read 32 bytes ?
gotoEE_Read
;
GOTO $
;
;------ INTERNAL EEPROM READ ------
;
READ_EEPROM
bankselEEADR
movfData_EE_Addr,W; Load EEPROM Address into Pointer
movwfEEADR
;
; bcf INTCON,GIE
bankselEECON1
bcfEECON1,EEPGD
bsfEECON1,RD
;
bankselEEDATA
movfEEDATA,W
; bsf INTCON,GIE
;
bankselPORTA
return
;
;----INTERNAL EEPROM WRITE-----
;
WRITE_EEPROM
bankselEEADR
movfData_EE_Addr,W; Load EEPROM Address & Data into Register
movwfEEADR
movfData_EE_Data,W
movwfEEDATA
;
bankselEECON1
bcfEECON1,EEPGD; Selected Data EEPROM for Writing
bsfEECON1,WREN; Enable write function
;bcfINTCON,GIE; Disable Interrupt
movlw0x55; Write Sequence control
movwfEECON2
movlw0xAA
movwfEECON2
bsfEECON1,WR; Start EEPROM write operation
;
;bsfINTCON,GIE; Enable Interrupt (If used interrupt )
bcfEECON1,WREN; Disable write EEPROM
;
btfscEECON1,WR; Waiting for writing completed (4mS)
goto$-1; During write period, must hold both EEADR & EEDATA until complete
;
bankselPORTA
return
;
;---------INITIAL----------------
INIT
BCF PIR2,EEIF
bankselPIE2
BCF PIE2,EEIE
RETURN
;--------------------------------
END