- 功能要求
- 一、程序
- 二、仿真
功能要求
掌握单片机 C 语言判断语句、分支语句以及子程序调用等编程知识
此程序调试时间方式为先暂停再调时,故有调秒的功能。
(1) 实现正确稳定地显示小时(两位数)、分钟(两位数)、秒钟(两位数),同时数
码管应无闪烁问题
(2) 通过按键分别实现时、分、秒信息的调整,方便用户对时间的校准
一、程序
#include二、仿真#define uChar unsigned char #define uInt unsigned int uChar a[10]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6}; //0~9 uChar b[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; uChar second=55,minute=59,hour=23,count; sbit Key1 = P1^0; //计时停止 sbit Key2 = P1^1; //调位 sbit Key3 = P1^2; //加一 //sbit Buzzer=P1^1; void Delay(uInt t) { while(t) { t--; } } void Dispaly1(uChar second,uChar minute,uChar hour) { P2=b[0];P0=a[hour/10];Delay(10);P0=0x00; P2=b[1];P0=a[hour%10];Delay(10);P0=0x00; P2=b[2];P0=0x02;Delay(10);P0=0x00; P2=b[3];P0=a[minute/10];Delay(10);P0=0x00; P2=b[4];P0=a[minute%10];Delay(10);P0=0x00; P2=b[5];P0=0x02;Delay(10);P0=0x00; P2=b[6];P0=a[second/10];Delay(10);P0=0x00; P2=b[7];P0=a[second%10];Delay(10);P0=0x00; } void Keyscan1() { static uChar i=0,j=0; if(Key1==0) { Delay(300); if(Key1==0) while(!Key1); i++; } if(i%2==1) { TR0=0; } if(i%2==0) { TR0=1; } if(Key2==0) { Delay(300); if(Key2==0) while(!Key2); j++; } if(j%4==1) { if(Key3==0) { Delay(300); if(Key3==0) while(!Key3); second++; if(second==60) second=0; } } if(j%4==2) { if(Key3==0) { Delay(300); if(Key3==0) while(!Key3); minute++; if(minute==60) minute=0; } } if(j%4==3) { if(Key3==0) { Delay(300); if(Key3==0) while(!Key3); hour++; if(hour==24) hour=0; } } } void main() { TMOD=0x01; TH0=(65536-20000)/256; TL0=(65536-20000)%256; EA=1; ET0=1; TR0=1; while(1) { Dispaly1(second,minute,hour); Keyscan1(); } } void time0_int(void) interrupt 1 { TH0=(65536-20000)/256; TL0=(65536-20000)%256; count++; if(count==100) { count=0; second++; if(second==60) { second=0; minute++; if(minute==60) { minute=0; hour++; if(hour==24) { hour=0; } } } } }



