《AT89C51单片机的数字时钟设计课件.ppt》由会员分享,可在线阅读,更多相关《AT89C51单片机的数字时钟设计课件.ppt(22页珍藏版)》请在三一办公上搜索。
1、1.模式0,显示时钟,2.模式1,第1次按KEY1键选择调秒,3.模式2,第2次按KEY1键选择调分,4.模式3,第3次按KEY1键选择调时,KEY1 BIT P1.0KEY2 BIT P1.1KEY3 BIT P1.2CLK BIT P1.3LOAD BIT P1.4DIN BIT P1.5SECOND DATA 30HMINUTE DATA 31HHOUR DATA 32H ORG 0000H AJMP MAIN ORG 000BH AJMP TIMER0MAIN:MOV SP,#60H MOV TMOD,#01H;定时器T0方式设置,MOV TH0,#0B1H;定时器T0装入初值20ms
2、 MOV TL0,#0E0H SETB ET0;T0中断允许 SETB EA;总中断允许 MOV R2,#50;T0中断为1s SETB TR0;启动T0 MOV SECOND,#00H MOV MINUTE,#00H MOV HOUR,#08H ;初始化时间8点00分00秒 MOV A,HOUR时间值送显示缓冲区79H7EH ANL A,#0F0H SWAP A MOV 79H,A,MOV A,HOUR ANL A,#0FH MOV 7AH,A MOV A,MINUTE ANL A,#0F0H SWAP A MOV 7BH,A MOV A,MINUTE ANL A,#0FH MOV 7CH,
3、A MOV A,SECOND ANL A,#0F0H SWAP A MOV 7DH,A MOV A,SECOND ANL A,#0FH MOV 7EH,A MOV R3,#00H;R3初值设为0,显示时钟 ACALL INI7219;调用7219初始化子程序,I NI7219:MOV 50H,#0FH;显示测试寄存器设置 MOV 51H,#00H;非测试模式,正常显示MOV R0,#50HACALL SEND7219MOV 50H,#0CH;关闭显示寄存器MOV 51H,#01H;正常显示MOV R0,#50HACALL SEND7219MOV 50H,#0BH;扫描界限寄存器MOV 51H,
4、#05H;6位显示MOV R0,#50HACALL SEND7219MOV 50H,#0AH;亮度寄存器,其值为000EH,设置为0AH,MOV 51H,#09HMOV R0,#50HACALL SEND7219MOV 50H,#09H;译码方式寄存器MOV 51H,#0FFH;8位显示全部B译码MOV R0,#50HACALL SEND7219RETDISPLAY:MOV 50H,#01H;显示子程序 MOV 51H,79H;显示缓冲区设在79H7EH单元MOV R0,#50H;其内容从左至右一次显示在数码管显示器上ACALL SEND7219MOV 50H,#02HMOV 51H,7AH,
5、MOV R0,#50HACALL SEND7219MOV 50H,#03HMOV 51H,7BHMOV R0,#50HACALL SEND7219MOV 50H,#04HMOV 51H,7CHMOV R0,#50HACALL SEND7219MOV 50H,#05HMOV 51H,7DHMOV R0,#50HACALL SEND7219MOV 50H,#06HMOV 51H,7EHMOV R0,#50HACALL SEND7219RET,SEND7219:CLR LOAD;向7219写入数据子程序 MOV R4,#02H;写入16位串行数据CLR CLOP1: MOV R1,#08H MOV
6、A,R0LOP2: RLC A MOV DIN,C SETB CLK CLR CLK DJNZ R1,LOP2 INC R0 DJNZ R4,LOP1 SETB LOAD RET;键扫描处理子程序KEY: JNB KEY1,KK1;判断KEY1键是否按下 JB KEY1,$;等待KEY1键是否释放,INC R3;模式值增1 ,在0、1、2、3间逐项切换 CJNE R3,#04H,JMPKK3;当让中的模式值为4时,在设置为0 MOV R3,#00HJMPKK3:AJMP KK3;转至KK3,按R3内容选择显示KK1:JNB KEY2,KK2;判断KEY2键是否按下 JB KEY2,$;等待KE
7、Y2键释放MOV A,R3;根据当前R3值对时间项增1调整RL AMOV DPTR,#KJMP1JMP A+DPTRKJMP1: AJMP DISCLK AJMP SINC AJMP MINC AJMP HINC,SINC: MOV A,SECOND;秒增1 ADD A,#01H DA A MOV SECOND,A CJNE A,#60H,DISS1 MOV 30H,#01HDISS1:AJMP DISS;转至秒显示缓冲区设置MINC:MOV A,MINUTE;分增1 ADD A,#01H DA A MOV MINUTE,A CJNE A,#60H,DISM1 MOV MINUTE,#00HD
8、ISM1:AJMP DISM;转至分显示缓冲区设置HINC: MOV A,HOUR;时增1 ADD A,#01H DA A MOV HOUR,A,CJNE A,#24H,DISH1 MOV HOUR,#00HDISH1:AJMP DISH;转至时显示缓冲区设置KK2:JNB KEY3,KK3;判断KEY3是否按下 JB KEY3,$;等待KEY3键释放MOV A,R3RL AMOV DPTR,#KJMP2JMP A+DPTRKJMP2:AJMP DISCLK AJMP SDEC AJMP MDEC AJMP HDECSDEC:MOV A,#9AH;秒减1 CLR C SUBB A,#01H A
9、DD A,SECOND DA A,MOV SECOND,A CJNE A,#00H,DISS2 MOV SECOND,#59HDISS2:AJMP DISS;转至秒显示缓冲区设置MDEC:MOV A,#9AH;分减1 CLR C SUBB A,#01H ADD A,MINUTE DA A MOV MINUTE,A CJNE A,#0,DISM2 MOV MINUTE,#59HDISM2:AJMP DISM;转至分显示缓冲区设置HDEC:MOV A,#9AH;时减1 CLR C SUBB A,#01H ADD A,HOUR DA A,MOV HOUR,A CJNE A,#99H,DISH2 MO
10、V HOUR,#23HDISH2:AJMP DISH;转至时显示缓冲区设置KK3:MOV A,R3 RL AMOV DPTR,#KJMP3 JMP A+DPTRKJMP3:AJMP DISCLK ;R3=0显示时钟 AJMP DISS;R3=1显示秒 AJMP DISM;R3=2显示分 AJMP DISH;R3=3显示时DISCLK:MOV R0,#79H MOV R1,#HOUR MOV R4,#03H,CFLOOP:MOV A,R1 SWAP A ANL A,#0FH MOV R0,A INC R0 MOV A,R1 ANL A,#0FH MOV R0,A INC R0 DEC R1 DJ
11、NZ R4,CFLOOP SETB TR0 RETDISS:CLR TR0;显示秒,时与分的数码管不亮 MOV A,SECOND SWAP A ANL A,#0FH MOV 7DH,A MOV A,SECOND,ANL A,#0FH MOV 7EH,A MOV 79H,#0FH MOV 7AH,#0FH MOV 7BH,#0FH MOV 7CH,#0FH AJMP KK5DISM:CLR TR0;显示分,时与秒的数码管不亮 MOV A,MINUTE SWAP A ANL A,#0FH MOV 7BH,A MOV A,MINUTE ANL A,#0FH MOV 7CH,A MOV 79H,#0F
12、H MOV 7AH,#0FH MOV 7DH,#0FH MOV 7EH,#0FH AJMP KK5,DISH: CLR TR0;显示时,分与秒的数码管不亮 MOV A,HOUR SWAP A ANL A,#0FH MOV 79H,A MOV A,HOUR ANL A,#0FH MOV 7AH,A MOV 7BH,#0FH MOV 7CH,#0FH MOV 7DH,#0FH MOV 7EH,#0FHKK5:RET;定时器T0中断处理子程序TIMER0:PUSH ACC;保护现场 PUSH PSW MOV TH0,#0B1H;重装定时常数 MOV TL0,#0E0H DJNZ R2,NEXT;判断
13、是否中断了50次,不足50次,中断返回MOV R2,#50;若达到50次,计时满1s,刷新时间单元格,MOV A,SECOND ADD A,#01H DA A MOV SECOND,A CJNE A,#60H,NEXTS1:MOV SECOND,#00H MOV A,MINUTEADD A,#01HDA AMOV MINUTE,ACJNE A,#60H,NEXTS2:MOV MINUTE,#00H MOV A,HOUR ADD A,#01H DA A MOV HOUR,A CJNE A,#24H,NEXT MOV HOUR,#00HNEXT:POP PSW;恢复现场 POP ACCRETI;中断返回 END,