i2cr.txt
来自「I2C protocal read program」· 文本 代码 · 共 137 行
TXT
137 行
;------------------------------------------------
; I2C Bus Byte Write sample program
; Master 80c51
; Assembler 2500 A.D. 4.03a
; slave Atmel A24c16
; by Eddie W.Y. Liang 25-Sep-98
;------------------------------------------------
SCL .REG 90H.0 ;P1.0
SDA .REG 90H.1 ;P1.1
ACK .REG 22H.0 ;0=ACK 1=Not ACK
A24C16W .EQU 10100000B ;1010-->device address
;bit3 to bit1-->page address
;bit0 -->0=Write
A24C16R .EQU 10100001B ;1010-->device address
;bit3 to bit1-->page address
;bit0 -->1=Read
BIT_COUNT .EQU 21H ;bit counter address
DELAY_2 .MACRO
NOP
NOP
.MACEND
DELAY_3 .MACRO
NOP
NOP
NOP
.MACEND
DELAY_4 .MACRO
NOP
NOP
NOP
NOP
.MACEND
.ORG 0000H
SETB ACK
ACALL START
MOV A,#A24C16W ;
ACALL SEND_MSG ;send 24c16 device address and write
;actually, it's not "write". it is "dummy".
DELAY_4
MOV A,#00000000B
ACALL SEND_MSG ;send 24c16 data address
SETB SDA
SETB SCL
DELAY_4
ACALL START
MOV A,#A24C16R ;
ACALL SEND_MSG ;send 24c16 device address and read
DELAY_4
SETB SDA
ACALL READ_MSG ;send 24c16 data address
ACALL STOP
RET
READ_MSG
MOV BIT_COUNT,#8
RM_LOOP
SETB SCL
DELAY_3
MOV C,SDA
RLC A
CLR SCL
DELAY_3
DJNZ BIT_COUNT,RM_LOOP
DELAY_2
SETB SDA
DELAY_3
SETB SCL
DELAY_4
CLR SCL
DELAY_3
RET
SEND_MSG ;YOU HAVE TO PUT MESSAGE INTO A REGISTER
MOV BIT_COUNT,#8
SM_LOOP
RLC A
MOV SDA,C ;send 1 bit
SETB SCL
DELAY_3
CLR SCL
DELAY_3
DJNZ BIT_COUNT,SM_LOOP
SETB SDA
SETB SCL
DELAY_3
JNB SDA,NO_ACK ; if SDA =0 then Ack=0 acknowledge enable
CLR ACK
NO_ACK:
CLR SCL
DELAY_3
DELAY_4
CLR SDA
DELAY_4
RET
START:
SETB SDA
SETB SCL
DELAY_2
CLR SDA
DELAY_3
CLR SCL
DELAY_3
RET
STOP:
CLR SDA
SETB SCL
DELAY_4
SETB SDA
DELAY_3
RET
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?