;*******************************************************
;单    位:苏州华芯微电子有限公司                       * 
;                                                      *
;文件名称:											   *
;摘    要:  HS16F3211 E2 读写                                           *
;                                                      *
;当期版本:	V1.0                                         *
;作    者:   WSG                            *
;开始日期: 2021.12.4                                  *
;完成日期:                                             *
;                                                      *
;先前版本:                                             *	
;作    者:                                             *
;创始日期:                                             *
;*******************************************************



A	EQU		0
R	EQU		1


E2_TEST		EQU	10H
E2_TEST_ADDR		EQU	11H
TEST_CNT		EQU	12H


;EEPROM	BANK2*******************************************
EECON1		EQU	10H
;EEADRH		EQU	7BH
EEADRL		EQU	0DH
EEDATL		EQU	0EH
;FLASH	BANK3*******************************************
FLCON1		EQU	0EH
FLCON2		EQU	0FH
FLADRH		EQU	10H
FLADRL		EQU	11H
FLDATH		EQU	12H
FLDATL		EQU	13H
FL_eerf		EQU	14H
;***************************************************BANK0
INDF		EQU	00H
TMR0		EQU	01H
PCL			EQU	02H
STATUS		EQU	03H
FSR			EQU	04H
PORTA		EQU	05H
PORTB		EQU	06H
TC0L		EQU	11H
TC0M		EQU	12H
TC0R		EQU	13H
TC0H		EQU	14H
TC0D1		EQU	15H
TC0D2		EQU	16H
PWMCON		EQU	17H
;****************************************************BANK1
OPTION_REG		EQU	01H
TRISA		EQU		05H
TRISB		EQU		06H
IOSTA		EQU		05H
IOSTB		EQU		06H
;*****************************************************
.CODE
	ORG		000H
	NOP
	NOP
	GOTO	INITIAL
INITIAL:
	MOVIA		00H
	MOVAR		PORTA
	MOVAR		PORTB
	BSR			STATUS,5
	MOVIA		000H
	IOST		IOSTA
	IOST		IOSTB
	OPTION
	
	
	CALL	ERS_ALL_EE
;****************************************************

MAIN:
	BCR			STATUS,5
	BCR			STATUS,6		;BANK0
	
	INCR	TEST_CNT,R 
	BTRSS	TEST_CNT,7 		;最大128个
	GOTO	TEST_EE_LOOP
	CLRR	TEST_CNT
	CALL	ERS_ALL_EE
	
TEST_EE_LOOP:	
	MOVR	TEST_CNT,A 
	MOVAR	E2_TEST 
	MOVAR	E2_TEST_ADDR
	CALL	WRITE_EE	
	
	CLRR	E2_TEST
	CALL	READ_EE
	MOVAR	E2_TEST
	
	MOVIA	0X5A 
	XORAR	E2_TEST,A 
	BTRSS	STATUS,3		
	GOTO	TEST_EE_ERR 
	
	MOVIA	0X01 
	XORAR	PORTB,R 

TEST_EE_ERR:
	GOTO	MAIN
	
;==================================================
;e2
;==================================================
ERS_ALL_EE:
;擦除E2
	BCR			STATUS,5
	BSR			STATUS,6		;BANK2	

	CLRR		EEADRL
	BSR			EECON1,3		;ERSALLEE		全擦
	BSR			EECON1,2		;ERASEEEE		字擦
WAIT_EE_ERASEALL:
	BTRSC		EECON1,2
	GOTO		WAIT_EE_ERASEALL	;判断擦除成功
	NOP
	BCR			EECON1,3	
	
	BCR			STATUS,5
	BCR			STATUS,6		;BANK0
	
	RETURN 

	
	
	
WRITE_EE:
;写E2
	BCR			STATUS,5
	BSR			STATUS,6		;BANK2	
	
	CLRR		EEADRL			;00H

	BCR			STATUS,5
	BCR			STATUS,6		;BANK0
	MOVR		E2_TEST,A 		;写数据
	BCR			STATUS,5
	BSR			STATUS,6		;BANK2	
	MOVAR		EEDATL
		
	
	BCR			STATUS,5
	BCR			STATUS,6		;BANK0
	MOVR		E2_TEST_ADDR,A		;写地址
	BCR			STATUS,5
	BSR			STATUS,6		;BANK2	
    MOVAR		EEADRL
        
	BSR			EECON1,1		;PROGEE 字写
WAIT_WRITE_FIRST:
	BTRSC		EECON1,1
	GOTO		WAIT_WRITE_FIRST	;判断写成功
	NOP 
	
	BCR			STATUS,5
	BCR			STATUS,6		;BANK0
	RETURN 
	
	
	
READ_EE:
;读E2
	BCR			STATUS,5
	BSR			STATUS,6		;BANK2	
	
	CLRR		EEADRL			;00H
	CLRR		EEDATL
	BSR			EECON1,0		;使能读EE
	NOP
	MOVR		EEADRL,A 
	BCR			STATUS,5
	BCR			STATUS,6		;BANK0
	;MOVAR		E2_TEST
	RETURN 
	
	
	
	
	