scl bit p1.6
sda bit p1.7
org 00h
sjmp star
org 30h
star:
mov dptr, #tab
mov r3, #0
main: acall churu
acall delay1
acall duchu
ljmp main
;/////////////////////////////////
churu:
acall start ;开始
mov a, #0a0h
acall fusongzi ;发送
acall jieshouying ;接收应答
jc next
mov a, #00h
acall fusongzi
acall jieshouying
jc next
mov a, #0x99
acall fusongzi
acall jieshouying
jc next
acall delay
next: acall tingzhi ;停止
ret
;///////////////////////////////////////
duchu:
acall start
mov a, #0a0h
acall fusongzi
acall jieshouying
jc next1
mov a, #00h
acall fusongzi
acall jieshouying
jc next1
acall start
mov a, #0a1h
acall fusongzi
acall jieshouying
jc next1
acall jieshouzi
mov p2, a
acall yingda
next1: acall tingzhi
ret
;/////////////起始信号/////////////////
start:
setb scl
setb sda
acall delay
clr sda
acall delay
clr scl
acall delay
ret
;///////////////终止信号//////////////////////
tingzhi:
clr scl
clr sda
acall delay
setb scl
acall delay
setb sda
acall delay
clr sda
acall delay
ret
;///////////////发送应答信号//////////////////////////
yingda:
clr sda
setb scl
acall delay
clr scl
setb sda
acall delay
ret
;/////////////////接收应答信号///////////////////
jieshouying:
setb sda
setb scl
acall delay
mov c, sda
acall delay
clr scl
acall delay
ret
;///////////////非应答信号/////////////////////
feiyingda:
setb sda
setb scl
acall delay
clr scl
acall delay
clr sda
ret
;////////////////发送应答////////////////////
fuying:
clr sda
setb scl
acall delay
clr scl
setb sda
acall delay
ret
;///////////////发送一个字节/////////////
fusongzi:
mov r1, #08h
ll:
rlc a
mov sda, c
setb scl
acall delay
clr scl
acall delay
djnz r1, ll
ret
;/////////////接发一个字节//////////////
jieshouzi:
mov r1, #08h
setb sda
ll1:
setb scl
acall delay
mov c, sda
rlc a
acall delay
clr scl
acall delay
djnz r1, ll1
ret
;////////////////延时////////////////////////
delay:
nop
nop
nop
nop
nop
nop
nop
nop
ret
delay1:
mov r6, #200
kk: mov r7, #200
djnz r7, $
djnz r6, kk
ret
delay2:
mov r5, #10
kk2: mov r6, #100
kk3: mov r7, #100
djnz r7, $
djnz r6, kk3
djnz r5, kk2
ret
tab:
db 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82
db 0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
;///////////////////////////////////
end
; 读出两个数 |