对AT24C04进行读写操作的汇编程序
INDI EQU 0X0
RTCC EQU 0X1
PC EQU 0X2
STATUS EQU 0X3
FSR EQU 0X4
PORT5 EQU 0X5
PORT6 EQU 0X6
PORT7 EQU 0X7
SCL EQU 0X6,0
SDA EQU 0X6,1
INF EQU 0X0F
YS-1 EQU 0X10
YS-2 EQU 0X11
YS-3 EQU 0X12
STACK_S EQU 0X13
STACK_A EQU 0X14
SLAVE EQU 0X15
KWRITE EQU 0X16
YWRITE EQU 0X17
BYTE EQU 0X18
SCNL EQU 0X19
RCNLE EQU 0X1A
RE-A EQU 0X1B
RE-B EQU 0X1C
RE-C EQU 0X1D
RE-D EQU 0X1E
ORG 0
JMP MAIN
ORG 8
MOV STACK_A,A
SWAP STACK_A
JBS KWRITE,2
JMP DCAK
DJZ YWIRE ;内部页写周期延时,每页10MS ,3.58M 2分频时为4次,
JMP DCAK
BS KWRITE,0
BC KWRITE,2
DCAK:
BC 0X0F,0
SWAP STACK_A
MOV A,STACK_A
RETI
WSLAVE: ;开始命令及从器件地址发送子程序
MOV A,@0B00000000
IOW PORT6
BS SDA
CALL YS2
BS SCL
CALL YS1
BC SDA
CALL YS2
BC SCL ;开始位
CALL YS1
MOV A,@8 ;从器件的地址和读写控制位。1,0,1,0,A2,A1,a8,R/W.
MOV CNSL
WSCL:
JBS SLAVE,7
JMP WRD
BS SDA
JMP WRG
WRD:
BC SDA
WRG:
BS SCL
CALL YS2
BC SDA
CALL YS1
BC SCL
RLC SLAVE
DJZ CNSL
JMP WSCL
MOV A,@0B00000010 ;设置SDA为输入,测ACK应答信号
IOW PORT6
BS SCL
CALL YS2
JBS SDA
JMP DACK
CALL YS2
JBS SDA
JMP DACK
JMP CACK
CACK:
BC SCL
BC KWRITE,1 ;测不到ACK应答信号标志位
RET
DACK:
BC SCL
BS KWRITE,1 ;测到ACK应答信号标志位
RET
WBYTE: ;字节地址的发送子程序,a7,a6,a5,a4,a3,a2,a1,a0,
MOV A,@0B00000000
IOW PORT6
BC SDA
CALL YS1
BS SCL
MOV A,@8
MOV CNSL
WSUL:
JBS BYTE,7
JMP WBD
BS SDA
JMP WBG
WBD:
BC SDA
WBG:
CALL YS2
BC SDA
CALL YS1
BC SCL
RLC BYTE
DJZ CNSL
JMP WSUL
MOV A,@0B00000010 ;设置SDA为输入,测ACK应答信号
IOW PORT6
BS SCL
CALL YS2
JBS SDA
JMP AACK
CALL YS2
JBS SDA
JMP AACK
JMP BACK
BACK:
BC SCL
BC KWRITE,1 ;测不到ACK应答信号标志位
RET
AACK:
BC SCL
BS KWRITE,1 ;测到ACK应答信号标志位
NOP
RET
WSTOP:
MOV A,@0B00000000 ;写一个停止信号
IOW PORT6
BC SDA
CALL YS2
BS SCL
CALL YS2
BS SDA
CALL YS2
BC SCL
CALL YS1
RET
YS3: ;内部页写周期延时,每页10MS ,3.58M 2分频时为4次,
MOV YWRITE,A ;内部页写周期延时计数器
BC KWRITE,0 ;内部页写周期延时结束标志位
BS KWRITE,2 ;内部页写周期延时开始标志位
MOV YWRITE,A
RET
RBYTE: ;位读子程序
MOV A,@0B00000010
MOV A,@8 ;数据寄存器为8BITS
MOV CNSL
RSCL:
RLC RCNLE
BS SCL
CALL YS2
JBS SDA
JMP RRD
BS RCNLE,0
JMP RRG
RRD:
BC RCNLE,0
RRG:
BC SCL
CALL YS2
DJZ CNSL
JMP RSCL
RET
SETACK:
MOV A,@0B00000000 ;设置SDA为输入,测ACK应答信号
IOW PORT6
BC SDA
CALL YS2
BS SCL
CALL YS2 ;设置ACK应答信号
BC SDA
RET
YS1:
MOV A,@3
MOV YS-2,A
KOPL:
NOP
DJZ YS-2
JMP KOPL
RET
YS2:
MOV A,@6
MOV YS-3,A
KOPLL:000
NOP
DJZ YS-3
JMP KOPLL
RET
MAIN:
MOV A,@0X0
IOW 0X0E
MOV A,@0B11000000
CONTW
CLR 0X0F
MOV A,@0X01
IOW 0X0F
ENI
MOV A,@0X10
MOV 0X4,A
LOPKN:
MOV A,@0
MOV 0X0,A
JBS 0X4,6
JMP KOL
JMP KIL
KOL:
NOP
INC 0X4
JMP LOPKN
KIL:
BS 0X4,6
MOV A.@0B00000000
IOW PORT5
IOW PORT6
WRITE:
JBS KWRITE,0 ;内部页写周期延时结束标志位
JMP SSOP
MOV A,@0B10100000
MOV SLAVE,A
CALL WSLAVE
JBS KWRITE,1 ;写操作子程序,AT24C04 页写缓冲器有16字节
JMP WCUCO ;连续写不能超过16次,(每次为8-BITS,)
MOV A,@0B00000000 ;内部页写周期每页要大于10mS,通信速率为100K
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP WCUCO
MOV A,@0B10101010 ;数据1次
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP WCUCO
MOV A,@0B10101010 ;数据2次
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP WCUCO
MOV A,@0B10101010 ;数据3次
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP WCUCO
MOV A,@0B10101010 ;数据4次
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP WCUCO
MOV A,@0B10101010 ;数据5次
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP WCUCO
CALL WSTOP
MOV A,@20
CALL YS3 ;调用内部页写周期延时
READ: ;读数据
JBS KWRITE,0
JMP SSOP
MOV A,@0B10100001 ;写入从器件地址
MOV SLAVE,A
CALL WSLAVE
JBS KWRITE,1
JMP RCUCO
MOV A,@0X0 ;写入读伪地址
MOV BYTE,A
CALL WBYTE
JBS KWRITE,1
JMP RCUCO
CALL WSTOP
MOV A,@0B10100001 ;写入从器件地址
MOV SLAVE,A
CALL WSLAVE
JBS KWRITE,1
JMP RCUCO
CALL RBYTE ;数据读出开始
CALL SETACK ;ACK确认
MOV A,RCNLE
MOV RE-A,A
CALL RBYTE ;数据读出开始
CALL SETACK ;ACK确认
MOV A,RCNLE
MOV RE-B,A
CALL RBYTE ;数据读出开始
CALL SETACK ;ACK确认
MOV A,RCNLE
MOV RE-C,A
CALL RBYTE ;数据读出开始
BS SCL
CALL YS2
CALL YS2 ;ON ACK
BC SCL
CALL WSTOP ;发停止位,数据读取结束
MOV A,RCNLE
MOV RE-D,A
JMP LOP
WCUCO:
JMP WRITE
RCUCO:
JMP READ
LOP:
CALL YS2
NOP
NOP
NOP
CALL YS2
NOP
NOP
NOP
JMP LPO
EOP
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。