24c02开机计数器程序
扫描二维码
随时随地手机看文章
LED1EQU08H
LED2EQU09H
LED3EQU0AH
LED4EQU0BH
LedT0EQU0CH;0-3轮流点亮4个数据管
SDAEQUP2.0
SCLEQUP2.1
AddressEQU0EH
I2CDataEQU0FH
;=================================================
ORG0000H
LJMPSTART;转入主程序
;=================================================
ORG000BH
;定时器0中断入口
INT_TO:
PUSHACC
PUSHPSW
CLRTF0;清定时0中断标志
MOV TH0,#0F6H;2.5MS
MOV TL0,#03DH;
MOV A,#11110000B;关数码管
ORLP2,A
MOV A,LedT0
CJNEA,#0,INT_T0_1
MOV A,LED1
MOV CA,@A+DPTR
MOV P0,A
CLRP2.7
MOV LedT0,#1
LJMPINT_T0_END
INT_T0_1:
MOV A,LedT0
CJNEA,#1,INT_T0_2
MOV A,LED2
MOV CA,@A+DPTR
MOV P0,A
CLRP2.6
MOV LedT0,#2
LJMPINT_T0_END
INT_T0_2:
MOV A,LedT0
CJNEA,#2,INT_T0_3
MOV A,LED3
MOV CA,@A+DPTR
MOV P0,A
CLRP2.5
MOV LedT0,#3
LJMPINT_T0_END
INT_T0_3:
MOV A,LED4
MOV CA,@A+DPTR
MOV P0,A
CLRP2.4
MOV LedT0,#0
LJMPINT_T0_END
INT_T0_END:
POPPSW
POPACC
RETI
;=============================================
START:
MOV SP,#60H
CALLDELAY10MS
CALLDELAY10MS
CALLDELAY10MS
CALLAT24C01_READ_ALL
MOV A,LED1
CJNEA,#10,$+3;A<10,C=1
JNCSTART_RESET_24C01
MOV A,LED2
CJNEA,#10,$+3;A<10,C=1
JNCSTART_RESET_24C01
MOV A,LED3
CJNEA,#10,$+3;A<10,C=1
JNCSTART_RESET_24C01
MOV A,LED4
CJNEA,#10,$+3;A<10,C=1
JNCSTART_RESET_24C01
JMPSTART_INC
;第一次使用复位内部数值
START_RESET_24C01:
MOV LED1,#0
MOV LED2,#0
MOV LED3,#0
MOV LED4,#0
JMPSTART_INC_END
;加1
START_INC:
INCLED4
MOV A,LED4
CJNEA,#10,START_INC_END;等于10,进位
MOV LED4,#00
INCLED3
MOV A,LED3
CJNEA,#10,START_INC_END
MOV LED3,#00
INCLED2
MOV A,LED2
CJNEA,#10,START_INC_END
MOV LED2,#00
INCLED1
MOV A,LED1
CJNEA,#10,START_INC_END
MOV LED1,#00
MOV LED2,#00
MOV LED3,#00
MOV LED4,#00
START_INC_END:
;储存
CALLAT24C01_WRITE_ALL
MOV TMOD,#01H;设定T0为十六位定时模式。
MOV TH0,#0F6H;2MS
MOV TL0,#03DH;
SETBTR0;开定时器0
SETBET0;致能定时0中断
CLRTF0;清定时0中断标志
SETBEA;开总中断
MOV LedT0,#00H
MOV DPTR,#TABLE
;=============================================
SCAN:
;按键扫描
JBP3.2,$
LCALLDELAY10MS;去抖动
JNBP3.2,KEY_RESET
LJMPSCAN
;=============================================
KEY_RESET:
MOV LED1,#00
MOV LED2,#00
MOV LED3,#00
MOV LED4,#00
;储存
CALLAT24C01_WRITE_ALL
;检测按键是否放开
JNBP3.2,$
JMPSCAN
DELAY10MS:
MOV R4,#20
DELAY10MSA:
MOV R5,#247
DJNZR5,$
DJNZR4,DELAY10MSA
RET
TABLE:
DB00101000B;0
DB11101011B;1
DB00110010B;2
DB10100010B;3
DB11100001B;4
DB10100100B;5
DB00100100B;6
DB11101010B;7
DB00100000B;8
DB10100000B;9
DB01100000B;A
DB00100101B;b
DB00111100B;C
DB00100011B;d
DB00110100B;E
DB01110100B;F
DB11110111B;-
DB11111111B;
;====================================================
;读出全部数据
AT24C01_READ_ALL:
MOV Address,#00H
CALLI2C_READ
MOV LED1,A;读出数据
MOV Address,#01H
CALLI2C_READ
MOV LED2,A;读出数据
MOV Address,#02H
CALLI2C_READ
MOV LED3,A;读出数据
MOV Address,#03H
CALLI2C_READ
MOV LED4,A;读出数据
RET
;====================================================
;写入全部数据
AT24C01_WRITE_ALL:
MOV Address,#00H
MOV A,LED1
MOV I2CData,A
CALLI2C_WRITE;写入数据
MOV Address,#01H
MOV A,LED2
MOV I2CData,A
CALLI2C_WRITE;写入数据
MOV Address,#02H
MOV A,LED3
MOV I2CData,A
CALLI2C_WRITE;写入数据
MOV Address,#03H
MOV A,LED4
MOV I2CData,A
CALLI2C_WRITE;写入数据
RET
;====================================================
;写一个字节Address地址I2CDatata写入的数据
;====================================================
I2C_WRITE:
I2C_WRITE_A:
LCALLI2C_START
MOV A,#10100000B
LCALLI2C_SEND8BIT
LCALLI2C_ACK
JCI2C_WRITE_A;=1,表示无确认,再次发送
MOV A,Address
LCALLI2C_SEND8BIT
LCALLI2C_ACK
MOV A,I2CData
LCALLI2C_SEND8BIT
LCALLI2C_ACK
LCALLI2C_STOP
RET
;====================================================
;读一个字节Address地址Data读出的数据
;====================================================
I2C_READ:
I2C_READ_A:
LCALLI2C_START
MOV A,#10100000B
LCALLI2C_SEND8BIT
LCALLI2C_ACK
JCI2C_READ_A;=1,表示无确认,再次发送
MOV A,Address
LCALLI2C_SEND8BIT
LCALLI2C_ACK
I2C_READ_B:
LCALLI2C_START
MOV A,#10100001B
LCALLI2C_SEND8BIT
LCALLI2C_ACK
JCI2C_READ_B
LCALLI2C_RECEIVE8BIT
MOV I2CData,A
LCALLI2C_ACK
LCALLI2C_STOP
RET
;发送开始信号
I2C_START:
SETBSCL
SETBSDA
NOP
NOP
CLRSDA
NOP
NOP
CLRSCL
RET
;发送结束信号
I2C_STOP:
CLRSDA
NOP
NOP
SETBSCL
NOP
NOP
SETBSDA
RET
;发送接收确认信号
I2C_ACk:
SETBSDA
SETBSCL
NOP
NOP
JBSDA,I2C_ACK0
CLRC
SJMPI2C_ACK_END
I2C_ACK0:
SETBC
I2C_ACK_END:
CLRSCL
RET
;送八位数据
I2C_SEND8BIT:
MOV B,#08H
I2C_SEND8BIT_A:
RLCA
MOV SDA,C
SETBSCL
NOP
NOP
CLRSCL
DJNZB,I2C_SEND8BIT_A
RET
;接收八位数据
I2C_RECEIVE8BIT:
MOV B,#08H
CLRA
SETBSDA
I2C_RECEIVE8IT_A:
SETBSCL
NOP
NOP
MOV C,SDA
RLCA
CLRSCL
DJNZB,I2C_RECEIVE8IT_A
RET
END