include HT46F49E.inc
_CS EQU IAR1.4 ;EEPROM lines setup to have corresponding
_SK EQU IAR1.5 ;Bit in the Indirect Addressing Register IAR1
_DI EQU IAR1.6 ;EEPROM can only be indirectly addressed using MP1
_DO EQU IAR1.7 ;
_EECR EQU 40H
C_Addr_Length EQU 8 ;Address length-8bits
C_Data_Length EQU 8 ;Data length-always 8bits
ds .section AT 70H 'data'
EE_command DB ? ;Stores the read or write instruction information
ADDR DB ? ;Store Write data or read data address
WR_Data DB ? ;Store read or write data
COUNT DB ? ;Temporary counter
TIAOSHI DB ?
TIAOSHI1 DB ? ;原EEPROM数据
TIAOSHI2 DB ? ;写入后数据
TIAOSHI3 DB ? ;擦除后数据
TIAOSHI4 DB ? ;察看READ是否有效
cs .section 'code'
_main:
MOV A,0
MOV EE_command,A
MOV ADDR,A
MOV WR_DATA,A
MOV COUNT,A
MOV TIAOSHI,A
MOV TIAOSHI1,A
MOV TIAOSHI2,A
MOV TIAOSHI3,A
MOV TIAOSHI4,A
MOV A,11000000b ;原EEPROM数据
MOV EE_command,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
CALL READ
MOV A,TIAOSHI
MOV TIAOSHI1,A
MOV A,98H ;使能擦除
MOV EE_command,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
call EWEN
MOV A,10100000B ;向地址0写入aa
MOV EE_command,A
MOV A,10101010B
MOV WR_DATA,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
CALL WRITE
MOV A,11000000b ;读出写入地址0的数据
MOV EE_command,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
CALL READ
MOV A,TIAOSHI
MOV TIAOSHI2,A
mov A,90h ;擦除所有EEPROM
mov EE_command,A
mov A,12
mov WR_Data,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
set _CS
call ERAL
MOV A,11000000b ;读出擦除后的地址0数据
MOV EE_command,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
CALL READ
MOV A,TIAOSHI
MOV TIAOSHI3,A
MOV A,10100000B ;向地址0写入bb
MOV EE_command,A
MOV A,10111011B
MOV WR_DATA,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
CALL WRITE
MOV A,11000000b ;读出写入地址0的数据
MOV EE_command,A
MOV A,01H
MOV BP,A
mov A,_EECR
MOV MP1,A
CLR _CS
SET _CS
CALL READ
MOV A,TIAOSHI
MOV TIAOSHI4,A
jmp $
EWEN:
MOV A,12 ;Read,Write and Erase instruction are 3bits long
MOV COUNT,A
EWEN_0:
CLR _DI ;Prepare the transmitted bit
SZ EE_command.7 ;Check value of highest instruction code bit
SET _DI
SET _SK
CLR _SK
CLR C
RLC EE_command ;Get next bit of instruction code
SDZ COUNT ;Check if last bit has been transmitted
JMP EWEN_0
;CLR _CS
CLR _DI
RET
ERAL: ;Write instruction code subroutine
MOV A,12 ;Read,Write and Erase instruction are 3bits long
MOV COUNT,A
ERAL_0:
CLR _DI ;Prepare the transmitted bit
SZ EE_command.7 ;Check value of highest instruction code bit
SET _DI
SET _SK
CLR _SK
CLR C
RLC EE_command ;Get next bit of instruction code
SDZ COUNT ;Check if last bit has been transmitted
JMP ERAL_0
CLR _CS
SET _CS
SNZ _DO
JMP $-1
CLR _CS
RET
READ:
WriteCommand: ;Write instruction code subroutine
MOV A,12 ;Read,Write and Erase instruction are 3bits long
MOV COUNT,A
WriteCommand_0:
CLR _DI ;Prepare the transmitted bit
SZ EE_command.7 ;Check value of highest instruction code bit
SET _DI
SET _SK
CLR _SK
CLR C
RLC EE_command ;Get next bit of instruction code
SDZ COUNT ;Check if last bit has been transmitted
JMP WriteCommand_0
CLR _DI
ReadData:
MOV A,C_Data_Length ;Setup data length
MOV COUNT,A
CLR WR_Data
ReadData_0:
CLR C
RLC WR_Data
SET _SK
SZ _DO ;Check value of data MSB
SET WR_Data.0
CLR _SK
SDZ COUNT ;Check if lsb has been received
JMP ReadData_0
;CLR _CS
MOV A,WR_Data
MOV TIAOSHI,A
RET
WRITE: ;Write instruction code subroutine
MOV A,12 ;Read,Write and Erase instruction are 3bits long
MOV COUNT,A
WriteCommand_1:
CLR _DI ;Prepare the transmitted bit
SZ EE_command.7 ;Check value of highest instruction code bit
SET _DI
SET _SK
CLR _SK
CLR C
RLC EE_command ;Get next bit of instruction code
SDZ COUNT ;Check if last bit has been transmitted
JMP WriteCommand_1
CLR _DI
WriteData:
MOV A,C_Data_Length ;Setup data length
MOV COUNT,A
WriteData_0:
CLR _DI
SZ WR_Data.7 ;Check value of data MSB
SET _DI
CLR C
RLC WR_Data ;Get next address bit
SET _SK
CLR _SK
SDZ COUNT ;Check if data lsb has been written
JMP WriteData_0
CLR _CS ;CS low edge initiates internal write cycle
SET _CS ;CS high edge allows DO to be used to indicate
;end of write cycle
SNZ _DO ;Poll for DO high to indicate end of write cycle
JMP $-1
RET