实时时钟芯片DS1302设计程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
IO EQU P1.0
SCLK EQU P1.1
RST EQU P1.2
RW BIT P1.6
RS BIT P1.7
E BIT P1.5
KEY0 BIT P3.2
KEY1 BIT P3.3
KEY2 BIT P3.5
KEY3 BIT P3.7
ORG 0000H
AJMP MAIN
ORG 000BH
AJMP T0INT
ORG 0100H
MAIN: ACALL LCDINIT ;对LCD进行初始化
LOP5:
MOV 30H,#8 ;以下为将DS1302的数据存到单片机的40H到46H中MOV R0,#40H
MOV R7,#7
MOV R6,#81H
LOP:
CLR P1.2
CLR P1.1
SETB P1.2
MOV B,R6
ACALL WRITE
ACALL READ
MOV @R0,A
INC R0
INC R6
INC R6
DJNZ R7,LOP ;读出DS1302中的数据存到单片机中
MOV 30H, 40H ;秒
MOV 31H, 41H ;分
MOV 32H, #00H ;时缓存
MOV 33H, 42H ;时
MOV R4, #00H ;控制显示模式
MOV R3, #00H ;记录控制信号
MOV TMOD, #01H
MOV TL0, #0E0H
MOV TH0, #0B1H
MOV IE, #10000010B
MOV R2, #32H
SETB TR0
MOV A, #83H ;DDRAM的地址进行设置,选择从哪里开始显示
ACALL WRC
MOV A, #54H ;T的ASCII码
ACALL WRD
MOV A, #49H ;I的ASCII码
ACALL WRD
MOV A, #4DH ;M的ASCII码
ACALL WRD
MOV A, #45H ;E的ASCII码
ACALL WRD
MOV A, #3AH ;:的ASCII码
ACALL WRD
MOV A,#0C3H ;对DDRAM地址进行设置CALL WRC
MOV A,#58H ;X的ASCII码
CALL WRD
MOV A,#51H ;Q的ASCII码
CALL WRD
DEC 45H
MOV A,45H ;显示星期
ANL A,#0FH
ADD A,#30H
ACALL WRD
MOV A,#3AH ;:的ASCII码
CALL WRD
MOV A,#10H ; 的ASCII码
CALL WRD
MOV A,46H ;显示年
ANL A,#0F0H
SWAP A
ADD A,#30H
ACALL WRD
MOV A,46H
ANL A,#0FH
ADD A,#30H
ACALL WRD
MOV A,#0CAH ;对DDRAM地址进行设置CALL WRC
MOV A,#2DH ;-的ASCII码
CALL WRD
MOV A,44H ;显示月
ANL A,#0F0H
SWAP A
ADD A,#30H
ACALL WRD
MOV A,44H
ANL A,#0FH
ADD A,#30H
ACALL WRD
MOV A,#0CDH ;对DDRAM地址进行设置CALL WRC
MOV A,#2DH ;-的ASCII码
CALL WRD
MOV A,43H ;显示日
ANL A,#0F0H
SWAP A
ADD A,#30H
ACALL WRD
MOV A,43H
ANL A,#0FH
ADD A,#30H
ACALL WRD
DONE: ACALL DISPLAY
MOV A,#097H ;DDRAM的地址进行设置,选择从哪里开始显示CALL WRC
MOV A,#31H ;1
CALL WRD
MOV A,#31H ;1
CALL WRD
MOV A,#30H ;0
CALL WRD
MOV A,#32H ;2
CALL WRD
MOV A,#3AH ;:
CALL WRD
MOV A,#48H ;H的ASCII码
CALL WRD
MOV A,#41H ;A的ASCII码
CALL WRD
MOV A,#4EH ;N的ASCII码
CALL WRD
MOV A,#10H ; 的ASCII码
CALL WRD
MOV A,#58H ;X的ASCII码
CALL WRD
MOV A,#49H ; I 的ASCII码
CALL WRD
MOV A,#41H ; A 的ASCII码
CALL WRD
MOV A,#4FH ;O的ASCII码
CALL WRD
MOV A,#10H ; 的ASCII码
CALL WRD
MOV A,#4EH ;N的ASCII码
CALL WRD
MOV A,#41H ; A 的ASCII码
CALL WRD
MOV A,#4EH ;N的ASCII码
CALL WRD
ACALL KEY
AJMP DONE
LCDINIT:MOV A, #01H
ACALL WRC
MOV A, #38H
ACALL WRC
MOV A, #0CH
ACALL WRC
MOV A, #06H
ACALL WRC
RET ;对液晶显示器初始功能设置
DISPLAY:MOV R0, #32H
MOV R1, #70H
MOV R5, #03H
MOV A, 33H
CJNE A, #12H, LP2
LP2: JC LP1
MOV A, #9AH
CLR C
SUBB A, #12H
ADD A, 33H
DA A
LP1: MOV 32H, A ;调整显示模式LP3: MOV A, @R0
SWAP A
ANL A, #0FH
MOV @R1, A
INC R1
MOV A, @R0
ANL A, #0FH
MOV @R1, A
INC R1
DEC R0
MOV @R1, #0AH ;:ASCII码减30H INC R1
DJNZ R5, LP3
MOV A, #88H ;第二行首地址
ACALL WRC
MOV R0, #70H
LP4: MOV A, @R0
ADD A, #30H
ACALL WRD
INC R0
RET ;显示时间
KEY: JB KEY0, KK1
ACALL DELAY
JB KEY0, KK1
JNB KEY0, $
INC R4
CJNE R4, #02H, KOEND
MOV R4, #00H KOEND: RET
KK1:
JB KEY1, KK2
ACALL DELAY
JB KEY1, KK2
JNB KEY1, $
CLR TR0
INC R3
CJNE R3, #04H, K1END
MOV R3, #00H
SETB TR0
K1END: RET
KK2: JB KEY2, KK3
ACALL DELAY
JB KEY2, KK3
JNB KEY2, $
MOV A, R3
RL A
MOV DPTR, #KJMP2
JMP @A+DPTR
KJMP2: AJMP K2END
AJMP SINC
AJMP MINC
AJMP HINC
SINC: MOV A, 30H
ADD A, #01H
DA A
MOV 30H, A
CJNE A, #60H, K2END
MOV 30H, #00H
AJMP K2END
MINC: MOV A, 31H
ADD A, #01H
DA A
MOV 31H, A
CJNE A, #60H, K2END
MOV 31H, #00H
AJMP K2END
HINC: MOV A, 33H
ADD A, #01H
DA A
MOV 33H, A
CJNE A, #24H, K2END
MOV 33H, #00H
K2END: RET ;增1程序KK3: JB KEY3, K3END
ACALL DELAY
JB KEY3, K3END
JNB KEY3, $
MOV A, R3
RL A
MOV DPTR, #KJMP3
JMP @A+DPTR
KJMP3: AJMP K3END
AJMP SDEC
AJMP MDEC
AJMP HDEC
SDEC: MOV A, #9AH
CLR C
SUBB A, #01H
ADD A, 30H
DA A
MOV 30H, A
CJNE A, #99H, K3END
MOV 30H, #59H
AJMP K3END
MDEC: MOV A, #9AH
CLR C
SUBB A, #01H
ADD A, 31H
DA A
MOV 31H, A
CJNE A, #99H, K3END
MOV 31H, #59H
AJMP K3END
HDEC: MOV A, #9AH
CLR C
SUBB A, #01H
ADD A, 33H
DA A
MOV 33H, A
CJNE A, #99H, K3END
MOV 33H, #59H
AJMP K3END
K3END: RET ;减1程序
T0INT: PUSH ACC
PUSH PSW
MOV TL0, #0E0H
MOV TH0, #0B1H
DJNZ R2, NEXT
MOV R2, #32H
MOV A, 30H
ADD A, #01H
DA A
MOV 30H, A
CJNE A, #60H, NEXT
MOV 30H, #00H
S1: MOV A, 31H
ADD A, #01H
DA A
MOV 31H, A
CJNE A, #60H, NEXT
MOV 31H, #00H
S2: MOV A, 32H
ADD A, #01H
DA A
MOV 33H, A
MOV A, 33H
CJNE A, #24H, NEXT
MOV 33H, #00H
NEXT: POP P SW
POP A CC
RETI ;定时器计时
WRITE: ;以下全为子程序
MOV 30H,#8
LOP1:
MOV A,B
RRC A
MOV B,A
MOV P1.0,C
SETB P1.1
CLR P1.1
DJNZ 30H,LOP1
RET ;写入命令子程序DS1302
READ:
MOV 30H,#8
LOP2:
MOV C,P1.0
RRC A
SETB P1.1
CLR P1.1
DJNZ 30H,LOP2
RET ;读出数据子程序DS1302
WRC: CLR RS
CLR RW
SETB E
MOV P2, A
CLR E
ACALL DELAY
RET ;写入LCD命令的程序
WRD: SETB RS
CLR RW
SETB E
MOV P2, A
CLR E
ACALL DELAY
RET ;写入LCD要显示的数据的
DELAY: MOV R7, #35H
LP5: MOV R6, #3FH
DJNZ R6, $
DJNZ R7, LP5
RET ;延时
END ACALL DELAY
RET ;写入LCD要显示的数据的DELAY: MOV R7, #35H
LP5: MOV R6, #3FH
DJNZ R6, $
DJNZ R7, LP5
RET ;延时 END。