您好,欢迎访问三七文档
本人为单片机初学者,这是本人制作的一个简单的数字时钟程序,程序比较繁琐,只适合初学者学习参考使用,因为程序都是一步步来的,没有跳跃。只要将此程序复制到keil,然后编译生成hex文件,连到单片机(实物)就可以直接显示了。希望对有些人有帮助#includereg52.h//头文件#defineucharunsignedchar#defineuintunsignedint//宏定义ucharcount;uinthour,minute,second;uinthour1=0,minute1=0;sbitdula=P2^6;sbitwela=P2^7;sbitkey1=P3^0;sbitkey2=P3^1;sbitkey3=P3^2;sbitkey4=P3^3;sbitkey5=P3^4;sbitkey6=P3^5;sbitkey7=P3^6;sbitBEEP=P1^2;ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0-9,横杆ucharcodewei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//扫描voiddelay(uintz)//延时函数{ucharx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidinit()//初始化中断{TMOD=0x10;//定时器1以方式1定时TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;//总中断打开ET1=1;//打开定时中断TR1=1;//打开定时器}voidtime1()interrupt3{TH1=(65536-50000)/256;TL1=(65536-50000)%256;count++;if(count==20)//1秒累加一次{count=0;second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}}voiddisplay(){P0=0xff;//消影wela=1;//位打开P0=wei[0];//显示第一位wela=0;//锁存dula=1;//段打开P0=duan[hour/10];//段赋值dula=0;//段锁存delay(10);P0=0xff;wela=1;P0=wei[1];wela=0;dula=1;P0=duan[hour%10];dula=0;delay(10);P0=0xff;wela=1;P0=wei[2];wela=0;dula=1;P0=0x40;dula=0;delay(10);P0=0xff;wela=1;P0=wei[3];wela=0;dula=1;P0=duan[minute/10];dula=0;delay(10);P0=0xff;wela=1;P0=wei[4];wela=0;dula=1;P0=duan[minute%10];dula=0;delay(10);P0=0xff;wela=1;P0=wei[5];wela=0;dula=1;P0=0x40;dula=0;delay(10);P0=0xff;wela=1;P0=wei[6];wela=0;dula=1;P0=duan[second/10];dula=0;delay(10);P0=0xff;wela=1;P0=wei[7];wela=0;dula=1;P0=duan[second%10];dula=0;delay(10);}voidkey_scan()//按键+和-{if(key1==0){delay(10);if(key1==0)if(hour23)hour++;elsehour=0;while(!key1);}if(key3==0){delay(10);if(key3==0)if(minute59)minute++;elseminute=0;while(!key3);}if(key5==0){delay(10);if(key5==0)if(second59)second++;elsesecond=0;while(!key5);}if(key2==0){delay(10);if(key2==0)if(hour0)hour--;elsehour=23;while(!key2);}if(key4==0){delay(10);if(key4==0)if(minute0)minute--;elseminute=59;while(!key4);}if(key6==0){delay(10);if(key6==0)if(second0)second--;elsesecond=59;while(!key6);}if(key7==0)//复位功能{delay(10);if(key7==0){hour=0;minute=0;second=0;}while(!key7);}}voidzhengdianbaoshi()//整点报时(从50秒开始){if(hour==hour&&minute==59&&second50){BEEP=!BEEP;}}voidnaozhong()//闹钟七点半,就响30秒{if(hour==7&&minute==30&&second30)BEEP=!BEEP;}voidmain()//主函数{init();while(1){key_scan();display();naozhong();zhengdianbaoshi();}}
本文标题:数字时钟C语言程序
链接地址:https://www.777doc.com/doc-5077454 .html