由于小弟C能力有限,就用汇编写了一个DS1302+LED 程序, 程序在按键数据处理上有问题,实在不好写.
高电平点亮位数码管!
CLK BIT P3.5;预定义ds1302 IO BIT P3.6 RST BIT P3.7 SPEAKER BIT P3.1;蜂鸣器 KEY_S BIT P3.2;按键定义 KEY_M BIT P3.3 KEY_H BIT P3.4 SECOND EQU 30H;秒地址 MINUIE EQU 31H;分 HOUR EQU 32H;时地址 DAY EQU 33H MONTH EQU 34H WEEK EQU 35H YEAR EQU 36H Q1 EQU 50H Q2 EQU 51H CTH EQU (65536-50000)/256 CTL EQU (65536-50000) MOD 256 ORG 0000H SJMP START ORG 000BH LJMP TIME_0 ORG 0030H JMP START START: MOV SP,#60H ;开始 MOV R2,#20 MOV DPTR,#ABC MAIN: MOV TMOD,#01H MOV TH0,#CTH MOV TL0,#CTL SETB EA SETB ET0 CALL READ1302 MOV ACC,30H JNB ACC.7,JUMP CALL INIT JUMP: SETB TR0 LOOP: CALL DISPLAY JNB KEY_S,K1 JNB KEY_M,K2 JNB KEY_H,K3 SJMP LOOP K1: CALL DELAY JB KEY_S,LOOP MOV A,30H MOV B,#16 DIV AB MOV A,B INC A CJNE A,#10,D1 MOV A,30H ADD A,#16 CJNE A,#60H,D4 MOV A,#00H JMP D4 K2: CALL DELAY JB KEY_M,LOOP MOV A,31H MOV B,#16 DIV AB MOV A,B INC A CJNE A,#10,D2 MOV A,31H ADD A,#16 CJNE A,#60H,D5 MOV A,#00H JMP D5 K3: CALL DELAY JB KEY_H,LOOP MOV A,32H MOV B,#16 DIV AB MOV A,B INC A CJNE A,#10,D3 MOV A,32H ADD A,#16 CJNE A,#30H,D6 MOV A,#00H JMP D6 D1: MOV A,30H ;MIAO TIAO ZHENG SHU RU INC A D4: MOV 30H,A CALL DISPLAY CLR CLK CLR RST MOV Q1,#80H MOV Q2,30H MOV R1,#Q1 CALL W1302 CALL DISPLAY JMP LOOP D2: MOV A,31H ;FEN YIAO ZHENG SHU RU INC A D5: MOV 31H,A CALL DISPLAY CLR CLK CLR RST MOV Q1,#82H MOV Q2,31H MOV R1,#Q1 CALL W1302 CALL DISPLAY JMP LOOP D3: MOV A,32H ;SHI SHU JU SHU RU INC A D6: MOV 32H,A CALL DISPLAY CLR CLK CLR RST MOV Q1,#84H MOV Q2,32H MOV R1,#Q1 CALL W1302 CALL DISPLAY JMP LOOP INIT: CLR CLK ;初始化1302 CLR RST MOV Q1,#8EH;OPEN WRITE PROTECTION MOV Q2,#00H MOV R1,#Q1 CALL W1302 MOV Q1,#90H;OPEN BATTREY MOV Q2,#0AAH MOV R1,#Q1 CALL W1302 MOV Q1,#80H;START MOV Q2,#00H MOV R1,#Q1 CALL W1302 RET W1302: CLR CLK NOP SETB RST NOP NOP MOV R7,#2 W_1: MOV R6,#8 MOV A,@R1 CLR C W_2: CLR CLK RRC A MOV IO,C NOP SETB CLK DJNZ R6,W_2 INC R1 DJNZ R7,W_1 CLR RST NOP CLR CLK RET READ1302: MOV R0,#30H ; 读1302数据 MOV R5,#3 MOV R7,#81H READ1302_: MOV A,R7 CLR RST NOP SETB RST NOP MOV R4,#8 ;1302写一字节 CLR C SETONE1: CLR CLK RRC A MOV IO,C NOP SETB CLK DJNZ R4,SETONE1 GET1302: MOV R4,#8 ;1302读一字节 CLR C GETONE1: SETB CLK NOP CLR CLK NOP MOV C,IO RRC A DJNZ R4,GETONE1 CLR RST NOP CLR CLK ;WAN BI MOV @R0,A INC R0 INC R7 INC R7 DJNZ R5,READ1302_ RET
TIME_0: PUSH ACC ;中断服务 PUSH PSW MOV TH0,#CTH MOV TL0,#CTL DJNZ R2,KEIL CALL READ1302 MOV R2,#20 KEIL: POP PSW POP ACC RETI DISPLAY: MOV A,SECOND ;显示子程序 MOV B,#16 DIV AB MOVC A,@A+DPTR MOV P2,A SETB P1.1 CALL DELAY CLR P1.1;@@@@@@@@@秒十位 MOV A,B MOVC A,@A+DPTR MOV P2,A SETB P1.0 CALL DELAY CLR P1.0;@@@@@@@@@@@@秒个位 MOV P2,#0BFH SETB P1.2 CALL DELAY CLR P1.2;@@@@@@@@@222分秒之间横杠 MOV A,MINUIE MOV B,#16 DIV AB MOVC A,@A+DPTR MOV P2,A SETB P1.4 CALL DELAY CLR P1.4;@@@@@@@@@@分十位 MOV A,B MOVC A,@A+DPTR MOV P2,A SETB P1.3 CALL DELAY CLR P1.3;@@@@@@@@@@@@分个位 MOV P2,#0BFH SETB P1.5 CALL DELAY CLR P1.5;@@@@@@@@@@分时横杠 MOV A,HOUR MOV B,#16 DIV AB MOVC A,@A+DPTR MOV P2,A SETB P1.7 CALL DELAY CLR P1.7;@@@@@@@@@@@时十位 MOV A,B MOVC A,@A+DPTR MOV P2,A SETB P1.6 CALL DELAY CLR P1.6 RET DELAY: MOV R3,#5 N1: MOV R6,#250 DJNZ R6,$ DJNZ R3,N1 RET ABC: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H ;码表 END 另有ds1302 资料打包
在此感谢王老师提供的视频。 |