LED1 EQU 08H
LED2 EQU 09H
LED3 EQU 0AH
LED4 EQU 0BH
LedT0 EQU 0CH ;0-3 轮流点亮4个数据管
SDA EQU P2.0
SCL EQU P2.1
Address EQU 0EH
I2CData EQU 0FH
;=================================================
ORG 0000H
LJMP START;转入主程序
;=================================================
ORG 000BH
;定时器0中断入口
INT_TO:
PUSH ACC
PUSH PSW
CLR TF0 ;清定时0中断标志
MOV TH0,#0F6H ;2.5MS
MOV TL0,#03DH ;
MOV A,#11110000B ;关数码管
ORL P2,A
MOV A,LedT0
CJNE A,#0,INT_T0_1
MOV A,LED1
MOV C A,@A+DPTR
MOV P0,A
CLR P2.7
MOV LedT0,#1
LJMP INT_T0_END
INT_T0_1:
MOV A,LedT0
CJNE A,#1,INT_T0_2
MOV A,LED2
MOV C A,@A+DPTR
MOV P0,A
CLR P2.6
MOV LedT0,#2
LJMP INT_T0_END
INT_T0_2:
MOV A,LedT0
CJNE A,#2,INT_T0_3
MOV A,LED3
MOV C A,@A+DPTR
MOV P0,A
CLR P2.5
MOV LedT0,#3
LJMP INT_T0_END
INT_T0_3:
MOV A,LED4
MOV C A,@A+DPTR
MOV P0,A
CLR P2.4
MOV LedT0,#0
LJMP INT_T0_END
INT_T0_END:
POP PSW
POP ACC
RETI
;=============================================
START:
MOV SP,#60H
CALL DELAY10MS
CALL DELAY10MS
CALL DELAY10MS
CALL
AT24C01_READ_ALL
MOV A,LED1
CJNE A,#10,$+3 ;A<10 ,C=1
JNC START_RESET_24C01
MOV A,LED2
CJNE A,#10,$+3 ;A<10 ,C=1
JNC START_RESET_24C01
MOV A,LED3
CJNE A,#10,$+3 ;A<10 ,C=1
JNC START_RESET_24C01
MOV A,LED4
CJNE A,#10,$+3 ;A<10 ,C=1
JNC START_RESET_24C01
JMP START_INC
;第一次使用复位内部数值
START_RESET_24C01:
MOV LED1,#0
MOV LED2,#0
MOV LED3,#0
MOV LED4,#0
JMP START_INC_END
;加1
START_INC:
INC LED4
MOV A,LED4
CJNE A,#10,START_INC_END ;等于10,进位
MOV LED4,#00
INC LED3
MOV A,LED3
CJNE A,#10,START_INC_END
MOV LED3,#00
INC LED2
MOV A,LED2
CJNE A,#10,START_INC_END
MOV LED2,#00
INC LED1
MOV A,LED1
CJNE A,#10,START_INC_END
MOV LED1,#00
MOV LED2,#00
MOV LED3,#00
MOV LED4,#00
START_INC_END:
;储存
CALL
AT24C01_WRITE_ALL
MOV TMOD,#01H ;设定T0为十六位定时模式。
MOV TH0,#0F6H ;2MS
MOV TL0,#03DH ;
SETB TR0 ;开定时器0
SETB ET0 ;致能定时0中断
CLR TF0 ;清定时0中断标志
SETB EA ;开总中断
MOV LedT0,#00H
MOV DPTR,#TABLE
;=============================================
SCAN:
;按键扫描
JB P3.2,$
LCALL DELAY10MS ;去抖动
JNB P3.2,KEY_RESET
LJMP SCAN
;=============================================
KEY_RESET:
MOV LED1,#00
MOV LED2,#00
MOV LED3,#00
MOV LED4,#00
;储存
CALL
AT24C01_WRITE_ALL
;检测按键是否放开
JNB P3.2,$
JMP SCAN
DELAY10MS:
MOV R4,#20
DELAY10MSA:
MOV R5,#247
DJNZ R5,$
DJNZ R4,DELAY10MSA
RET
TABLE:
DB 00101000B ;0
DB 11101011B ;1
DB 00110010B ;2
DB 10100010B ;3
DB 11100001B ;4
DB 10100100B ;5
DB 00100100B ;6
DB 11101010B ;7
DB 00100000B ;8
DB 10100000B ;9
DB 01100000B ;A
DB 00100101B ;b
DB 00111100B ;C
DB 00100011B ;d
DB 00110100B ;E
DB 01110100B ;F
DB 11110111B ;-
DB 11111111B ;
;====================================================
;读出全部数据
AT24C01_READ_ALL:
MOV Address,#00H
CALL I2C_READ
MOV LED1,A ;读出数据
MOV Address,#01H
CALL I2C_READ
MOV LED2,A ;读出数据
MOV Address,#02H
CALL I2C_READ
MOV LED3,A ;读出数据
MOV Address,#03H
CALL I2C_READ
MOV LED4,A ;读出数据
RET
;====================================================
;写入全部数据
AT24C01_WRITE_ALL:
MOV Address,#00H
MOV A,LED1
MOV I2CData,A
CALL I2C_WRITE ;写入数据
MOV Address,#01H
MOV A,LED2
MOV I2CData,A
CALL I2C_WRITE ;写入数据
MOV Address,#02H
MOV A,LED3
MOV I2CData,A
CALL I2C_WRITE ;写入数据
MOV Address,#03H
MOV A,LED4
MOV I2CData,A
CALL I2C_WRITE ;写入数据
RET
;====================================================
;写一个字节 Address地址 I2CDatata写入的数据
;====================================================
I2C_WRITE:
I2C_WRITE_A:
LCALL I2C_START
MOV A,#10100000B
LCALL I2C_SEND8BIT
LCALL I2C_ACK
JC I2C_WRITE_A ;=1,表示无确认,再次发送
MOV A,Address
LCALL I2C_SEND8BIT
LCALL I2C_ACK
MOV A,I2CData
LCALL I2C_SEND8BIT
LCALL I2C_ACK
LCALL I2C_STOP
RET
;====================================================
;读一个字节 Address地址 Data读出的数据
;====================================================
I2C_READ:
I2C_READ_A:
LCALL I2C_START
MOV A,#10100000B
LCALL I2C_SEND8BIT
LCALL I2C_ACK
JC I2C_READ_A ;=1,表示无确认,再次发送
MOV A,Address
LCALL I2C_SEND8BIT
LCALL I2C_ACK
I2C_READ_B:
LCALL I2C_START
MOV A,#10100001B
LCALL I2C_SEND8BIT
LCALL I2C_ACK
JC I2C_READ_B
LCALL I2C_RECEIVE8BIT
MOV I2CData,A
LCALL I2C_ACK
LCALL I2C_STOP
RET
;发送开始信号
I2C_START:
SETB SCL
SETB SDA
NOP
NOP
CLR SDA
NOP
NOP
CLR SCL
RET
;发送结束信号
I2C_STOP:
CLR SDA
NOP
NOP
SETB SCL
NOP
NOP
SETB SDA
RET
;发送接收确认信号
I2C_ACk:
SETB SDA
SETB SCL
NOP
NOP
JB SDA,I2C_ACK0
CLR C
SJMP I2C_ACK_END
I2C_ACK0:
SETB C
I2C_ACK_END:
CLR SCL
RET
;送八位数据
I2C_SEND8BIT:
MOV B,#08H
I2C_SEND8BIT_A:
RLC A
MOV SDA,C
SETB SCL
NOP
NOP
CLR SCL
DJNZ B,I2C_SEND8BIT_A
RET
;接收八位数据
I2C_RECEIVE8BIT:
MOV B,#08H
CLR A
SETB SDA
I2C_RECEIVE8IT_A:
SETB SCL
NOP
NOP
MOV C,SDA
RLC A
CLR SCL
DJNZ B,I2C_RECEIVE8IT_A
RET
END