;

;		P_CLKIIC 	EQU	P1.2
; P_DATIIC EQU P1.3
; PM_DATIIC EQU P1M.3 ; EE_ADDR DS 1 ;地址寄存器
; TMP3_IIC DS 1
; TMP2_IIC DS 1
; TMP1_IIC DS 1
; DATA_TEMP DS 1 ;数据缓存
; R_DATA DS 1 ;读数据寄存器
; W_DATA DS 1 ;写数据寄存器 ;*************************************************
;功能:读出一认定地址的数据
;INPUT: EE_ADDR_H , EE_ADDR_L --> EE_ADDR
;OUTPUT: R_DATA
;影响参数:DATA_TEMP ,TMP1_IIC ,TMP2_IIC ,TMP3_IIC,TMP4_IIC
;======================================
READ_EEPROM:
MOV A,#200
MOV TMP2_IIC,A
READ_EEPROM_LOOP0:
;------------
B0BSET PM_DATIIC ;SET OUT MODEL
CALL STAR_CONDITION
MOV A,#10100000B
B0MOV DATA_TEMP,A
CALL SEND_DATA_ADD ;写模式,写入高3位地址
B0BCLR PM_DATIIC ;IN MODEL
B0BSET P_CLKIIC
B0BTS1 P_DATIIC
JMP READ_EEPROM_LOOP1
DECMS TMP2_IIC
JMP READ_EEPROM_LOOP0
BSET F_ERRIIC
READ_EEPROM_LOOP1:
B0BCLR P_CLKIIC
B0BSET PM_DATIIC ;OUT MODE
MOV A,EE_ADDR
MOV DATA_TEMP,A
CALL SEND_DATA_ADD ;写入低8位地址
B0BCLR PM_DATIIC ;IN MODEL
B0BSET P_CLKIIC
MOV A,#200
MOV TMP2_IIC,A
READ_EEPROM_LOOP2:
B0BTS1 P_DATIIC
JMP READ_EEPROM_LOOP3
DECMS TMP2_IIC
JMP READ_EEPROM_LOOP2
BSET F_ERRIIC
READ_EEPROM_LOOP3:
B0BCLR P_CLKIIC
B0BSET PM_DATIIC ;OUT MODEL
CALL STAR_CONDITION
MOV A,#10100001B
MOV DATA_TEMP,A
CALL SEND_DATA_ADD ;读出数据
B0BCLR PM_DATIIC ;IN MODEL
B0BSET P_CLKIIC
MOV A,#200
MOV TMP2_IIC,A
READ_EEPROM_LOOP4:
B0BTS1 P_DATIIC
JMP READ_EEPROM_LOOP5
DECMS TMP2_IIC
JMP READ_EEPROM_LOOP4
BSET F_ERRIIC
READ_EEPROM_LOOP5:
B0BCLR P_CLKIIC
CALL OUT_DATA_X
B0BSET PM_DATIIC ;OUT MODEL
CALL STOP_CONDITION
B0BCLR PM_DATIIC ;IN MODEL
;-------------
MOV A,DATA_TEMP
MOV R_DATA,A ;8.28
RET
;************************************************************************
;功能:定入一指定地址的数据
;INPUT: EE_ADDR_H , EE_ADDR_L ,W_DATA
;OUTPUT: F_EER ,24写入有错
;影响参数:DATA_TEMP ,TMP1_IIC ,TMP2_IIC ,TMP3_IIC
;===================================================
WRITE_EEPROM:
;先读出,与写入相同则不写 --> 如果读出的数与要写的数一致则不用再写 CALL READ_EEPROM ; MOV A,R_DATA
XOR A,W_DATA
BTS0 FZ
RET ;=================================
MOV A,#3 ;如果写数据失败则重复写 最多写3次
MOV TMP3_IIC,A
;------------
WRITE_EEPROM00:
B0BSET PM_DATIIC ;SET P1.3 OUT MODEL
CALL STAR_CONDITION
MOV A,#10100000B ;写入高3位地址
MOV DATA_TEMP,A
CALL SEND_DATA_ADD ;DEVICE_ADDRESS
B0BCLR PM_DATIIC ;IN MODEL
B0BSET P_CLKIIC
MOV A,#200
MOV TMP2_IIC,A
WRITE_EEPROM_LOOP0:
B0BTS1 P_DATIIC
JMP WRITE_EEPROM_LOOP1
DECMS TMP2_IIC
JMP WRITE_EEPROM_LOOP0
BSET F_ERRIIC
WRITE_EEPROM_LOOP1:
B0BCLR P_CLKIIC
B0BSET PM_DATIIC ;OUT
MOV A,EE_ADDR
MOV DATA_TEMP,A
CALL SEND_DATA_ADD ;写入低8位地址
B0BCLR PM_DATIIC ;IN MODEL
B0BSET P_CLKIIC
MOV A,#200
MOV TMP2_IIC,A
WRITE_EEPROM_LOOP2:
B0BTS1 P_DATIIC
JMP WRITE_EEPROM_LOOP3
DECMS TMP2_IIC
JMP WRITE_EEPROM_LOOP2
BSET F_ERRIIC
WRITE_EEPROM_LOOP3:
B0BCLR P_CLKIIC
B0BSET PM_DATIIC ;OUT
MOV A,W_DATA
MOV DATA_TEMP,A
CALL SEND_DATA_ADD ;写入的数据
B0BCLR P_DATIIC;
B0BCLR PM_DATIIC ;IN MODEL
B0BSET P_CLKIIC
MOV A,#200
MOV TMP2_IIC,A
WRITE_EEPROM_LOOP4:
B0BTS1 P_DATIIC
JMP WRITE_EEPROM_LOOP5
DECMS TMP2_IIC
JMP WRITE_EEPROM_LOOP4
BSET F_ERRIIC
WRITE_EEPROM_LOOP5:
B0BCLR P_CLKIIC
B0BSET PM_DATIIC ;OUT
B0BSET P_DATIIC
JMP $+1
B0BCLR P_DATIIC
CALL STOP_CONDITION
CALL STOP_CONDITION
B0BCLR PM_DATIIC ;IN MODEL
;---------------
;读出比较是否与定入的一致
CALL READ_EEPROM
MOV A,R_DATA
CMPRS A,W_DATA
RET
DECMS TMP3_IIC
JMP WRITE_EEPROM00
BSET F_EEPOM_W
RET
;----------------
OUT_DATA_X:
CLR DATA_TEMP
MOV A,#8H
MOV TMP1_IIC,A
OUT_LOOP1:
B0BCLR PFLAG.2
B0BTS0 P_DATIIC
B0BSET PFLAG.2
;=========================== JMP $+1
JMP $+1
B0BSET P_CLKIIC
JMP $+1
JMP $+1
B0BCLR P_CLKIIC
;==========================
RLCM DATA_TEMP
DECMS TMP1_IIC
JMP OUT_LOOP1
RET
;----------------
SEND_DATA_ADD: ;
MOV A,#8H
MOV TMP1_IIC,A
SEND_LOOP1:
RLCM DATA_TEMP
B0BCLR P_DATIIC
B0BTS0 PFLAG.2
B0BSET P_DATIIC
;===========================
JMP $+1
JMP $+1
B0BSET P_CLKIIC
JMP $+1
JMP $+1
B0BCLR P_CLKIIC
;==========================
DECMS TMP1_IIC
JMP SEND_LOOP1
RLCM DATA_TEMP
RET
;---------
POSITIVE_EDGE: ;起始信号
JMP $+1
JMP $+1
B0BSET P_CLKIIC
JMP $+1
JMP $+1
B0BCLR P_CLKIIC
RET
;----------
STAR_CONDITION: ;结束信号
B0BSET P_CLKIIC
B0BSET P_DATIIC
JMP $+1
JMP $+1
B0BCLR P_DATIIC
JMP $+1
JMP $+1
B0BCLR P_CLKIIC
RET
;------------
STOP_CONDITION: ;
B0BCLR P_DATIIC
B0BSET P_CLKIIC
JMP $+1
JMP $+1
B0BSET P_DATIIC
JMP $+1
JMP $+1
B0BCLR P_CLKIIC
B0BCLR P_DATIIC
RET
04-25 08:40