PIC單片機(jī)的EEPROM讀寫實(shí)例及說(shuō)明
;****************************************************************************
; This is a program to test the function of readingwritting for EEPROM.
; You can observe the value of register(30H--?) buy changing "VALU" and "WRC_".
; Notice that:it must be { ADDR+WRC_=0ffh } !
;****************************************************************************
include "p16f877.inc"
ADDR EQU 20H ;寫入地址寄存器
VALU EQU 21H ;寫入值
REC_ EQU 22H ;讀計(jì)數(shù)
WRC_ EQU 24H ;寫計(jì)數(shù)
org 0
goto main
main
bcf STATUS,RP1
bcf STATUS,RP0 ;bank0
movlw 10h
movwf ADDR ;寫入EEPROM初始值
movlw 90h
movwf VALU ;初始寫入值
movlw 30h
movwf FSR ;間址,讀出值初始存放地址
movlw 0Fh
movwf WRC_ ;寫入次數(shù)
movwf REC_
;incf REC_ ;讀出次數(shù)
wri_ ;寫子程序
bsf STATUS,RP1
bsf STATUS,RP0 ;bank3
btfsc EECON1,WR
goto $-1
bcf STATUS,RP0
bcf STATUS,RP1 ;bank0
movf ADDR,W
bsf STATUS,RP1 ;bank2
movwf EEADR
bcf STATUS,RP1 ;bank0
movf VALU,W
bsf STATUS,RP1 ;bank2
movwf EEDATA
bsf STATUS,RP0 ;bank3
bcf EECON1,EEPGD ;to data memory
bsf EECON1,WREN
bcf INTCON,GIE
movlw 55h
movwf EECON2
movlw 0aah
movwf EECON2
bsf EECON1,WR
bcf STATUS,RP0
bcf STATUS,RP1 ;bank0
incf ADDR,1
decf VALU,1
decfsz WRC_ ;all write,to read_
goto wri_
read_ ;讀子程序
bcf STATUS,RP1
bcf STATUS,RP0 ;bank0
decf ADDR ;next value
bsf STATUS,RP1 ;bank2
movwf EEADR
bsf STATUS,RP0 ;bank3
EEwr.asm程序說(shuō)明:
1、 本程序是對(duì)PIC16F877單片機(jī)的EEPROM數(shù)據(jù)區(qū)進(jìn)行讀寫的演示程序;
2、 wri_為寫入數(shù)據(jù)子程序,該子程序從ADDR寄存器確定的地址開始對(duì)EEPROM數(shù)據(jù)區(qū)寫入(范例從10H開始),每進(jìn)行一次寫操作將使ADDR值加一;寫入數(shù)據(jù)的初始值由VALU寄存器給定(范例值為90H),每進(jìn)行一次寫操作將使VALU值減一;寫入規(guī)定個(gè)數(shù)(由WRC_寄存器給定,范例值為0FH)的數(shù)值后,結(jié)束寫入,轉(zhuǎn)至read_。
3、 read_為讀出數(shù)據(jù)子程序,該子程序從ADDR寄存器當(dāng)前值確定的地址開始對(duì)EEPROM數(shù)據(jù)區(qū)讀出(當(dāng)前值=ADDR+WRC_=0FFH),每進(jìn)行一次讀操作將使ADDR值減一;讀出數(shù)據(jù)后程序?qū)言摂?shù)據(jù)存入通用寄存器中,開始存放的地址由FSR的值指定(范例為30H),每進(jìn)行一次讀操作將使FSR的值加一;讀出規(guī)定個(gè)數(shù)(由寄存器REC_給定,范例值為0FH)的數(shù)值后,結(jié)束讀出,回main。
51單片機(jī)相關(guān)文章:51單片機(jī)教程
評(píng)論