1、基于 51单片机的电子钟 C语言程序#include#include#define uchar unsigned char#define uint unsigned int/*七段共阴管显示定义*/ uchar code dispcode =0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0xBF,0x86,0xCB,0xCF,0xEF,0xED,0xFD,0x87,0xFF,0xDF;/*定义并初始化变量*/uchar seconde=0;uchar minite=0;uchar hour=12;uchar mstcnt=0;sbit P1
2、_0=P10; / second 调整定义sbit P1_1=P11; /minite调整定义sbit P1_2=P12; /hour调整定义/*函数声明*/void delay(uint k ); /延时子程序void delay1(uchar h );void time_pro( ); /时间处理子程序void display( ); /显示子程序void keyscan( ); /键盘扫描子程序/*/*延时子程序*/*/void delay1 (uchar h)uchar j;while(h-)!=0)for(j=0;j0;c-)for(b=38;b0;b-)for(a=130;a0;a
3、-); /*/*时间处理子程序*/*/void time_pro( void) if(seconde=60) /秒钟设为 60进制 seconde=0;minite+;if( minite=60) /分钟设为 60进制 minite=0;hour+;if(hour=24) /时钟设为 24进制hour=0; /*/* 显示子程序*/*/void display(void) P2=0xfe;P0=dispcodehour/10; /显示小时的十位delay1(4);P2=0xfd;P0=(dispcode(hour%10)|0X80; /显示小时的个位delay1(4);P2=0xfb;P0=d
4、ispcodeminite/10; /显示分的十位delay1(4);P2=0xf7;P0=(dispcodeminite%10)|0X80; /显示分的个位delay1(4);P2=0xef;P0=dispcodeseconde/10; /显示秒的十位delay1(4);P2=0xdf;P0=dispcodeseconde%10; /显示秒的个位delay1(4); /*/*键盘扫描子程序*/*/void keyscan (void) if(P1_0=0) /按键 1秒的调整 delay(2);if(P1_0=0)/delay(300);seconde+;if(seconde=60)seco
5、nde=0; if(P1_1=0) /按键 2分的调整 delay(2);if(P1_1=0)/ delay(300);minite+;if(minite=60)minite=0;if(P1_2=0) /按键 3小时的调整 delay(2);if(P1_2=0)/delay(300);hour+;if(hour=24)hour=0; void timer0(void) interrupt 1 using 0 /定时器 0方式 1,50ms 中断一次TH0=0x3c;TMOD = 0x11;mstcnt+;if(mstcnt=20)seconde+;mstcnt=0; /注意点。对计数单元的清零十分的重要,本次调试中就是 /因为忽略了这一点,给我早成了很大的被动。/*/*主函数*/*/void main(void) P1=0xff; /初始化 p1口,全设为 1 TMOD = 0x11; /time0为定时器,方式 1TH0=0x3c; /预置计数初值TL0=0xb0;EA=1; ET0=1; TR0=1; while (1) keyscan( ); /按键扫描time_pro( ); /时间处理display( ); /显示时间