Sơ đồ nguyên lý
CODE
SDA BIT P3.1
SCL BIT P3.0
TEMP DATA 30H
ORG 0
MAIN:
LCALL I2C_KHOITAO
QUET:
LCALL I2C_READBYTE
MOV TEMP,A
LCALL QUETLED7
SJMP QUET
I2C_KHOITAO:
LCALL I2C_START
MOV A,#0D0H
LCALL I2C_WRITE
MOV A,#00H
LCALL I2C_WRITE
MOV A,#00H
LCALL I2C_WRITE
LCALL I2C_STOP
LCALL I2C_START
MOV A,#0D0H
LCALL I2C_WRITE
MOV A,#007H
LCALL I2C_WRITE
MOV A,#10H
LCALL I2C_WRITE
LCALL I2C_STOP
RET
I2C_READBYTE:
LCALL I2C_START
MOV A,#0D0H
LCALL I2C_WRITE
MOV A,#00H
LCALL I2C_WRITE
LCALL I2C_STOP
LCALL I2C_START
MOV A,#0D1H
LCALL I2C_WRITE
LCALL I2C_READ
LCALL I2C_STOP
RET
QUETLED7:
MOV DPTR,#MALED7
MOV P0,#00
MOV A,TEMP
ANL A,#0FH
MOVC A,@A+DPTR
MOV P0,#01H
MOV P2,A
LCALL DELAY8MS
MOV P0,#00
MOV A,TEMP
SWAP A
ANL A,#0FH
MOVC A,@A+DPTR
MOV P0,#02H
MOV P2,A
LCALL DELAY8MS
RET
I2C_START:
SETB SCL
SETB SDA
NOP
CLR SDA
CLR SCL
RET
I2C_STOP:
CLR SDA
CLR SCL
SETB SCL
NOP
SETB SDA
RET
I2C_WRITE:
MOV R7,#8
RE_WRITE:
RLC A
MOV SDA,C
SETB SCL
NOP
CLR SCL
DJNZ R7,RE_WRITE
SETB SDA
SETB SCL
JB SDA,I2C_WRITE
CLR SCL
RET
I2C_READ:
MOV R7,#8
RE_READ:
SETB SDA
SETB SCL
MOV C,SDA
RLC A
CLR SCL
DJNZ R7,RE_READ
SETB SDA
SETB SCL
CLR SCL
RET
DELAY8MS:
MOV R6,#5
D1: MOV R5,#10
D2: MOV R4,#78
D3: DJNZ R4,D3
DJNZ R5,D2
DJNZ R6,D1
RET
MALED7:
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
Liện Hệ Số Điện Thoại: 01675 525 792
0 nhận xét:
Đăng nhận xét