;*******************************
;读取FLASH校验状态
;*******************************
STATUS_CHECK
MOVLB 3
MOVLW CHECK_STATUS_LO
MOVWF PMADRL
MOVLW CHECK_STATUS_HI
MOVWF PMADRH
BCF PMCON1,CFGS
BSF PMCON1,RD
NOP
NOP
MOVF PMDATL,0
MOVWF CHECK_STATUS
RETURN
;*******************************
;ERASE FLASH
;*******************************
ERASE_FLASH
BCF INTCON,GIE ; Disable ints so required sequences will execute properly
MOVLB 3
MOVF ADDRL,0 ; Load lower 8 bits of erase address boundary
MOVWF PMADRL
MOVF ADDRH,0 ; Load upper 6 bits of erase address boundary
MOVWF PMADRH
BCF PMCON1,CFGS
BSF PMCON1,FREE ; Specify an erase operation
BSF PMCON1,WREN ; Enable writes
MOVLW 55h ; Start of required sequence to initiate erase
MOVWF PMCON2 ; Write 55h
MOVLW 0AAh ;
MOVWF PMCON2 ; Write AAh
BSF PMCON1,WR ; Set WR bit to begin erase
NOP ; NOP instructions are forced as processor starts
NOP ; row erase of program memory.
BCF PMCON1,WREN ; Disable writes
BSF INTCON,GIE ; Enable interrupts
RETURN
;*******************************
;written to FLASH
;*******************************
WRITE_FLASH
BCF INTCON,GIE ;关闭全局中断
MOVLB 3 ;选择bank3
MOVF ADDRH,0 ;加载初始地址
MOVWF PMADRH
MOVF ADDRL,0
MOVWF PMADRL
MOVF LOW_DATA_ADDR,0 ;加载初始值
MOVWF FSR0L
MOVF HIGH_DATA_ADDR,0
MOVWF FSR0H
BCF PMCON1,CFGS ;配置PMCON1
BSF PMCON1,WREN ;使能写操作
BSF PMCON1,LWLO ;加载写锁存器
LOOP_FLASH
MOVIW FSR0++ ;加载低有效位第一个字节
MOVWF PMDATL ;
MOVIW FSR0++ ;加载高有效位第一个字节
MOVWF PMDATH ;
MOVF PMADRL,0 ; 检测低地址位是否为’00000‘
XORLW 0x0F ; 检查是否在16位地址的最后
ANDLW 0x0F ;
BTFSC STATUS,Z ;Exit if last of 16 words,
GOTO START_WRITE ;
MOVLW 55h ;闪存编程解锁序列
MOVWF PMCON2 ;Write 55h
MOVLW 0AAh ;
MOVWF PMCON2 ;Write AAh
BSF PMCON1,WR ;将WR置1开始写操作
NOP ;
NOP ;
INCF PMADRL,1 ;继续加载递增的锁存地址
GOTO LOOP_FLASH ;写下一个写锁存器
START_WRITE
BCF PMCON1,LWLO ; 开始写闪存
MOVLW 55h ; 闪存编程解锁序列
MOVWF PMCON2 ; Write 55h
MOVLW 0AAh ;
MOVWF PMCON2 ; Write AAh
BSF PMCON1,WR ; 将WR置1开始写操作
NOP ;
NOP ;
BCF PMCON1,WREN ; 关闭写使能
BSF INTCON,GIE ; 使能全局中断
RETURN
|