C51单片机多功能数字钟C源程序

合集下载

基于51单片机的数码管时钟(带闹钟调时秒表)

基于51单片机的数码管时钟(带闹钟调时秒表)

基于51单片机的数码管时钟(带闹钟调时秒表)/**************************************************************** ************ ** *********************************************************** ** * * ** * * ** * 基于51单片机的数码管时钟程序* ** ** * * ** * * *********************************************************** ** ** ************************************************************ ****************/#include<stdio.h>#define uint unsigned int#define uchar unsigned charsbit dula=P2^6; //申明u1锁存器的锁存端sbit wela=P2^7; //申明u2锁存器的锁存端sbit key1=P3^0; //功能键sbit key2=P3^1;//增大键sbit key3=P3^2;//减小键sbit key4=P3^3;//秒表查看键sbit beep=P2^3;//蜂鸣端uchar num,num1,num2,numm;uchar numf,num,nums,num;uchar s,s1,f,f1,m,m1;uchar ns,ns1,nf,nf1;uchar numns,numnf;uchar dingshi;void display(uchar m,uchar m1,ucharf,uchar f1,uchar s,uchar s1);void display1(uchar nf,uchar nf1,uchar ns,uchar ns1);void key();void key1();void alram();void di();void delayms(xms);void T0_time() interrupt 1;void miaobiao();uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x77,0x7c,0x39,0x5e,0x79,0x71};/*********************************************************主函数void main()*********************************************************/int main(){TMOD=0X01;TH0=(65536-46080)/256;TL0=(65536-46080)%256;EA=1;ET0=1;TR0=1;nums=12;numf=0;num2=0;num=0;while(1){if(dingshi==0){keyscan();display(m,m1,f,f1,s,s1);alarm();}else{miaobiao();keyscan1();display1(nf,nf1,ns,ns1);}}return 0;}/********************************************************* 蜂鸣器函数di()*********************************************************/ void di(){beep=0;delayms(100);beep=1;}/********************************************************** 按键调时函数key()**********************************************************/ void keyscan(){if(key1==0){delayms(10);display(m,m1,f,f1,s,s1);}}}/********************************************** 按键设定闹钟函数keyscan1();***********************************************/ void keyscan1(){if(key1==0){delayms(10);if(key1==0){ numns++;if(numns==24);numns=0;while(!key1);di();display1(nf,nf1,ns,ns1);}}if(key2==0){delayms(10);if(key2==0){ numnf++;if(numnf==24);numnf=0;while(!key2);di();display1(nf,nf1,ns,ns1);}}if(key3==0){delayms(10);if(key3==0){dingshi=0;while(!key1);di();display1(nf,nf1,ns,ns1);}}}/********************************************************* 闹钟函数alram()*********************************************************/ void alram(){if((nums==numns)&&(numnf==numf))beep=0;}/*********************************************************秒表函数miaobiao()*********************************************************/ void miaobiao(){if(key4==0){delayms(100);if(key4==0){while(!key4);di();displaym(uchar m,uchar m1);}}}/********************************************************* 正常显示函数display()*********************************************************/ void display(uchar m,uchar m1,ucharf,uchar f1,uchar s,uchar s1){dula=1;P0=table[m1]//秒位第1位dula=0;PO=0xff;// 送入位选信号前关闭所有显示,防止打开位选锁存时wela=1;P0=0xef;wela=0;delayms(10);dula=1;P0=table[m]; //秒第2位dula=0;P0=0xff;wela=1;P0=0xdf;wela=0;delayms(100)dula=1;P0=table[f1]; //分第1位dula=0;P0=0xff;wela=1;P0=0xfb;wela=0;delayms(100)dula=1;P0=table[f]; //分第2位dula=0;P0=0xff;wela=1;P0=0xf7;wela=0;delayms(10)dula=1;P0=table[s1]; //时第1位dula=0;P0=0xff;wela=1;P0=0xfe;wela=0;delayms(100)dula=1;P0=table[s]; //时第2位dula=0;P0=0xff;wela=1;P0=0xfd;wela=0;delayms(10)}/********************************************************** ***调时闹钟显示函数display1()*********************************************************** **/void display1(uchar nf,uchar nf1,uchar ns,uchar ns1){nf1=numnf/10;nf=numnf%10;ns1=nums/10;ns=nums%10;dula=1;P0=table[nf1];//分第1位dula=0;P0=0xff;wela=1;P0=0xfb;//待改参数wela=0;delayms(10);dula=1;P0=table[nf];//分第2位dula=0;P0=0xff;wela=1;P0=0xf7;//待改参数wela=0;delayms(10);P0=table[ns1];//时第1位dula=0;P0=0xff;wela=1;P0=0xfe;//待改参数wela=0;delayms(10);dula=1;P0=table[ns];//时第2位dula=0;P0=0xfd;wela=1;P0=0xff;//待改参数wela=0;delayms(10);}/********************************************************** ***秒表显示函数displaym()*********************************************************** **/void displaym(uchar m,uchar m1){dula=1;P0=table[m1];//秒位第一位P0=0xff;wela=1;P0=0Xbf;wela=0;delayms(10);dula=1;P0=table[m];dula=0;P0=0xff;wela=1;P0=0X7f;wela=0;delayms(10);}/**************************************************** 中断服务函数void T0_time() interrupt 1****************************************************/ void T0_time() interrupt 1{TH0=(65536-45872)/256; //46080TL0=(65536-45872)%256;num2++;if(num2==20){num++;num2=0;m=num%10;m1=num/10;f=numf%10;f1=numf/10;s=nums%10;s1=nums/10;if(num==59){num=0;numf++;if(numf==59){numf=0;nums++;}if(nums==24)nums=0;}}}/*************************************************** 延时函数delayms()***************************************************/ void delayms(xms){uint i,j;for(i=xms;i>0;i--)for(j=110;j>0;j--); }。

基于C51单片机的数字时钟课程设计(C语言,带闹钟).

基于C51单片机的数字时钟课程设计(C语言,带闹钟).

单片机技术课程设计数字电子钟学院:班级:姓名:学号:教师:摘要电子钟在生活中应用非常广泛,而一种简单方便的数字电子钟则更能受到人们的欢迎。

所以设计一个简易数字电子钟很有必要。

本电子钟采用AT89C52单片机为核心,使用12MHz 晶振与单片机AT89C52 相连接,通过软件编程的方法实现以24小时为一个周期,同时8位7段LED数码管(两个四位一体数码管)显示小时、分钟和秒的要求,并在计时过程中具有定时功能,当时间到达提前定好的时间进行蜂鸣报时。

该电子钟设有四个按键KEY1、KEY2、KEY3、KEY4和KEY5键,进行相应的操作就可实现校时、定时、复位功能。

具有时间显示、整点报时、校正等功能。

走时准确、显示直观、运行稳定等优点。

具有极高的推广应用价值。

关键词:电子钟 AT89C52 硬件设计软件设计目录一、数字电子钟设计任务、功能要求说明及方案介绍 (4)1.1 设计课题设计任务 (4)1.2 设计课题的功能要求说明 (4)1.3 设计课的设计总体方案介绍及工作原理说明 (4)二、设计课题的硬件系统的设计 (5)2.1硬件系统各模块功能简要介绍 (5)2.1.1 AT89C52简介 (5)2.1.2 按键电路 (6)三、设计课题的软件系统的设计 (6)3.1 使用单片机资源的情况 (6)3.2 软件系统个模块功能简要介绍 (7)3.3 软件系统程序流程框图 (7)3.4 软件系统程序清单 (7)四、设计课题的设计结论、仿真结果、误差分析 (9)4.1 设计结论及使用说明 (9)4.2 仿真结果 (10)结束语 (12)参考文献 (12)附录 (13)附录A:程序清单 (13)一、数字电子钟设计任务、功能要求说明及方案介绍1.1 设计课题设计任务设计一个具有特定功能的电子钟。

具有时间显示,并有时间设定,时间调整功能。

1.2 设计课题的功能要求说明设计一个具有特定功能的电子钟。

该电子钟上电或按键复位后能自动显示系统提示符“d.1004-22”,进入时钟准备状态;第一次按电子钟启动/调整键,电子钟从12时59分0秒开始运行,进入时钟运行状态;按电子钟S5键,则电子钟进入时钟调整状态,此时可利用各调整键调整时间,调整结束后可按S5键再次进入时钟运行状态。

51-数字时钟-89C52-单片机C语言程序

51-数字时钟-89C52-单片机C语言程序

51-数字时钟-89C52-单片机C语言程序数字时钟89C52 单片机C语言程序STC89C52|/************** 【数字时钟】****************//****【功能】1、时间显示2、秒表3、闹钟4、日期显示都可以设置****/#include /*包含器件配置文件*/#define uchar unsigned char /*宏定义字符型数据整型数据*/ #define uint unsigned intuchar code H[] = {0x0f, 0x07, 0x0b, 0x0d, 0x0e}; /*按键【P3】端口断码用于按键*/ char Code[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d,0x7d, 0x07, 0x7f, 0x6f}; /*显示【0 1 2 3 4 5 6 7 8 9】数字的数码管的段码*/ uchar code C[] = {0x0, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};/*列扫描控制LED1位2位3位4位5位6位7位8位*/uchar MON[]={0,31,28,31,30,31,30,31,31,30,31,30,31};uchar A;uchar BIN=0; /* 【BIN】作为倒计时开始的标志*/uchar hour = 0; /* 定义[时][分][秒] */uchar min = 0;uchar sec = 0;uint shi=12;uint fen=30;uchar Mmin=0;uchar Msec=30;uchar M0=0;uchar m=0;uchar year=9;uchar month=7;uchar month2;uchar day=19;uchar set1 = 1; /* set1=1 是调节时分秒set1=2时时调节年月日set=3时事调节闹钟*/uchar set2 = 1; /* set2=1时是调节【时】位set2=2时事调节【分】位*/uint x = 0; /* x 每【0.01s】自加一*/void Delay(uint k);void Timer0() /*定时器T0 5mS初始化*/{TMOD = 0x01; /*设置*定时器0**计数方式一* (每12个时钟脉冲计数一次)或IT0=1 第二种计数方式*/TH0 = (65536 - 10000) / 256; /*给定计时器高位赋予初值=15536/256*/TL0 = (65536 - 10000) % 256; /*给定计时器低位赋予初值=15536%256 */ET0 = 1; /*打开定时器外部终断0允许ET1是中断器1的开关*/ EA = 1; /*打开总中断允许*/TR0 = 1;}void T0_srv(void) interrupt 1 /*5mS定时中断服务函数秒分时的值*/{TH0 = (65536 - 10000) / 256; /*给定计时器高位赋予初值=15536/256*/TL0 = (65536 - 10000) % 256; /*给定计时器低位赋予初值=15536%256 */x++;if(x==100){sec++;x=0; /*没每一秒【sec】加一*/if(sec==60){min++;sec=0;if(min==60) /*每一分钟【min】加一*/ {min = 0;hour++;if(hour==24){hour = 0; /*每一小时【hour】加一*/ day++;if(year%4==0) /*是否闰年的判断*/ MON[2]=29;else MON[2]=28;if(day==MON[month]){day=0;month++;}}}}}if(BIN==1)if(Mmin!=0||Msec!=0){M0 = 100-x;if(M0==100){ if(Mmin>0){Msec--;if(Msec==0){Msec=60;Mmin-- ;}if(Msec==0) {Mmin=0;Msec=0;M0=0;} }if(Mmin==0){if(Msec!=0)Msec--;if(Msec==0){Msec=0;M0=0;}}}}if(Mmin==0&&Msec==0){M0=0;}} /*秒表分秒M0的赋值*/}void Scan_key() /*按键扫描函数【Scan_key()】*/ {P3=H[0];if(P3!=H[0])Delay(1);if(P3==H[4]){set1++;if(set1>4) set1 = 1;er1: if(P3==H[4]) goto er1; /*按下键后未释放,在此等候*/ }Delay(1);if(P3==H[3]){set2++;if(set2>3) set2 = 1;er2: if(P3==H[3]) goto er2; /*按下键后未释放,在此等候*/ }Delay(1);if(P3==H[1]){BIN++;if(BIN>1) BIN=0;er3: if(P3==H[1]) goto er3; /*按下键后未释放,在此等候*/ }}}void Time_show() /*时间的显示变量为【时】【分】【秒】*/ { P0 = Code[hour/10]; /*第一位数字显示【时】的【十】位*/ P2 = C[1];Delay(1);P0 = Code[hour%10]; /*第二位数字显示【时】的【个】位*/P2 = C[2];Delay(1);if(x/50==0)P0 = 0x40; /*第三位符号【-】的显示*/elseP0 = 0x00;P2 = C[3];Delay(1);P0 = Code[min/10]; /*第四位的数字的显示【分】的【十】位*/ P2 = C[4];Delay(1);P0 = Code[min%10]; /*第五位的数字显示【分】的【个】位*/ P2 = C[5];Delay(1);Delay(1);if(x/50==0)P0 = 0x40; /*第六位符号【-】的显示*/elseP0 = 0x00;P2 = C[6];Delay(1);P0 = Code[sec/10]; /*第七位数字的显示【秒】的【十】位显示*/P2 = C[7];Delay(1);P0 = Code[sec%10]; /*第八位数字的显示【秒】的【个】位显示*/ P2 = C[8];Delay(1);}void Year_show(){P0 = Code[year/10]; /*第一位数字显示【年】的【十】位*/P2 = C[1];Delay(1);P0 = Code[year%10]; /*第二位数字显示【年】的【个】位*/P2 = C[2];Delay(1);P0 = 0x40; /*第三位符号【-】的显示*/P2 = C[3];Delay(1);P0 = Code[month/10]; /*第四位的数字的显示【月】的【十】位*/P2 = C[4];Delay(1);P0 = Code[month%10]; /*第五位的数字显示【月】的【个】位*/P2 = C[5];Delay(1);Delay(1);P0 = 0x40; /*第六位符号【-】的显示*/P2 = C[6];Delay(1);P0 = Code[day/10]; /*第七位数字的显示【天】的【十】位显示*/P2 = C[7];Delay(1);P0 = Code[day%10]; /*第八位数字的显示【天】的【个】位显示*/ P2 = C[8];Delay(1);}void Clock_show(){P0 = Code[shi/10]; /*第一位数字显示【时】的【十】位*/P2 = C[4];P0 = Code[shi%10]; /*第二位数字显示【时】的【个】位*/P2 = C[5];Delay(1);P0 = 0x40; /*第三位符号【-】的显示*/P2 = C[6];Delay(1);P0 = Code[fen/10]; /*第四位的数字的显示【分】的【十】位*/ P2 = C[7];Delay(1);P0 = Code[fen%10]; /*第五位的数字显示【分】的【个】位*/P2 = C[8];Delay(1);}void Miao_show() /*时间的显示变量为【时】【分】【秒】*/ { P0 = Code[Mmin/10]; /*第一位数字显示【时】的【十】位*/P2 = C[1];Delay(1);P0 = Code[Mmin%10]; /*第二位数字显示【时】的【个】位*/ P2 = C[2];Delay(1);P0 = 0x40; /*第三位符号【-】的显示*/P2 = C[3];Delay(1);P0 = Code[Msec/10]; /*第四位的数字的显示【分】的【十】位*/ P2 = C[4];Delay(1);P0 = Code[Msec%10]; /*第五位的数字显示【分】的【个】位*/ P2 = C[5];Delay(1);P0 = 0x40; /*第六位符号【-】的显示*/P2 = C[6];Delay(1);P0 = Code[M0/10]; /*第七位数字的显示【秒】的【十】位显示*/P2 = C[7];Delay(1);P0 = Code[M0%10]; /*第八位数字的显示【秒】的【个】位显示*/ P2 = C[8];Delay(1);}void Time_set() /*调整时间的函数time_set() */{Scan_key();if(set1==1){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) hour++; /*按一下第三行的键hour 就加一*/Delay(1);if(hour>24) hour=0;while(P3==H[2]); /*按键未释放,在此等候*/}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) min++; /*按一下第三行的键hour 就加一*/Delay(1);if(min>60){ min=0; hour++;}while(P3==H[2]); /*按键未释放,在此等候*/}}}void Year_set() /*调整时间的函数time_set() */{Scan_key();if(set1==2){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) year++; /*按一下第三行的键hour 就加一*/ Delay(1);if(year>100) year=0;while(P3==H[2]);}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) month++; /*按一下第三行的键hour 就加一*/ Delay(1);if(month>12){ month=0; year++;}while(P3==H[2]);}if(set2==3){if(P3==H[2]) day++; /*按一下第三行的键hour 就加一*/ Delay(1);if(day>MON[month]){ day=0; month++;}while(P3==H[2]); /*按键未释放,在此等候*/}}}Clock_set(){Scan_key();if(set1==3){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) shi++; /*按一下第三行的键hour 就加一*/ Delay(1);if(shi>24) shi=0;while(P3==H[2]); /*按键未释放,在此等候*/}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) fen++; /*按一下第三行的键hour 就加一*/ Delay(1);if(fen>60){ fen=0; shi++;}while(P3==H[2]); /*按键未释放,在此等候*/}}}void Miao_set() /*调整时间的函数time_set() */{Scan_key();if(set1==4){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) Mmin++; /*按一下第三行的键hour 就加一*/Delay(1);if(Mmin>24) Mmin=0;while(P3==H[2]); /*按键未释放,在此等候*/}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) Msec++; /*按一下第三行的键hour 就加一*/ Delay(1);。

51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)

51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)

51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)效果图:程序如下://51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)#include<reg51.h>#include<absacc.h>#define uchar unsigned char#define uint unsigned int/*七段共阴管显示定义*///此表为LED 的字模,共阴数码管0-9 -uchar code dispcode[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //段码控制/*定义并初始化变量*/uchar seconde=0;//秒uchar minite=0;//分uchar hour=12; //时uchar shi=0;//闹铃功能uchar fen=0;uchar bjcs;//报警次数sbit P1_0=P1^0; //second 调整定义sbit P1_1=P1^1; //minite调整定义sbit P1_2=P1^2; //hour调整定义sbit P1_5=P1^5; //整点报时sbit P1_3=P1^3; //闹铃功能,调整时间sbit P1_6=P1^6; //调整时sbit P1_7=P1^7; //调整分sbit P1_4=P1^4; //关闭闹铃/*函数声明*/void delay(uint k ); //延时子程序void time_pro( ); //时间处理子程序void display( ); //显示子程序void keyscan( ); //键盘扫描子程序/*xx子程序*/void delay (uint k){uchar j;while((k--)!=0){for(j=0;j<125;j++){;}}}/*时间处理子程序*/void time_pro(void){if(seconde==60){seconde=0;minite++;if(minite==60){minite=0;hour++;if(hour==24){hour=0;}}}}/*显示子程序*/void display(void){if(P1_3==1){P2=0XFE; P0=dispcode[seconde%10];//秒个位delay(1);P2=0XFD;P0=dispcode[seconde/10];//秒十位delay(1);P2=0XFB;P0=dispcode[10];//间隔符-delay(1);P2=0XF7;P0=dispcode[minite%10];//分个位delay(1);P2=0XEF;P0=dispcode[minite/10];//分十位delay(1);P2=0XDF;P0=dispcode[10];//间隔符-delay(1);P2=0XBF;P0=dispcode[hour%10];//时个位delay(1);P2=0X7F;P0=dispcode[hour/10];//时十位delay(1);}}/*键盘扫描子程序*/void keyscan(void){if(P1_0==0)//秒位的调整{delay (30);if(P1_0==0){seconde++;if(seconde==60){seconde=0;}}delay(250);}if(P1_1==0)//分位的调整{delay(30);if(P1_1==0){minite++;if(minite==60){minite=0;}}delay(250);}if(P1_2==0)//时位的调整{delay(30);if(P1_2==0){hour++;if(hour==24){hour=0;}}delay(250);}}/*整点报警*/void zhengdian (void){if((seconde==0)&(minite==0))//整点报时{P1_5=0;delay(1000);P1_5=1;}}/*定时闹钟*/void dingshi(void){if(P1_3==0)//按住P1_3BU不松,显示闹铃设置界面,分别按P1_6、P1_7设置闹铃时间。

单片机用C51实现时间、星期、年月、温度在DS1602上显示的多功能数字钟

单片机用C51实现时间、星期、年月、温度在DS1602上显示的多功能数字钟

//***************************************************************************** //******用DS1602实现可调日期,时间,星期及温度实时监测的实用数字钟功能******* //*****************创建日期:2010-1-7***********************//功能介绍:用3*3按键在DS1602上显示第一排显示"2010-01-05 MON"第二排显示"23:59:00"//*******************功能介绍**************************************//********* K6:adjtime K3:hour K2:minute K1:OK*******************//********* K3:adjalarm K3:hh K1:OK**********************//********* K2:lookalarm K1:OK **********************//********* K1:alarm ON/OFF ***********************//********* K5:adjdate (K9:month)K8:day (K7:week)K4:OK*********//***********************************************************************#include "main.h"void lcd_data(unsigned char dat);//DS1602数据写入unsigned char kbscan();//按键扫描void ds1602();//DS1602初始化void lcd_com(unsigned char com);//DS1602命令写入void timer0();//用于延时的定时中断初始2mSvoid disp();//主程序的显示调用void display();//正常的显示void display_alarm();//查看闹铃void alarm();//闹铃void process();//按键的处理unsigned char hands();//与18B20通信握手void write_temp_date(unsigned char dat);//写温度数据18B20unsigned char read_temp_date();//读温度数据18B20void tmpdelay(unsigned char tmp);//ds18b20对时序要求严格unsigned char result();//计算月份的天数//*******************************//DS1602命令的写入//*******************************void lcd_com(unsigned char com){rs=0;rw=0;P0=com;dema=1;while(dema!=0);en=1;dema=1;while(dema!=0);en=0;}//*********************//DS1602数据的写入//*********************void lcd_data(unsigned char dat){rs=1;rw=0;P0=dat;dema=1;while(dema!=0);en=1;dema=1;while(dema!=0);en=0;}//***************************//3*3按键的扫描//***************************unsigned char kbscan(){unsigned char sccode,recode,tenp;P1=0XF8;tenp=P1;if(tenp!=0xf8){demb=3;while(demb!=0);if(tenp==P1){sccode=0xfe;while((sccode&0xf7)!=0){P1=sccode;if((P1&0xf8)!=0xf8){recode=P1&0Xf8;sccode=sccode&0xc7;return(recode|sccode);}elsesccode=sccode<<1|0x01;}}}return(0);}//**************************//*********每月的天数计算***//**************************unsigned char result(){switch(month){case 1:return 31;break;case 3:return 31;break;case 5:return 31;break;case 7:return 31;break;case 8:return 31;break;case 10:return 31;break;case 12:return 31;break;case 4:return 30;break;case 6:return 30;break;case 9:return 30;break;case 11:return 30;break;case 2:if(year%4==0){return 29;//闰年29天}else{return 28;//平年28天}}}//**************************//*********DS1602的初始化***//**************************void ds1602(){lcd_com(0x38);//16*2显示5*7矩阵dema=1;while(dema!=0);lcd_com(0x0c);//显示开,无光标无闪烁dema=1;while(dema!=0);lcd_com(0x06);//指针+1,整屏不动dema=1;while(dema!=0);lcd_com(0x01);//清屏}//*******************************//定时中断延时的初始化以及中断***//*******************************void timer0(){TMOD=0X01;TH0=0Xf8;TL0=0X30; //2mS初值12M晶振TR0=1;ET0=1;EA=1;}TIME0() interrupt 1{TR0=0;TH0=0Xf8;TL0=0X30; //2mS初值TR0=1;count++;if(count>=500)//1s{count=0;second++;if(second>=60)//1m{second=0;minute++;if(minute>=60)//1h{minute=0;hour++;if(hour>=24)//1day{hour=0;flag_week++;//星期调整if(flag_week>8){flag_week=1;}day++;if(day>(days=result()))//1month{day=1;month++;if(month>12)//1year{month=1;year++;if(year>100){year=0;}}}}}}}if(dema)dema--;//写命令的延时if(demb)demb--;//按键的延时//************闹铃**********if(demc){demc--;}if(demd){demd--;}flag++;if(flag>=200){flag=0;temp=1;deme++;if(deme>=2){demc=0;temp=0;}}}//**************************//正常日期时间显示//**************************void display(){a=hour/10;b=hour%10;c=minute/10;d=minute%10;e=second/10;f=second%10;m3=year/10;m4=year%10;m6=month/10;m7=month%10;m9=day/10;m10=day%10;//*****第一排显示**********lcd_com(0x81);//第一排从第2个位置开始显示lcd_data(0x32);lcd_com(0x82);lcd_data(0x30);lcd_com(0x83);lcd_data(0x30+m3);lcd_com(0x84);lcd_data(0x30+m4);lcd_com(0x85);lcd_data(0x2d);//"-"lcd_com(0x86);lcd_data(0x30+m6);lcd_com(0x87);lcd_data(0x30+m7);lcd_com(0x88);lcd_data(0x2d);//"-"lcd_com(0x89);lcd_data(0x30+m9);lcd_com(0x8a);lcd_data(0x30+m10);if(flag_week==1)//周1{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week1[n]);}}else if(flag_week==2)//周2 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week2[n]);}}else if(flag_week==3)//周3 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week3[n]);}}else if(flag_week==4)//周4 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week4[n]);}}else if(flag_week==5)//周5 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week5[n]);}}else if(flag_week==6)//周6 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week6[n]);}}else if(flag_week==7)//周7{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week7[n]);}}//*****第二排显示***********lcd_com(0xc1);//第二排从第3个位置开始显示lcd_data(0x30+a);lcd_com(0xc2);lcd_data(0x30+b);lcd_com(0xc3);lcd_data(0x3a);//":"lcd_com(0xc4);lcd_data(0x30+c);lcd_com(0xc5);lcd_data(0x30+d);lcd_com(0xc6);lcd_data(0x3a);//":"lcd_com(0xc7);lcd_data(0x30+e);lcd_com(0xc8);lcd_data(0x30+f);lcd_com(0xca);lcd_data(0x47);//"G"lcd_com(0xcb);lcd_data(0x47);//"G"lcd_com(0xcd);all_date=all_date%100;lcd_data(match[all_date/10]);//十位lcd_com(0xce);lcd_data(match[all_date%10]);//个位lcd_com(0xcf);lcd_data('C');//"temperapure" "C"}//********************************************** //*************************看闹钟*************** void display_alarm(){a=hh/10;b=hh%10;c=min/10;d=min%10;e=sec/10;f=sec%10;m3=year/10;m4=year%10;m6=month/10;m7=month%10;m9=day/10;m10=day%10;//*****第一排显示**********lcd_com(0x81);//第一排从第2个位置开始显示lcd_data(0x32);lcd_com(0x82);lcd_data(0x30);lcd_com(0x83);lcd_data(0x30+m3);lcd_com(0x84);lcd_data(0x30+m4);lcd_com(0x85);lcd_data(0x2d);//"-"lcd_com(0x86);lcd_data(0x30+m6);lcd_com(0x87);lcd_data(0x30+m7);lcd_com(0x88);lcd_data(0x2d);//"-"lcd_com(0x89);lcd_data(0x30+m9);lcd_com(0x8a);lcd_data(0x30+m10);if(flag_week==1)//周1{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week1[n]);}}else if(flag_week==2)//周2{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week2[n]);}}else if(flag_week==3)//周3 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week3[n]);}}else if(flag_week==4)//周4 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week4[n]);}}else if(flag_week==5)//周5 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week5[n]);}}else if(flag_week==6)//周6 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week6[n]);}}else if(flag_week==7)//周7 {lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week7[n]);}}//****第二排**************lcd_com(0xc1);//第二排从第3个位置开始显示lcd_data(0x30+a);lcd_com(0xc2);lcd_data(0x30+b);lcd_com(0xc3);lcd_data(0x3a);//":"lcd_com(0xc4);lcd_data(0x30+c);lcd_com(0xc5);lcd_data(0x30+d);lcd_com(0xc6);lcd_data(0x3a);//":"lcd_com(0xc7);lcd_data(0x30+e);lcd_com(0xc8);lcd_data(0x30+f);lcd_com(0xcd);all_date=all_date%100;lcd_data(match[all_date/10]);//十位lcd_com(0xce);lcd_data(match[all_date%10]);//个位lcd_com(0xcf);lcd_data('C');//"temperapure" "C"}//**********************//闹钟开的显示**********//**********************void display_onalarm(){a=hour/10;b=hour%10;c=minute/10;d=minute%10;e=second/10;f=second%10;m3=year/10;m4=year%10;m6=month/10;m7=month%10;m9=day/10;m10=day%10;//*****第一排显示**********lcd_com(0x81);//第一排从第2个位置开始显示lcd_data(0x32);lcd_com(0x82);lcd_data(0x30);lcd_com(0x83);lcd_data(0x30+m3);lcd_com(0x84);lcd_data(0x30+m4);lcd_com(0x85);lcd_data(0x2d);//"-"lcd_com(0x86);lcd_data(0x30+m6);lcd_com(0x87);lcd_data(0x30+m7);lcd_com(0x88);lcd_data(0x2d);//"-"lcd_com(0x89);lcd_data(0x30+m9);lcd_com(0x8a);lcd_data(0x30+m10);if(flag_week==1)//周1{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week1[n]);}}else if(flag_week==2)//周2{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week2[n]);}}else if(flag_week==3)//周3{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week3[n]);}}else if(flag_week==4)//周4{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week4[n]);}}else if(flag_week==5)//周5{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week5[n]);}}else if(flag_week==6)//周6{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week6[n]);}}else if(flag_week==7)//周7{lcd_com(0x8c);for(n=0;n<3;n++){lcd_data(week7[n]);}}//****第二排**************lcd_com(0xc1);//第二排从第3个位置开始显示lcd_data(0x30+a);lcd_com(0xc2);lcd_data(0x30+b);lcd_com(0xc3);lcd_data(0x3a);//":"lcd_com(0xc4);lcd_data(0x30+c);lcd_com(0xc5);lcd_data(0x30+d);lcd_com(0xc6);lcd_data(0x3a);//":"lcd_com(0xc7);lcd_data(0x30+e);lcd_com(0xc8);lcd_data(0x30+f);lcd_com(0xca);lcd_data(0x4B);//"K"lcd_com(0xcb);lcd_data(0x4B);//"K"lcd_com(0xcd);all_date=all_date%100;lcd_data(match[all_date/10]);//十位lcd_com(0xce);lcd_data(match[all_date%10]);//个位lcd_com(0xcf);lcd_data('C');//"temperapure" "C" }//*****************************//*******按键的处理************//***************************** void process(){if(key!=0){if(key==K6){display();while((key=kbscan())!=K1){display();if((key=kbscan())==K3){hour++;if(hour>=24){hour=0;}}if((key=kbscan())==K2){minute++;if(minute>=60){minute=0;}}}}else if(key==K2)//看闹铃{while(((key=kbscan())!=K1)){display_alarm();}}else if(key==K3)//调闹铃{display_alarm();while((key=kbscan())!=K1){display_alarm();if((key=kbscan())==K3){hh++;if(hh>=24){hh=0;}}if((key=kbscan())==K2){min++;if(min>=60){min=0;}}}}else if(key==K1)//开/关闹钟{flag_alarm=~flag_alarm; }else if(key==K5)//调日期星期{display();while((key=kbscan())!=K4)//ok{display();// if((key=kbscan())==K9)//month // {// month++;// if(month>12)// {// month=1;// }// }if((key=kbscan())==K8)//day{day++;flag_week++;if(flag_week>7){flag_week=1;}if(day>(days=result())){day=1;month++;if(month>12){month=1;year++;if(year>100){year=0;}}}// if((key=kbscan())==K7)//week // {// flag_week++;// if(flag_week>8)// {// flag_week=1;// }// }}}}}}//*************************************//*****************闹铃****************//*************************************void alarm(){if(flag_offalarm==1){if(flag_alarm==1){if(temp){TR0=0;TH0=0Xf8;TL0=0X30;TR0=1; //重装初值2mSdemd=2;while(demd!=0);//P37=~P37;}else{TR0=0;TH0=0XF8;TL0=0X30;TR0=1; //重装初值demc=1;while(demc!=0);//P37=~P37;}}else{P37=1;flag_offalarm=0;}}elseif((flag_alarm==1)&&(minute==min)&&(hour==hh)) {flag_offalarm=1;if(temp){TR0=0;TH0=0Xf8;TL0=0X30;TR0=1; //重装初值2mSdemd=2;while(demd!=0);//P37=~P37;}else{TR0=0;TH0=0XF8;TL0=0X30;TR0=1; //重装初值demc=1;while(demc!=0);//P37=~P37;}}}//**********************************//***********DS18B20时序控制*********** //**********************************void tmpdelay(unsigned char tmp)//注意!!{while(tmp>0){tmp--;}}//*tmdelay(1) ;//16.28us//增加一个就加6.5us // tmdelay(2) ;//22.78us// tmdelay(3) ;//29.30us// tmdelay(4) ;//35.81us// tmdelay(70) ;//465.49// tmdelay(74) ;//491.54// tmdelay(80) ;//530.60// tmdelay(115) ;//150us *//*例如16.28+(?*6.5)=490*///********************************//**************************************//**********DS18B20读取温度***********//*************************************unsigned char read_temp_date()//读温度数据18B20{unsigned char i;unsigned int y=0;value=0;for(i=8;i>0;i--){value>>=1;DQ=0;// tmpdelay(1);//>15usy++;y++;y++;y++;//1-15usDQ=1;// tmpdelay(1);//读温度对时序要求严格不能用tmpdelay(1)代替y++;if(DQ==1){value=value|0x80;}else{}tmpdelay(16);//120US}return (value);}//**************************************//**********DS18B20写指令温度***********//*************************************void write_temp_date(unsigned char dat)//写温度指令18B20{unsigned char i;for(i=8;i>0;i--){DQ=0;tmpdelay(1);//>15usif((dat&0x01)==1){DQ=1;tmpdelay(16);//120us}else{DQ=0;tmpdelay(16);//120usDQ=1;tmpdelay(1);//>15us}dat=dat>>1;}DQ=1;tmpdelay(1);//>15us}//**************************************//**********DS18B20复位*****************//*************************************unsigned char hands()//握手复位{temp=0;// noanswer:do{DQ=0;tmpdelay(72);//490uSDQ=1;tmpdelay(11);/*>60uS 注意:此处把函数名改为hans()后,这里两处的tmpdelay()括号内最好改成74和12*/}while(DQ!=0);temp=1;tmpdelay(35);//>240usDQ=1;return(temp);//此处可以采用两种方式表达,1用直到...才;2用goto转向// if(DQ==1)// {// goto noanswer;// }// else if(DQ==0)// {// temp=1;// tmpdelay(40);//>250uS// DQ=1;// }// return(temp);}void disp(){if(flag_alarm){display_onalarm();}else{display();}}//***********************//*******主程序**********//***********************main(void){timer0();//中断延时初始化ds1602();//初始化while(1){key=kbscan();//调用按键process();//按键处理alarm();//闹零if(hands()==1){write_temp_date(0xcc);//跳过write_temp_date(0x44);//温度转换tmpdelay(75);//500us// dema=250;// while(dema!=0);//500ms}if(hands()==1){write_temp_date(0xcc);//跳过write_temp_date(0xbe);//读取温度数据tmpdelay(75);//500us// dema=250;// while(dema!=0);//500ms}date_l=read_temp_date();//读取低位温度date_h=read_temp_date();//读取高位温度all_date=((date_h<<4)|(date_l>>4));// 整数位// dot_date=date_l&0x0f;// dot_date=dot_date*625;//小数位// all_date=all_date+dot_date/10000;disp();//显示}}。

51单片机时钟代码(带秒表闹钟功能).

51单片机时钟代码(带秒表闹钟功能).
write_com(0x80+0x00+7);
}
}
}
if(s6==0)
{
delay(5);
if(s6==0)
{
while(!s6);
di();
if(s4num==1)
{
miao--;
if(miao<0)
miao=59;
write_alarm(10,miao);
write_com(0x80+0x00+13);
ep=0;
}
voidwrite_data(uchardat) //写入字符显示数据到LCD
{
while(lcd_bz()); //等待LCD空闲
rs=1;
rw=0;
ep=0;
P0=dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
}
if(s1num==3)
{
hour++;
if(hour==24)
hour=0;
write_time(4,hour);
write_com(0x80+0x40+7);delay(5);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
di();
if(s1num==1)
批注本地保存成功开通会员云端永久保存去开通
#include <reg51.h>
#include <intrins.h>

基于c51功能最全的电子钟程序(lcd1602)

基于c51功能最全的电子钟程序(lcd1602)

功能最全的电子钟【单片机】c51数字时钟(带年月日显示)摘要:本设计以单片机为核心,lcd1602显示。

采用独立键盘输入能任意修改当前时间日期和设定闹钟时间。

具有显示年月日(区分闰年和二月),闹钟报警和整点报时功能主程序:/********************************************************************************************************************************************************************************* ****************************************** lcd1602电子钟********************************************************************************************************************************** *************************************************************************************************** ********************/# include <reg52.h># include "lcd16024.h"sbit key1 = P2^0; //调整sbit key2 = P2^1; //加1sbit key3 = P2^2; //减1sbit speaker = P2^3; //蜂鸣器sbit key4 = P2^4; //闹钟设计bit cal_year = 1; //进入判断闰年标志位bit leap_year; //闰年标志位bit calculate = 0; //日加一标记bit run = 0; //闹钟标志bit beep = 0; //整点报时标志//uint8 num = 0; //调整是给的脉冲uint8 code str1[] = "D: ";uint8 code str2[] = "T: ";uint8 code str3[] = "Wek";uint8 daystr[]="2013-07-29 "; //年月日格式uint8 timestr[]="21:30:59 N"; //时分秒格式uint8 daystr1[]="2013-07-29 "; //闹钟年月日格式uint8 timestr1[]="21:30:59 N"; //闹钟时分秒格式uint8 numweek = 0; //星期加1标记char week = 1; //星期char sec = 53; //秒char min = 50; //分char hour = 23; //时uint8 day = 30; // 日uint8 month = 9; //月uint16 year = 2013; //年char week1 = 1; //闹钟星期char sec1 = 58; //闹钟秒char min1 = 50; //闹钟分char hour1 = 23; //闹钟时uint8 day1 = 30; //闹钟日uint8 month1 = 9; //闹钟月uint16 year1 = 2013; //闹钟年uint8 WeekData1; //闹钟星期标记uint8 number = 0; //定时uint8 WeekData; //星期标记uint8 speaker_num; //整点报时次数uint8 scan_key(void); //函数声名/****************************************************************************** ***************************** 更新LCD时间分离读取******************************************************************************************************************* *******/void TimeChange(){//时分秒timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';timestr[4] = min%10+'0';timestr[3] = min/10+'0';timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';//年月日daystr[9] = day%10+'0';daystr[8] = day/10+'0';daystr[6] = month%10+'0';daystr[5] = month/10+'0';daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';//星期WeekData = week+'0';}/****************************************************************************** ***************************** 闹钟更新LCD时间分离读取******************************************************************************************************************* *******/void TimeChange1(){//时分秒timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';//年月日daystr1[9] = day1%10+'0';daystr1[8] = day1/10+'0';daystr1[6] = month1%10+'0';daystr1[5] = month1/10+'0';daystr1[3] = year1%10+'0';daystr1[2] = year1/10%10+'0';daystr1[1] = year1/100%10+'0';daystr1[0] = year1/1000+'0';//星期WeekData1 = week1+'0';}/****************************************************************************** ***************************** 初始化系统定时器0 ******************************************************************************************************************* *******/void systimer0_init(void){TMOD |=0x01;//设置为1时用或(|)TMOD &=0xfd;//设置为0时用与(&)TH0 = 0xDC; // 定时10msTL0 = 0x00;EA = 1;ET0=1;TR0=1;EX0 = 1;}/****************************************************************************** ***************************** 闹钟时间设置*************************************************************************************************************** *******/void naozhong(){uint8 number = 1;uint8 a = 0;uint8 b = 0;if(0 == key4){Delay1Ms(5);if(0 == key4){a = 1;LCD_write_command(0xc0+9);LCD_write_command(0x0f);}while(!key4);}while(a){if(0 == key4){Delay1Ms(5);if(0 == key4){a = 0;run = ~run;while(!key4);}}if(run){timestr[9] = 'Y';}else{timestr[9] = 'N';}if(0 == key1){Delay1Ms(5);if(0 == key1){b = 1;}while(!key1);}while(b){if(0 == key1){Delay1Ms(5);if(0 == key1){number++;if(4 == number)b = 0;}while(!key1);if(number == 1){LCD_write_command(0xc0+9);LCD_write_command(0x0f);}if(number == 2){LCD_write_command(0xc0+6);LCD_write_command(0x0f);}if(number == 3){LCD_write_command(0xc0+3);LCD_write_command(0x0f);}}}switch(number){case 1:if(0 == key2) //闹钟秒加1的设置{Delay1Ms(5);if(0 == key2){sec1++;if(60 == sec1){sec1 = 0;}while(!key2);timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';LCD_write_char(8,1,timestr1[6]);LCD_write_char(9,1,timestr1[7]);LCD_write_command(0xc0+9);}}if(0 == key3) //闹钟秒减1的设置Delay1Ms(5);if(0 == key3){sec1--;if(sec1 < 0){sec1 = 59;}while(!key3);timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';LCD_write_char(8,1,timestr1[6]);LCD_write_char(9,1,timestr1[7]);LCD_write_command(0xc0+9);}}break;case 2:if(0 == key2) //闹钟分加1的设置{Delay1Ms(5);if(0 == key2){min1++;if(60 == min1){min1 = 0;}while(!key2);timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';;LCD_write_char(5,1,timestr1[3]);LCD_write_char(6,1,timestr1[4]);LCD_write_command(0xc0+6);}}if(0 == key3) //闹钟分减1的设置{Delay1Ms(5);if(0 == key3){min1--;if(min1 < 0){min1 = 59;}while(!key3);timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';;LCD_write_char(5,1,timestr1[3]);LCD_write_char(6,1,timestr1[4]);LCD_write_command(0xc0+6);}}break;case 3:if(0 == key2) //闹钟时加1的设置{Delay1Ms(5);if(0 == key2){hour1++;if(24 == hour1){hour1 = 0;}while(!key2);timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';LCD_write_char(2,1,timestr1[0]);LCD_write_char(3,1,timestr1[1]);LCD_write_command(0xc0+3);}}if(0 == key3) //闹钟时减1的设置{Delay1Ms(5);if(0 == key3){hour1--;if(hour1 < 0){hour1 = 23;}while(!key3);timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';LCD_write_char(2,1,timestr1[0]);LCD_write_char(3,1,timestr1[1]);LCD_write_command(0xc0+3);}}break;case 4:b = 0;LCD_write_command(0x0c);break;}}}LCD_write_command(0x0c);while(!key4);}/****************************************************************************** ***************************** 判断按键进入时间调整*************************************************************************************************************** *******/uint8 scan_key(void){uint8 number = 1;uint8 a = 0;if(0 == key1){Delay1Ms(5);if(0 == key1){while(!key1);a = 1;LCD_write_command(0xc0+9);LCD_write_command(0x0f);}}while(a){if(0 == key1){Delay1Ms(5);if(0 == key1){number++;while(!key1);TR0 = 0;if(number == 2){LCD_write_command(0xc0+6);LCD_write_command(0x0f);}if(number == 3){LCD_write_command(0xc0+3);LCD_write_command(0x0f);}if(number == 4){LCD_write_command(0x80+11);LCD_write_command(0x0f);}if(number == 5){LCD_write_command(0x80+8);LCD_write_command(0x0f);}if(number == 6){LCD_write_command(0x80+5);LCD_write_command(0x0f);}if(number == 7){LCD_write_command(0xc0+14);LCD_write_command(0x0f);}if(8 == number){LCD_write_command(0x0c);a = 0;number = 0;}}}switch(number){case 1:if(0 == key2) //秒加1的设置{Delay1Ms(5);if(0 == key2){sec++;if(60 == sec){sec = 0;}timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';LCD_write_char(8,1,timestr[6]);LCD_write_char(9,1,timestr[7]);LCD_write_command(0xc0+9);while(!key2);}}if(0 == key3) //秒减1的设置{Delay1Ms(5);if(0 == key3){sec--;if(sec < 0){sec = 59;}timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';LCD_write_char(8,1,timestr[6]);LCD_write_char(9,1,timestr[7]);LCD_write_command(0xc0+9);while(!key3);}}break;case 2:if(0 == key2) //分加1的设置{Delay1Ms(5);if(0 == key2){min++;if(60 == min){min = 0;}timestr[4] = min%10+'0';timestr[3] = min/10+'0';;LCD_write_char(5,1,timestr[3]);LCD_write_char(6,1,timestr[4]);LCD_write_command(0xc0+6);while(!key2);}}if(0 == key3) //分减1的设置{Delay1Ms(5);if(0 == key3){min--;if(min < 0){min = 59;}timestr[4] = min%10+'0';timestr[3] = min/10+'0';LCD_write_char(5,1,timestr[3]);LCD_write_char(6,1,timestr[4]);LCD_write_command(0xc0+6);while(!key3);}}break;case 3:if(0 == key2) //时加1的设置{Delay1Ms(5);if(0 == key2){hour++;while(!key2);if(24 == hour){hour = 0;}timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';LCD_write_char(2,1,timestr[0]);LCD_write_char(3,1,timestr[1]);LCD_write_command(0xc0+3);}}if(0 == key3) //时减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);hour--;if(hour < 0){hour = 23;}timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';LCD_write_char(2,1,timestr[0]);LCD_write_char(3,1,timestr[1]);LCD_write_command(0xc0+3);}}break;case 4:if(0 == key2) //日加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);calculate = 1;if(calculate == 1){if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day++;if(day > 31){day=1;}}if(month==4|month==6|month==9|month==11){day++;if(day > 30){day=1;}}if(month == 2){cal_year = 1;while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day++;if(day > 30){day=1;}}else{day++;if(day > 29){day=1;}}}calculate = 0;}daystr[9] = day%10+'0';daystr[8] = day/10+'0';LCD_write_char(10,0,daystr[8]);LCD_write_char(11,0,daystr[9]);LCD_write_command(0x80+11);}}if(0 == key3) //日减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);calculate = 1;if(calculate == 1){if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day--;if(day == 0){day=31;}}if(month==4|month==6|month==9|month==11){day--;if(day == 0){day=30;}}if(month == 2){cal_year = 1;while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day--;if(day == 0){day=30;}}else{day--;if(day == 0){day=29;}}}calculate = 0;}daystr[9] = day%10+'0';daystr[8] = day/10+'0';LCD_write_char(10,0,daystr[8]);LCD_write_char(11,0,daystr[9]);LCD_write_command(0x80+11);}}break;case 5:if(0 == key2) //月加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);month++;if(13 == month){month = 1;}daystr[6] = month%10+'0';daystr[5] = month/10+'0';LCD_write_char(7,0,daystr[5]);LCD_write_char(8,0,daystr[6]);LCD_write_command(0x80+8);}}if(0 == key3) //月减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);month--;if(month == 0){month = 12;}daystr[6] = month%10+'0';daystr[5] = month/10+'0';LCD_write_char(7,0,daystr[5]);LCD_write_char(8,0,daystr[6]);LCD_write_command(0x80+8);}}break;case 6:if(0 == key2) //年加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);year++;}daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';LCD_write_char(2,0,daystr[0]);LCD_write_char(3,0,daystr[1]);LCD_write_char(4,0,daystr[2]);LCD_write_char(5,0,daystr[3]);LCD_write_command(0x80+5);}if (0 == key3) //年减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);year--;if(year == 0){year = 2020;}daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';LCD_write_char(2,0,daystr[0]);LCD_write_char(3,0,daystr[1]);LCD_write_char(4,0,daystr[2]);LCD_write_char(5,0,daystr[3]);LCD_write_command(0x80+5);}}break;case 7:if(0 == key2) //星期加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);week++;if(7 == week){week = 0;}LCD_write_char(14,1,week+'0');LCD_write_command(0xc0+14);}}if (0 == key3) //星期减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);week--;if(week < 0){week = 6;}LCD_write_char(14,1,week+'0');LCD_write_command(0xc0+14);}}break;case 8:TR0 = 1;break;}}}/****************************************************************************** ***************************** 主函数******************************************************************************************************************* *******/void main(void){systimer0_init();LCD_init();LCD_write_str(0,0,str1);LCD_write_str(0,1,str2);LCD_write_str(13,0,str3);speaker = 0;while (1){TimeChange();scan_key();naozhong();LCD_write_str(2,0,daystr);LCD_write_str(2,1,timestr);LCD_write_char(14,1,WeekData);if(1 == run){if(sec==sec1 & month==month1 & hour==hour){speaker_num =30;beep = 1;}}else{speaker_num =0;beep = 0;}}}/****************************************************************************** ***************************** 定时中断0 ******************************************************************************************************************* *******/void time_0() interrupt 1{TH0 = 0xDC; // 定时10msTL0 = 0x00;number++;if(number ==100){sec++;if(beep){speaker=!speaker;speaker_num--;if(speaker_num == 0){beep=0;speaker = 0;}}if(sec == 60){sec = 0;min++;if(min == 60){min = 0;hour++; //小时加1speaker_num = hour%12; //蜂鸣器响的次数beep = 1;if(hour == 24){hour = 0;calculate = 1;if(calculate == 1) //判断这个月有多少天{if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day++;if(month==7|month==12){week++;if(7 == week){week = 0;}}else{if(day <= 31){week++;if(7 == week){week = 0;}}}if(day > 31){if(month==7|month==12){day = 1;}else{day=0;}month++;if(month > 12){month=1;year++;cal_year=1;}}}if(month==4|month==6|month==9|month==11) {day++;week++;if(7 == week){week = 0;}if(day > 30){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}if(month == 2){while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day++;if(day <= 30){week++;if(7 == week){week = 0;}}if(day > 30){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}else{day++;week++;if(7 == week){week = 0;}if(day > 29){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}}}calculate=0;}}}}}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Lcd1602.c子程序:////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /****************************************************************************** ***************************** lcd1602低层函数********************************************************************************************************************* *******/# include <reg52.h># include <intrins.h># include "lcd16024.h"# define LCD_DATA P0 //LCD1602的数据口定义sbit LCD_RS = P0^0; //LCD1602控制线的定义,4位控制方式sbit LCD_RW = P0^1;sbit LCD_EN = P0^2;/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void Delay1Us(uint16 n){for(;n>0;n--){_nop_();}}/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void Delay1Ms(uint16 n){while(n--){Delay1Us(1000);}}/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void LCD_en_write(void){LCD_EN=0;LCD_EN=1;Delay1Us(1);LCD_EN=0;}/****************************************************************************** ************************ LCD写一个字节命令函数************************************************************************************************************ *******/void LCD_write_command(uint8 command){Delay1Us(16);LCD_RS=0;LCD_RW=0;LCD_DATA&=0x0f;LCD_DATA|=command&0xf0;LCD_en_write();command=command<<4;LCD_DATA&=0x0f;LCD_DATA|=command&0xf0;LCD_en_write();}/****************************************************************************** ********************** LCD写一个字节数据函数*************************************************************************************************************** *******/void LCD_write_data(uint8 Data){Delay1Us(16);LCD_RS=1;LCD_RW=0;LCD_DATA&=0x0f;LCD_DATA|=Data&0xf0;LCD_en_write();Data=Data<<4;LCD_DATA&=0x0f;LCD_DATA|=Data&0xf0;LCD_en_write();}/****************************************************************************** ***************************** LCD1602光标定位函数************************************************ x--列0~15;y--行0~1********************************************************************************************************* *******/void LCD_set_xy(uint8 x,uint8 y){uint8 address;if(y==0)address=0x80+x;else address=0xc0+x;LCD_write_command(address);}/***************************************************************************************************** LCD1602 初始化函数,四位显示方式******************************************************************************************************* *******/void LCD_init(){LCD_write_command(0x28);// Delay1Us(40);LCD_write_command(0x28);LCD_write_command(0x0c);LCD_write_command(0x01);LCD_write_command(0x06);Delay1Ms(2);}/****************************************************************************** ***************************** LCD写字符串函数******************************************************* x--列0~15;y--行0~1******************************************************* s指向字符串数组**************************************************************************************************************** *******/void LCD_write_str(uint8 x,uint8 y,uint8 *s){LCD_set_xy(x,y);while(*s){LCD_write_data(*s);s++;}}/****************************************************************************** ***************************** LCD写一个字符函数******************************************************* x--列0~15;y--行0~1******************************************************* d--字符的ASCII码**************************************************************************************************************** *******/void LCD_write_char(uint8 x,uint8 y,uint8 d){LCD_set_xy(x,y);LCD_write_data(d);}/*////////////////////////////////////////////////////////////////// 等待繁忙标志/////////////////////////////////////////////////////////////////void LCD_wait(void){P0 = 0xFF;do{LCD_RS = 0;LCD_RW = 1;LCD_EN = 0;LCD_EN = 1;}while (BUSY == 1);LCD_EN = 0;}*//****************************************************************************** ***************************** LCD1602左移********************************************************************************************************************* *******void LCD_youyi(uint8 y,uint8 *s){LCD_write_str(17,y,s);for(a=0;a<16;a++){LCD_write_command(0x1c); //左移LCD_write_command(0x1c); 为右移Delay1Ms(6);}}*///LCD_write_command(0x0d);//光标闪烁//LCD_write_command(0x0e);//光标显示不闪烁//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Lcd1602.h头文件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef _LCD16024_H_#define _LCD16024_H_typedef unsigned char uint8;typedef unsigned int uint16;typedef unsigned long uint32;void Delay1Us(uint16 n);void Delay1Ms(uint16 n);void LCD_write_data(uint8 dat);void LCD_write_command(uint8 com); //BuysC为0时忽略忙检测void LCD_set_xy(uint8 x,uint8 y);void LCD_en_write(void);void LCD_write_char(uint8 x, uint8 y, uint8 Data1);void LCD_write_str(uint8 x, uint8 y,uint8 *s);void LCD_init();//void LCD_wait(void);//void LCD_youyi(uint8 y,uint8 *s);#endif。

基于C51单片机的数字时钟课程设计(C语言带闹钟)

基于C51单片机的数字时钟课程设计(C语言带闹钟)

单片机技术课程设计数字电子钟学院:班级:姓名:学号:教师:摘要电子钟在生活中应用非常广泛,而一种简单方便的数字电子钟则更能受到人们的欢迎。

所以设计一个简易数字电子钟很有必要。

本电子钟采用AT89C52单片机为核心,使用12MHz 晶振与单片机AT89C52 相连接,通过软件编程的方法实现以24小时为一个周期,同时8位7段LED数码管(两个四位一体数码管)显示小时、分钟和秒的要求,并在计时过程中具有定时功能,当时间到达提前定好的时间进行蜂鸣报时。

该电子钟设有四个按键KEY1、KEY2、KEY3、KEY4和KEY5键,进行相应的操作就可实现校时、定时、复位功能。

具有时间显示、整点报时、校正等功能。

走时准确、显示直观、运行稳定等优点。

具有极高的推广应用价值。

关键词:电子钟 AT89C52 硬件设计软件设计目录NO TABLE OF CONTENTS ENTRIES FOUND.一、数字电子钟设计任务、功能要求说明及方案介绍1.1 设计课题设计任务设计一个具有特定功能的电子钟。

具有时间显示,并有时间设定,时间调整功能。

1.2 设计课题的功能要求说明设计一个具有特定功能的电子钟。

该电子钟上电或按键复位后能自动显示系统提示符“d.1004-22”,进入时钟准备状态;第一次按电子钟启动/调整键,电子钟从12时59分0秒开始运行,进入时钟运行状态;按电子钟S5键,则电子钟进入时钟调整状态,此时可利用各调整键调整时间,调整结束后可按S5键再次进入时钟运行状态。

1.3 设计课的设计总体方案介绍及工作原理说明本电子钟主要由单片机、键盘、显示接口电路和复位电路构成,设计课题的总体方案如图1所示:图1-1总体设计方案图本电子钟的所有的软件、参数均存放在AT89C52的Flash ROM和内部RAM 中,减少了芯片的使用数量简化了整体电路也降低了整机的工作电流。

键盘采用动态扫描方式。

利用单片机定时器及计数器产生定时效果通过编程形成数字钟效果,再利用数码管动态扫描显示单片机内部处理的数据,同时通过端口读入当前外部控制状态来改变程序的不同状态,实现不同功能。

数字钟51单片机程序c语言

数字钟51单片机程序c语言
F0:if(P3_4==0)
goto F0; //*按键未释放,在此等候*//
}
//*延时子函数*/
unsigned char i,j;
for(i=5;i>0;i--)
for(j=248;j>0;j--);
}
//*5mS定时中断服务 子函数*//
void zd(void) interrupt 1
{
TH0=-(4800//256);
TL0=-(4800%256);
deda++;
}
//*调整走时时间*//
{
t_dirve();
}
if(P3_7==0)
delay(1);
if(P3_7==0)
t_min++;
if(t_min==60)
{
if(deda<=100)
d_05s=0;
else d_05s=1;
if(deda>=200)
{sec++;deda=0;}
if(sec==60)
{min++;sec=0;}
if(min==60)
{hour++;min=0;}
if(hour==24)
{hour=0;}
p_out(); //*判别闹时到 子函数*// 否
}
//*定时器 T0 5mS初始化*//
void init_timer()
{
TMOD=0x01;
TH0=-(4800//256);
TL0=-(4800%256);
IE=0x82;

51单片机带字库液晶12864ds1302数字时钟c源程序(无按键修改功能)

51单片机带字库液晶12864ds1302数字时钟c源程序(无按键修改功能)

51单片机+带字库液晶12864+DS1302数字时钟C 源程序(无按键修改功 能)过两天的搜索与调试,在别人程序的基础上,不断修改,终于调试成功了 这个程序。

目前还不能修改时间与日期,只是以预定时间以始。

适用于开发板:51单片机(AT89S52 +带字库液晶12864(ST7920)+DS1302实时时钟)实现功能:简单,数字时钟 +日期(以后会不断完美)。

C 语言源程序如下:#include <reg52.h>#include <intrins.h> #define uchar unsigned char #define uint unsigned int /*DS1302 端 口设置 */sbit SCK=P3A 6;sbit SDA=P3A4;sbit RST = P3A5;bitReadRTC_Flag;/* 12864 端口定义 */ #define LCD_data P0 sbit LCD_RS = P2A4;sbit LCD_RW = P2A5; sbit LCD_EN = P2A 6;//液晶使能控制sbit PSB 二卩2八1; //并口控制 //DS1302 时钟 //DS1302 IO // DS1302 复位 //读DS1302全局变量//带字库液晶 12864数据口// 寄存器选择输入// 液晶读/写控制sbit RES=P2A3;uchar code dis1[] = {"电子设计天地"};// 液晶显示的汉字uchar code dis2[] = {"有志者,事竟成!"};uchar code dis4[] = {'0','1','2','3','4','5','6','7','8','9'};unsigned char temp;#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};void lcd_pos(uchar X,uchar Y); //确定显示位置unsigned char l_tmpdate[7]={0,7,16,19,10,1,9};〃秒分时日月周年09-10-19 16:07:00code unsigned charwrite_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年最低位读写位code unsigned charread_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};void Write_Ds1302_byte(unsigned char temp);void Write_Ds1302( unsigned char address,unsigned char dat );unsigned char Read_Ds1302 ( unsigned char address );void Read_RTC(void);//read RTCvoid Set_RTC(void); //set RTCvoid InitTIMER0(void);//inital timer0/*******************************************************************//**//* 延时函数 *//* *//******************************************************************* /void delay(unsigned int m) // 延时程序 {unsigned int i,j;for(i=0;i<m;i++)**************************************************//*检查LCD 忙状态*//*lcd_busy 为1时,忙,等待。

C51多功能电子时钟

C51多功能电子时钟

~~~~~~目录~~~~~~一、设计任务 (2)二、总体方案设计与方案论证 (3)2.1总体方案设计 (3)2.2方案论证 (4)三、总框图及总体软件设计说明 (7)四、系统资源分配说明 (10)五、局部设计说明 (12)六、系统功能与操作说明 (14)6.1系统功能 (14)6.2 操作说明 (15)七、调试记录及调试结果 (19)八、课程设计总结 (21)九、程序附录清单 (26)一、设计任务整体设计任务:本电多功能子时钟设计首要的工作是结合以往所学的单片机程序编写理论和编写规则来编写电子时钟的软件部分,编写时要结合所配的AT89S52芯片的管脚功能和其他硬件电路,该部分运用keil单片机软件来完成。

在编写完软件并检测完正确后再编译成.hex载入实验板中,并调试。

电子多功能万年历是一种非常广泛日常计时工具,对现代社会越来越流行。

它可以对年、月、日、周日、时、分、秒进行计时,温度显示,还具有闰年补偿,闹铃等多种功能。

对于数字电子万年历采用直观的数字显示,可以循环显示年、月、日、周日、时、分、秒和温度等信息,还具有时间校准等功能。

该电路采用AT89S52单片机作为核心,功耗小,能在3V的低压工作,电压可选用3~5V电压供电。

二、总体方案设计与方案论证1、总体方案设计根据设计要求,总体方案我划分为如下几个模块:(1)自检模块(2)显示更新子程序(3)显示内容送显示缓冲单元的拆字子程序(4)显示定时切换子程序(5)手动切换进程子程序(6)温度滚动采集存储子程序(7)键盘扫描子程序(8)定时切换设定子程序(9)时钟日期设定子程序(10)无操作处理子程序(11)按键操作子程序(12)温度计算模块(13)星期计算模块(部分蔡勒公式汇编程序)(14)正点报时模块(15)闹铃判断模块(16)消抖动子程序(17)定时中断子程序(17)数码管闪烁模块(18)小数点模块(19)双字节压缩二进制码转BCD码模块以上各个模块进行有效地串接与组合,就可以完成本次多功能电子时钟的设计。

基于51单片机的电子钟C语言程序

基于51单片机的电子钟C语言程序

基于51单片机的电子钟C语言程序#include<reg51.h>#include<absacc.h>#define uchar unsigned char#define uint unsigned int/*七段共阴管显示定义*/uchar codedispcode[ ]={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_0=P1^0; // second 调整定义sbit P1_1=P1^1; //minite调整定义sbit P1_2=P1^2; //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;j<125;j++){;}}}void delay (uint k){uint a,b,c;for(c=k;c>0;c--)for(b=38;b>0;b--)for(a=130;a>0;a--);}/**************************//*时间处理子程序*//**************************/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=dispcode[hour/10]; //显示小时的十位 delay1(4);P2=0xfd;P0=(dispcode[(hour%10)])|0X80; //显示小时的个位 delay1(4);P2=0xfb;P0=dispcode[minite/10]; //显示分的十位 delay1(4);P2=0xf7;P0=(dispcode[minite%10])|0X80; //显示分的个位 delay1(4);P2=0xef;P0=dispcode[seconde/10]; //显示秒的十位delay1(4);P2=0xdf;P0=dispcode[seconde%10]; //显示秒的个位delay1(4);}/*******************************//*键盘扫描子程序*//*******************************/void keyscan (void){if(P1_0==0) //按键1秒的调整{delay(2);if(P1_0==0)//delay(300);{seconde++;if(seconde==60){seconde=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; //注意点。

基于C51单片机的数字可调时钟

基于C51单片机的数字可调时钟

河南机电高等专科学校《C51程序设计》大作业设计题目:数字可调时钟班级:通技091学号:090413128姓名:成绩:2011年11月1 设计任务制作数字可调时钟,要求可以分开调节分、时、年、月、日,能够显示温度。

2电路原理图以下为protel99se画的的原理图3 系统流程图数字可调时钟分以下四个部分构成:显示部分:此次显示采用了动态扫描显示,采用74ls573进行数据锁存。

温度采集:温度采集采用了18b20采集的,18b20转化温度较快,精度高。

时钟:采用普通的ds1302芯片。

数据运算:单片机用普通的8051单片机(12M晶振)。

4 源程序/*******************************数字可调时钟*********************************/ /*******************************by:lhc****************************************/ #include<reg51.h> #define DataPort P0void delayms(unsigned char i); sbit DQ=P1^3; sbit sclk=P1^4;sbit date=P1^5; sbit rst=P1^6;sbit LATCH1=P2^2;//定义锁存使能端口 段锁存 sbit LATCH2=P2^3;// 位锁存unsigned char time[8]={20,11,11,27,11,30,00,7}; //年 月日 时 分 秒 周 unsigned char time1[8],readtemflag;unsigned char code DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 显示段码值0~9unsigned char code WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//分别对应相应的数码管点亮,即位码unsigned char code pingnian[13]={ 0,31,28,31,30,31,30,31,31,30,31,30,31};//平年的月份天数 unsigned char code yunnian[13]= { 0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年的月份天数 unsigned char TempData[8]; //存储显示值的全局变量 void delay(unsigned char i) {while(--i); //us 延时函数}void delayms(unsigned char i) //ms 延时函数 {while(i--) { delay(245); delay(245); } }bit rest(void)//18b20重启函数{ bit k=0; DQ=1; delay(5); DQ=0; delay(150); delay(200); DQ=1; delay(40); k =DQ; delay(25); r eturn(k); }unsigned char read()//18b20读数据函数{ unsigned char i=0; unsigned char dat=0;for(i=0;i<8;i++) {DQ=0; dat>>=1; DQ=1;if(DQ) dat|=0x80; delay(25); }return (dat);}void write(unsigned char dat)//18b20写数据函数{unsigned char i=0; for(i=0;i<8;i++){ DQ = 0; DQ = dat&0x01; delay(25); DQ = 1; dat>>=1;} delay(25);}unsigned int ReadTemperature(void) //读取温度函数{ unsigned char a=0; unsigned int kk=0,b=0;LOOP:if(rest()==0){ write(0xCC); //跳过ROM w rite(0x44); //初始化温度转换delayms(20); rest();TH1=0XFa; TL1=0Xff; write(0xCC); write(0xBE); //读取温度a=read(); b=read(); b<<=8; kk=a+b; return(kk);} else goto LOOP;}void restds1302(void){sclk=0; rst=0; //ds1302重启 }void writebyte(unsigned char addr,unsigned char byte) //写入ds1302一个字节数据{ unsigned char i;rst=1;addr=addr&0xfe;for(i=0;i<8;i++){ date=addr&0x01; sclk=1; sclk=0; addr>>=1;}for(i=0;i<8;i++){ date=byte&0x01; sclk=1; sclk=0; byte>>=1;}rst=0;}unsigned char readbyte(unsigned char addr) //读取一个字节的数据{ unsigned char i,temp;rst=1; addr=addr|0x01;for(i=0;i<8;i++) //读函数{ date=addr&0x01;sclk=1; sclk=0;addr=addr>>1;}for(i=0;i<8;i++){temp=temp>>1;if(date) temp|=0x80;else temp&=0x7f; sclk=1; sclk=0;}rst=0;return temp;}void writetime(void) //调时函数{unsigned char i,tmp;for(i=0;i<8;i++){ //BCD处理tmp=time[i]/10;time1[i]=time[i]%10;time1[i]=time1[i]+tmp*16;}writebyte(0x8e,0x00);//关闭写保护writebyte(0x80,0x80);// 暂停writebyte(0x8c,time1[1]);// 年写入writebyte(0x88,time1[2]);//月写入writebyte(0x86,time1[3]);// 日写入//些时间writebyte(0x84,time1[4]);// 时写入writebyte(0x82,time1[5]);// 分写入writebyte(0x80,time1[6]);// 秒写入writebyte(0x8a,time1[7]);// 周写入//writebyte(0x80,0x00);// 秒写入writebyte(0x8e,0x80);//打开写保护}void readtime(void) //读取时间函数{ unsigned char i,tmp;time1[1]=readbyte(0x8d);// 年读time1[2]=readbyte(0x89);// 月读time1[3]=readbyte(0x87);// 日读// 读时间time1[4]=readbyte(0x85);// 时time1[5]=readbyte(0x83);// 分time1[6]=readbyte(0x81);// 秒time1[7]=readbyte(0x8b);// 周for(i=0;i<8;i++) //BCD处理{ tmp=time1[i]/16;time[i]=time1[i]%16;time[i]=time[i]+tmp*10;}}void Display(unsigned char FirstBit,unsigned char Num) //动态显示函数{ static unsigned char i=0;DataPort=0; //清空数据,防止有交替重影LATCH1=1; //段锁存LATCH1=0;DataPort=WeiMa[i+FirstBit]; //取位码LATCH2=1; //位锁存LATCH2=0;DataPort=TempData[i]; //取显示数据,段码LATCH1=1; //段锁存LATCH1=0; i++;if(i==Num) i=0;}unsigned char key(void) //键盘读取函数{ unsigned char i;if(P3!=0xff){ delay(10); if(P3!=0xff){ i=P3; while(P3!=0xff) ;switch(i){case 0xfe:return 1;break;case 0xfd:return 2;break;case 0xfb:return 3;break;default:return 0;break;}}}return 0;}void T1_rest() //定时器1的初始化函数{TMOD|=0X10;TH1=0XF8;TL1=0X30;EA=1 ;ET1= 1;TR1=1;}void isr0(void) interrupt 3{static unsigned char qq;TR1=0; TH1=0XF8; TL1=0X30;Display(0,8); //送去显示qq++;if(qq==200){ qq=0,readtemflag=1; } TR1=1; }void main(){unsigned char bian=0,k=0;unsigned char num=6;unsigned int h,l,tempp,year;bit nianflag; restds1302(); writetime(); T1_rest();while(1){readtime(); year=time[1]*200;if(year%4==0&&year%100!=0||year%400==0) nianflag=1;else nianflag=0; k=key();if(k!=0){ if(k==1){ bian++;num=6; k=0; }//调节显示的内容if(bian==3) bian=0;if(k==2){ num--;if(num<4) bian=1;if(num>3) bian=0; //选着调节对象分,时,年月日if(num==0) num=6; k=0;}if(k==4&&num!=6) //调节对象(分时年月日)加一{ time[num]++;if(num==5&&time[num]==60) time[num]=0;if(num==4&&time[num]==24) time[num]=0;if(num==3&&nianflag){if(time[3]>yunnian[time[2]]) time[3]=1;}else if(num==3){ if(time[3]>pingnian[time[2]])time[3]=1;}if(num==2&&time[num]==13) time[num]=1;if(num==1&&time[num]==99) time[num]=0; k=0;}if(k==3&&num!=6) //调节对象(分时年月日)减一{ time[num]--;if(time[num]==-1&&num==5) time[num]=59;if(time[num]==-1&&num==4) time[num]=23;if(num==3&&nianflag){ if(time[3]==0) time[3]=yunnian[time[2]]; }else if(num==3){ if(time[3]==0) time[3]=pingnian[time[2]]; }if(time[num]==0&&num==2) time[num]=12;if(time[num]==-1&&num==1) time[num]=99; k=0;}if(nianflag){ if(time[3]>yunnian[time[2]]) time[3]=1;}else { if(time[3]>pingnian[time[2]]) time[3]=1; }writetime();}if(bian==0) //对时,分,秒,显示数据分离处理{TempData[0]=DuanMa[time[4]/10];TempData[1]=DuanMa[time[4]%10];TempData[2]=0x40; //加入"-"TempData[3]=DuanMa[time[5]/10];//分TempData[4]=DuanMa[time[5]%10];TempData[5]=0x40;TempData[6]=DuanMa[time[6]/10];//秒TempData[7]=DuanMa[time[6]%10];if(num!=6){ delayms(30);if(num==4){ TempData[0]=0; TempData[1]=0; delayms(30); }if(num==5){ TempData[3]=0; TempData[4]=0; delayms(30); }}}else if(bian==1) //对年月日的显示数据分离处理{ TempData[0]=DuanMa[time[1]/10]; TempData[1]=DuanMa[time[1]%10];TempData[2]=0x40;//加入"-"TempData[3]=DuanMa[time[2]/10];//月TempData[4]=DuanMa[time[2]%10];TempData[5]=0x40;TempData[6]=DuanMa[time[3]/10];//日TempData[7]=DuanMa[time[3]%10];if(num!=6){ delayms(30);if(num==1){ TempData[0]=0;TempData[1]=0;delayms(30); }if(num==2){ TempData[3]=0;TempData[4]=0;delayms(30); }if(num==3){ TempData[6]=0;TempData[7]=0;delayms(30); }}}else if(bian==2) //对温度和星期的显示数据分离处理{if( readtemflag==1){ tempp=ReadTemperature();readtemflag=0;}if(tempp&0x8000){ TempData[0]=0x40;//负号标志tempp=~tempp; tempp +=1;}elseTempData[0]=0;h=tempp>>4; l=tempp&0x0F; l=l*6/10;//小数近TempData[1]=DuanMa[(h%100)/10]; //十位温度TempData[2]=DuanMa[(h%100)%10]|0x80; //个位温度,带小数点TempData[3]=DuanMa[l];TempData[4]=0x39; TempData[5]=0;TempData[6]=DuanMa[time[7]/10];TempData[7]=DuanMa[time[7]%10];}}}参考文献【1】Brian W.Kernighan,Dennis M.Ritchie.C.程序设计语言.机械工业出版社,机械工业出版社,2004.1.【2】祁伟,杨婷.单片机C51程序设计教程与实验,北京航空航天大学出版社,2006.1. 【3】梅丽凤,郝万新.单片机原理及应用,清华大学出版社,2009.7.【4】18B20数据手册.【5】DS1302数据手册.。

【单片机】c51数字时钟(带年月日显示)

【单片机】c51数字时钟(带年月日显示)

【单片机】c51数字时钟(带年月日显示)显示当前时间:9点58分34秒(第一个零表示闹钟未开启)当前日期:10年4月六日摘要:本设计以单片机为核心,LED数码管动态扫描显示。

采用矩阵式键盘输入能任意修改当前时间日期和设定闹钟时间。

具有显示年月日(区分闰年和二月),闹钟报警和整点报时功能说明系统的功能选择由7个按键完成。

其中P3.0,P3.1分别对应调整当前时间的时和分,P3.2为外部中断0,控制闹钟功能的开启/关闭(开启时数码管第一位显示字母’c’)P3.3用作外部中断1,当前时间的显示与闹铃时间显示切换,闹钟显示时按P3.0,P3.1可进行闹钟时分的设定,此时,led1灯灭。

闹铃时间到切闹钟开关开启时,闹铃响一分钟。

P3.5\P3.6\P3.7对年月日进行调整(第一次按P3.5,就进入了年月日的显示,现在就可对日期进行调整)。

按P3.1回到当前时间的显示状态。

整点到时:报警对应小时的次数。

程序如下:#include<reg51.h>#include<intrins.h>#include<stdio.h>#define uchar unsigned char#define uint unsigned intuchar data keyvalue; //查到的键值uchar data keys; //转换出的数字uchar dis[8];uchar codeseg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,0x40,0x00,0x 39,0xf7};// 0 1 2 3 45 6 7 8 9 - 灭灯 c nsbit led_duan=P2^6; //段选通sbit led_wei=P2^7; //位选通sbit speaker=P2^3; //蜂鸣器sbit minitek=P3^0; //分校正按键sbit hourk=P3^1; //小时校正按键sbit p3_4=P3^4; //sbit yeark=P3^5; //年sbit monthk=P3^6; //月sbit dayk=P3^7; //日uchar data wei,i;bit leap_year; //闰年标志位bit dis_nyr;bit cal_year=1;bit calculate=1; //显示年月日与当前时间切换标志uchar data c_min; //闹钟‘分寄存单元uchar data c_hou; //闹钟、小时寄存单元uchar data second; //秒uchar data minite; //分变量uchar data hour; //小时变量uchar data year,month,day; //定义年月日变量uchar data CNTA;uchar data speaker_num; //蜂鸣次数bit beep; //整点报时标志bit run; //运行标志bit flash; //灭灯标志bit clarm_switch; //闹钟开关标志bit baoshi; //报时开关标志sbit led1=P1^6; // 按键标识指示灯sbit led2=P1^7; // 运行标志指示灯sbit P3_2=P3^2;sbit P3_3=P3^3;uint n,k;/*10微秒级延时*/void delay_10us(uchar n){ do{ _nop_();_nop_();_nop_();_nop_();_nop_();}while(--n);}/***毫秒级延时 ***/void delay_ms(uint n){ do delay_10us(131);while(--n);}/****** 当前时间转换******/clk_to_dis(){dis[0]=second%10;dis[1]=second/10;if(flash)dis[2]=10;else dis[2]=11;dis[3]=minite%10;dis[4]=minite/10;dis[5]=hour%10;dis[6]=hour/10;}/*****定时闹钟显示译码(用于七段码显示)*****/clarm_to_dis(){dis[0]=c_min%10;dis[1]=c_min/10;if(flash)dis[2]=10; //亮灯else dis[2]=11; //灭灯dis[3]=c_hou%10;dis[4]=c_hou/10;dis[5]=10;dis[6]=13;}/***********年月日显示译码************/nyr_to_dis(){dis[0]=day%10;dis[1]=day/10;dis[2]=10; //显示'-'dis[3]=month%10;dis[4]=month/10;dis[5]=10; // '-'dis[6]=year%10;dis[7]=year/10;}/*主函数*/void main(){P2=0xff;P1=0XFF;p3_4=0;run=1;led2=0; //运行指示灯亮led1=1;flash=0x00;dis[2]=10; //第三位显示“-”wei=0x7f; //选通低位i=0;second=21;minite=58;hour=9;CNTA=0x00;year=10;month=4;day=5;clk_to_dis();TMOD=0x11;TH0=15560/256;TL0=15560%256;TH1=0xfc;TL1=0x18;EA=1;PT0=1;EX0=1; //开中断;EX1=1;ET0=1;ET1=1;TR0=1;TR1=1;while(1){while(run==1){ clk_to_dis();if(calculate){if(month==1|month==3|month==5|month==7|month==8|month==10|mont h==12){day++;if(day>31){day=0x01;month++;if(month==13){month=1;year++;cal_year=1;}}led1=0; //指示灯亮}if(month==4|month==6|month==9|month==11){day++;if(day>30){day=0x01;month++;}led1=0; //指示灯亮}if(month==2) { if(leap_year==1){day++;if(day==30)day=1;}else {day++;if(day==29)day=1;}}while(cal_year){if((year+2000)%400==0) leap_year=1; // 被400整除为闰年else if((year+2000)%100==0) leap_year=0; // 不能被400整除能被100整除不是闰年elseif((year+2000)%4==0) leap_year=1; // 不能被400、100整除能被4整除是闰年else leap_year=0;cal_year=0;}calculate=0;led1=1;}while(!minitek){for(n=0;n<1000;n++);if(!minitek==0)break; //延时防抖minite++;second=0x00;led1=0;for(n=0;n<20;n++){speaker=!speaker;delay_10us(50); //蜂鸣器响}if(minite==60)minite=0x00;while(!minitek); //等待键松开led1=1; //显示灯}while(!hourk){for(n=0;n<1000;n++);if(!hourk==0)break;hour++;second=0x00;led1=0;for(n=0;n<30;n++){speaker=!speaker;delay_10us(30); //蜂鸣器响}if(hour==24) hour=0x00;while(!hourk);led1=1;}while(!yeark) //yeark键复用进入年月日调整{for(n=0;n<1000;n++);if(!yeark==0)break;dis_nyr=1;while(!yeark);while(dis_nyr){ nyr_to_dis();while(!yeark){for(n=0;n<1000;n++);if(!yeark==0)break;year++;led1=0;for(n=0;n<30;n++){speaker=!speaker;delay_10us(30); //蜂鸣器响}if((year+2000)%400==0) leap_year=1; // 被400整除为闰年else if((year+2000)%100==0) leap_year=0; // 不能被400整除能被100整除不是闰年elseif((year+2000)%4==0) leap_year=1; // 不能被400、100整除能被4整除是闰年else leap_year=0;while(!yeark);led1=1;}while(!monthk){for(n=0;n<1000;n++);if(!monthk==0)break;month++;if(month==13)month=1;led1=0;for(n=0;n<30;n++){speaker=!speaker;delay_10us(30); //蜂鸣器响}while(!monthk);led1=1;}while(!dayk){for(n=0;n<1000;n++);if(!dayk==0)break;if(month==1|month==3|month==5|month==7|month==8|month== 10|month==12){day++;if(day>31)day=0x01;led1=0; //指示灯亮}if(month==4|month==6|month==9|month==11){day++;if(day>30)day=0x01;led1=0; //指示灯亮}if(month==2) { if(leap_year==1){day++;if(day==30)day=1;}else {day++;if(day==29)day=1;}}led1=0;for(n=0;n<30;n++){speaker=!speaker;delay_10us(30); //蜂鸣器响}while(!dayk);led1=1;}while(!hourk){ for(n=0;n<1000;n++);if(hourk) break;dis_nyr=0;while(!hourk);if(clarm_switch)dis[7]=12; //开启显示 Celse dis[7]=11; //关闭不显示dis[6]=11;clk_to_dis();}}}if(clarm_switch) //闹铃时间到,指示灯闪烁蜂鸣器长响{if(c_min==minite&&c_hou==hour){speaker_num=60;beep=1;while(beep&& clarm_switch){led1=!led1;delay_ms(100);clk_to_dis();}led1=1;beep=0;}}}while(run==0) //闹钟时间设定{ clarm_to_dis();/////////////////////////////////////////////////////////// while(!minitek){for(n=0;n<1000;n++);if(!minitek==0)break; //延时防抖c_min++;led1=0;for(n=0;n<20;n++){speaker=!speaker;delay_10us(50); //蜂鸣器响}if(c_min==60)c_min=0x00;while(!minitek); //等待键松开led1=1; //显示灯}while(!hourk){for(n=0;n<1000;n++);if(!hourk==0)break;c_hou++;led1=0;for(n=0;n<30;n++)speaker=!speaker;delay_10us(30); //蜂鸣器响}if(c_hou==24) c_hou=0x00;while(!hourk);led1=1;}}}}/***************定时器T0中断*****************/timer0() interrupt 1 using 2 //定时器0中断号为1号使用第2组寄存器{TR1=0;TH0=15548/256;TL0=15548%256;CNTA++;if(beep){if(speaker_num%2)speaker=!speaker;if(!speaker_num) beep=0;}if(CNTA==20){ if(speaker_num)speaker_num--; //每秒整点报时次数减一flash=!flash;CNTA=0;second++; //秒加if(second==60){second=0;minite++;if(minite==60){minite=0;hour++;if(hour>7){speaker_num=2*hour; //整点到设定报时次数蜂鸣标志置1beep=1;}if(hour==24)hour=0; calculate=1; //0点到,日期标志加一}}}}TR1=1;}/*********定时器中断T1**********/timer1() interrupt 3 using 3{TH1=0xfc;TL1=0x18;P0=0xff;led_wei=1;led_wei=0;P0=seg[dis[i]];led_duan=1;led_duan=0;P0=wei;led_wei=1;led_wei=0;if(++i==8)i=0;wei=_cror_(wei,1);}/**********************外中断0*******************/int_0() interrupt 0{clarm_switch=!clarm_switch; // 闹钟开关if(clarm_switch)dis[7]=12; // 开启显示开启第一位显示C else dis[7]=11; // 关闭不显示for(k=0;k<40;k++){speaker=!speaker;led1=!led1;delay_ms(5);}led1=1;}/********************外中断1********************/int_1() interrupt 2 //闹钟时间设置/运行转换开关{run=!run;if(run)led2=0;else led2=1;for(k=0;k<90;k++){speaker=!speaker; // 蜂鸣器响led1=!led1; // 指示灯闪烁delay_10us(100);}led1=1;}。

c51高精准时钟c语言实用程序

c51高精准时钟c语言实用程序

//年误差2分钟#include<reg52.h>#include <string.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned intsbit D0=P2^0; //** P0口sbit D1=P2^1; //** P0口sbit D2=P2^2; //** P0口sbit D3=P3^2;sbit D4=P3^4;sbit d0=P2^4;sbit d1=P2^5;sbit d2=P2^6;sbit d3=P2^7;sbit key0=P3^5;sbit key1=P3^6;sbit key2=P3^7;sbit key3=P1^3; //启动备用电源sbit clk1_164 = P3^2;sbit data_164 = P3^1; //数据线sbit clk_164 = P3^0; //时钟线sbit baoshi=P3^0;sbit led=P2^3;sbit led1=P0^3;sbit DQ = P3^3;//ds18B20温度传感器定义#define jump_ROM 0xCC //跳过ROM命令#define start 0x44#define read_EEROM 0xBE //读存储器命令uchar TMPH,TMPL,shi,shi1,shi2,fen,fen1,fen2,miao,miao1,miao2,ymonth,yday,m0,m1,m2,m3, sweek,ylmonth,ylmonth1,ylmonth2,ylday,ylday1,ylday2,num,week,temp,tt,tt1,b;uint yyear,ttm,mn,n;uchar code table[]={0x28,0xf9,0x4c,0x58,0x99,0x1a,0x0a,0xf8,0x08,0x18};uchar code tableweek[]={0,0,3,3,6,1,4,6,2,5,0,3,5};uchar code led_char[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};uchar code led_chardain1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};uchar nbai,nshi,nge,m,mbai,mshi,mge,a,b;uchar code table1[]={0x28,0xf9,0x4c,0x58,0x99,0x1a,0x0a,0xf8,0x08,0x18};uchar code table2[]={0x20,0xf1,0x44,0x50,0x91,0x12,0x02,0xf0,0x00,0x10};//点小数点uchar code temp_table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf};//数据最高位为1时数据表void delay3(uint s){uchar k;while(s--){for(k=0; k<125; k++);}}void init(){TMOD=0x11;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;TH1=(65536-51182)/256;TL1=(65536-51182)%256;ET1=1;TR1=1;yyear=11;ymonth=12;yday=13;shi=18;fen=20;miao=0;P0 = 0xff;P1 = 0xff;clk_164 =0;// delay3(400);mn=50000;}void delay(uint z){uint x,y,n;if(shi<21){if(shi>5)n=20;}elsen=1;for(x=n;x>0;x--)for(y=z;y>0;y--);}void Conversion(bit c,uchar year,uchar month,uchar day);uchar code table_monthdate[]="0.31,59,90,120,151,181,212,243,273,304,334";//当前年每月前的总天数void delay1(uchar z){uchar x,y;for(x=80;x>0;x--)for(y=z;y>0;y--);}//21, 4, 19, 6, 21, 5, 21, 6,22, 6,22, 8, 23, 8, 24, 8, 24, 8, 24, 8,23, 8, 22///9, 6, 11,4, 9, 6, 10,6, 9,7, 9,7, 7, 8, 7, 9, 7, 9, 7, 9, 7,8, 7, 15///上面第一行数据为每月节气对应日期,15减去每月第一个节气,每月第二个节气减去15得第二行///这样每月两个节气对应数据都小于16,每月用一个字节存放,高位存放第一个节气数位存放///第二个节气的数据,可得下表uchar code table_jq[]={0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1901 00x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1902 120x96, 0xA5, 0x87, 0x96, 0x87, 0x87, 0x79, 0x69, 0x69, 0x69, 0x78, 0x78, ///1903 240x86, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x78, 0x87, ///1904 360x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1905 480x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///19060x96, 0xA5, 0x87, 0x96, 0x87, 0x87, 0x79, 0x69, 0x69, 0x69, 0x78, 0x78, ///19070x86, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19080x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///19090x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///19100x96, 0xA5, 0x87, 0x96, 0x87, 0x87, 0x79, 0x69, 0x69, 0x69, 0x78, 0x78, ///19110x86, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19120x95, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///19130x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///19140x96, 0xA5, 0x97, 0x96, 0x97, 0x87, 0x79, 0x79, 0x69, 0x69, 0x78, 0x78, ///19150x96, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///19160x95, 0xB4, 0x96, 0xA6, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x87, ///19170x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x77, ///19180x96, 0xA5, 0x97, 0x96, 0x97, 0x87, 0x79, 0x79, 0x69, 0x69, 0x78, 0x78, ///19190x96, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///19200x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x77, ///1922 0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x69, 0x69, 0x78, 0x78, ///1923 0x96, 0xA5, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///1924 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x87, ///1925 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1926 0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1927 0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///1928 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///1929 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1930 0x96, 0xA4, 0x96, 0x96, 0x97, 0x87, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1931 0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///1932 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///1933 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1934 0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1935 0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///1936 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///1937 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1938 0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1939 0x96, 0xA5, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///1940 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///1941 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1942 0x96, 0xA4, 0x96, 0x96, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1943 0x96, 0xA5, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///1944 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///1945 0x95, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, ///1946 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1947 0x96, 0xA5, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///1948 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x79, 0x78, 0x79, 0x77, 0x87, ///1949 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, ///1950 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x79, 0x79, 0x79, 0x69, 0x78, 0x78, ///1951 0x96, 0xA5, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///1952 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///1953 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x68, 0x78, 0x87, ///1954 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1955 0x96, 0xA5, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///1956 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///1957 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///1958 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1959 0x96, 0xA4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///1960 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///1961 0x96, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///1962 0x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///1963 0x96, 0xA4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19640x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19660x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///19670x96, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19680xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///19690x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19700x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x79, 0x69, 0x78, 0x77, ///19710x96, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19720xA5, 0xB5, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///19730x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19740x96, 0xB4, 0x96, 0xA6, 0x97, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, ///19750x96, 0xA4, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x89, 0x88, 0x78, 0x87, 0x87, ///19760xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///19770x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x78, 0x87, ///19780x96, 0xB4, 0x96, 0xA6, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, ///19790x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19800xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x77, 0x87, ///19810xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///1982 ********* 0x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x78, 0x79, 0x78, 0x69, 0x78, 0x77, ///1983 ********* 0x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, ///19840xA5, 0xB4, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///19850xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///19860x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x79, 0x78, 0x69, 0x78, 0x87, ///19870x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///19880xA5, 0xB4, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19890xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///19900x95, 0xB4, 0x96, 0xA5, 0x86, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19910x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///19920xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19930xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///19940x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x76, 0x78, 0x69, 0x78, 0x87, ///19950x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///19960xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///19970xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///19980x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///19990x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///20000xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///20010xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///20020x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///20030x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///20040xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///20050xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///20060x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x69, 0x78, 0x87, ///20070x96, 0xB4, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, ///20080xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///20100x95, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x78, 0x87, ///20110x96, 0xB4, 0xA5, 0xB5, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, ///2012 ******* 0xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, ///2013*******0xA5, 0xB4, 0xA6, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///2014 ******** 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///2015 ******** 0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x96, ///2016 ******* 0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x86, ///2017*******0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///2018 ******* 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x79, 0x77, 0x87, ///2019 ******* 0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x87, 0x87, 0x87, 0x78, 0x87, 0x96, ///2020 ******* 0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///2021*******0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///2022 ******0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x97, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///2023 ******* 0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x88, 0x87, 0x78, 0x87, 0x96, ///2024 ******** 0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///2025********0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///2026 ******* 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///2027 ******0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, ///2028 ******0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///2029******0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///2030 ****** 0xA5, 0xB4, 0x96, 0xA5, 0x96, 0x96, 0x88, 0x78, 0x78, 0x78, 0x87, 0x87, ///2031 *****0x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, ///2032 *****0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///2033******0xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x78, 0x88, 0x78, 0x87, 0x87, ///20340xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///20350x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, ///20360xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x86, ///20370xA5, 0xB3, 0xA5, 0xA5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///20380xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///20390x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x96, ///20400xA5, 0xC3, 0xA5, 0xB5, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, ///20410xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x88, 0x88, 0x88, 0x78, 0x87, 0x87, ///20420xA5, 0xB4, 0x96, 0xA5, 0xA6, 0x96, 0x88, 0x88, 0x78, 0x78, 0x87, 0x87, ///20430x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x88, 0x87, 0x96, ///20440xA5, 0xC3, 0xA5, 0xB4, 0xA5, 0xA6, 0x87, 0x88, 0x87, 0x78, 0x87, 0x86, ///20450xA5, 0xB3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x88, 0x78, 0x87, 0x87, ///20460x95, 0xB4, 0xA5, 0xB4, 0xA5, 0xA5, 0x97, 0x87, 0x87, 0x88, 0x86, 0x96, ///2048 0xA4, 0xC3, 0xA5, 0xA5, 0xA5, 0xA6, 0x97, 0x87, 0x87, 0x78, 0x87, 0x86, ///2049 0xA5, 0xC3, 0xA5, 0xB5, 0xA6, 0xA6, 0x87, 0x88, 0x78, 0x78, 0x87, 0x87 ///2050};code uchar year_code[597]={0x04,0xAe,0x53, //1901 00x0A,0x57,0x48, //1902 30x55,0x26,0xBd, //1903 60x0d,0x26,0x50, //1904 90x0d,0x95,0x44, //1905 120x46,0xAA,0xB9, //1906 150x05,0x6A,0x4d, //1907 180x09,0xAd,0x42, //1908 210x24,0xAe,0xB6, //19090x04,0xAe,0x4A, //19100x6A,0x4d,0xBe, //19110x0A,0x4d,0x52, //19120x0d,0x25,0x46, //19130x5d,0x52,0xBA, //19140x0B,0x54,0x4e, //19150x0d,0x6A,0x43, //19160x29,0x6d,0x37, //19170x09,0x5B,0x4B, //19180x74,0x9B,0xC1, //19190x04,0x97,0x54, //19200x0A,0x4B,0x48, //19210x5B,0x25,0xBC, //19220x06,0xA5,0x50, //19230x06,0xd4,0x45, //19240x4A,0xdA,0xB8, //19250x02,0xB6,0x4d, //19260x09,0x57,0x42, //19270x24,0x97,0xB7, //19280x04,0x97,0x4A, //19290x66,0x4B,0x3e, //19300x0d,0x4A,0x51, //19310x0e,0xA5,0x46, //19320x56,0xd4,0xBA, //19330x05,0xAd,0x4e, //19340x02,0xB6,0x44, //19350x39,0x37,0x38, //19360x09,0x2e,0x4B, //19370x0C,0x95,0x53, //1939 0x0d,0x4A,0x48, //1940 0x6d,0xA5,0x3B, //1941 0x0B,0x55,0x4f, //1942 0x05,0x6A,0x45, //1943 0x4A,0xAd,0xB9, //1944 0x02,0x5d,0x4d, //1945 0x09,0x2d,0x42, //1946 0x2C,0x95,0xB6, //1947 0x0A,0x95,0x4A, //1948 0x7B,0x4A,0xBd, //1949 0x06,0xCA,0x51, //1950 0x0B,0x55,0x46, //1951 0x55,0x5A,0xBB, //1952 0x04,0xdA,0x4e, //1953 0x0A,0x5B,0x43, //1954 0x35,0x2B,0xB8, //1955 0x05,0x2B,0x4C, //1956 0x8A,0x95,0x3f, //1957 0x0e,0x95,0x52, //1958 0x06,0xAA,0x48, //1959 0x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //1961 0x04,0xB6,0x45, //1962 0x4A,0x57,0x39, //1963 0x0A,0x57,0x4d, //1964 0x05,0x26,0x42, //1965 0x3e,0x93,0x35, //1966 0x0d,0x95,0x49, //1967 0x75,0xAA,0xBe, //1968 0x05,0x6A,0x51, //1969 0x09,0x6d,0x46, //1970 0x54,0xAe,0xBB, //1971 0x04,0xAd,0x4f, //1972 0x0A,0x4d,0x43, //1973 0x4d,0x26,0xB7, //1974 0x0d,0x25,0x4B, //1975 0x8d,0x52,0xBf, //1976 0x0B,0x54,0x52, //1977 0x0B,0x6A,0x47, //1978 0x69,0x6d,0x3C, //1979 0x09,0x5B,0x50, //1980 0x04,0x9B,0x45, //19810x0A,0x4B,0x4d, //19830xAB,0x25,0xC2, //19840x06,0xA5,0x54, //19850x06,0xd4,0x49, //19860x6A,0xdA,0x3d, //19870x0A,0xB6,0x51, //19880x09,0x37,0x46, //19890x54,0x97,0xBB, //19900x04,0x97,0x4f, //19910x06,0x4B,0x44, //19920x36,0xA5,0x37, //19930x0e,0xA5,0x4A, //19940x86,0xB2,0xBf, //19950x05,0xAC,0x53, //19960x0A,0xB6,0x47, //19970x59,0x36,0xBC, //19980x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //20010x0d,0x4A,0x4C, //20020x0d,0xA5,0x41, //20030x25,0xAA,0xB6, //20040x05,0x6A,0x49, //20050x7A,0xAd,0xBd, //20060x02,0x5d,0x52, //20070x09,0x2d,0x47, //20080x5C,0x95,0xBA, //20090x0A,0x95,0x4e, //20100x0B,0x4A,0x43, //20110x4B,0x55,0x37, //20120x0A,0xd5,0x4A, //20130x95,0x5A,0xBf, //20140x04,0xBA,0x53, //20150x0A,0x5B,0x48, //20160x65,0x2B,0xBC, //20170x05,0x2B,0x50, //20180x0A,0x93,0x45, //20190x47,0x4A,0xB9, //20200x06,0xAA,0x4C, //20210x0A,0xd5,0x41, //20220x24,0xdA,0xB6, //20230x04,0xB6,0x4A, //20240x69,0x57,0x3d, //20250x0d,0x26,0x46, //2027 0x5e,0x93,0x3A, //2028 0x0d,0x53,0x4d, //2029 0x05,0xAA,0x43, //2030 0x36,0xB5,0x37, //2031 0x09,0x6d,0x4B, //2032 0xB4,0xAe,0xBf, //2033 0x04,0xAd,0x53, //2034 0x0A,0x4d,0x48, //2035 0x6d,0x25,0xBC, //2036 0x0d,0x25,0x4f, //2037 0x0d,0x52,0x44, //2038 0x5d,0xAA,0x38, //2039 0x0B,0x5A,0x4C, //2040 0x05,0x6d,0x41, //2041 0x24,0xAd,0xB6, //2042 0x04,0x9B,0x4A, //2043 0x7A,0x4B,0xBe, //2044 0x0A,0x4B,0x51, //2045 0x0A,0xA5,0x46, //2046 0x5B,0x52,0xBA, //2047 0x06,0xd2,0x4e, //2048 0x0A,0xdA,0x42, //2049 0x35,0x5B,0x37, //2050 0x09,0x37,0x4B, //2051 0x84,0x97,0xC1, //2052 0x04,0x97,0x53, //2053 0x06,0x4B,0x48, //2054 0x66,0xA5,0x3C, //2055 0x0e,0xA5,0x4f, //2056 0x06,0xB2,0x44, //2057 0x4A,0xB6,0x38, //2058 0x0A,0xAe,0x4C, //2059 0x09,0x2e,0x42, //2060 0x3C,0x97,0x35, //2061 0x0C,0x96,0x49, //2062 0x7d,0x4A,0xBd, //2063 0x0d,0x4A,0x51, //2064 0x0d,0xA5,0x45, //2065 0x55,0xAA,0xBA, //2066 0x05,0x6A,0x4e, //2067 0x0A,0x6d,0x43, //2068 0x45,0x2e,0xB7, //20690x05,0x2d,0x4B, //20700x8A,0x95,0xBf, //20710x0A,0x95,0x53, //20720x0B,0x4A,0x47, //20730x6B,0x55,0x3B, //20740x0A,0xd5,0x4f, //20750x05,0x5A,0x45, //20760x4A,0x5d,0x38, //20770x0A,0x5B,0x4C, //20780x05,0x2B,0x42, //20790x3A,0x93,0xB6, //20800x06,0x93,0x49, //20810x77,0x29,0xBd, //20820x06,0xAA,0x51, //20830x0A,0xd5,0x46, //20840x54,0xdA,0xBA, //20850x04,0xB6,0x4e, //20860x0A,0x57,0x43, //20870x45,0x27,0x38, //20880x0d,0x26,0x4A, //20890x8e,0x93,0x3e, //20900x0d,0x52,0x52, //20910x0d,0xAA,0x47, //20920x66,0xB5,0x3B, //20930x05,0x6d,0x4f, //20940x04,0xAe,0x45, //20950x4A,0x4e,0xB9, //20960x0A,0x4d,0x4C, //20970x0d,0x15,0x41, //20980x2d,0x92,0xB5, //2099};code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};code uint day_code2[3]={0x111,0x130,0x14e};bit c_moon;data uchar year_moon,month_moon,day_moon;bit get_moon_day(uchar month_p,uint table_addr)//函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年){/*子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0*/uchar temp;switch (month_p){case 1:{temp=year_code[table_addr]&0x08;if (temp==0)return(0);else return(1);}case 2:{temp=year_code[table_addr]&0x04;if (temp==0)return(0);else return(1);}case 3:{temp=year_code[table_addr]&0x02;if (temp==0)return(0);else return(1);}case 4:{temp=year_code[table_addr]&0x01;if (temp==0)return(0);else return(1);}case 5:{temp=year_code[table_addr+1]&0x80;if (temp==0) return(0);else return(1);}case 6:{temp=year_code[table_addr+1]&0x40;if (temp==0)return(0);else return(1);}case 7:{temp=year_code[table_addr+1]&0x20;if (temp==0)return(0);else return(1);}case 8:{temp=year_code[table_addr+1]&0x10;if (temp==0)return(0);else return(1);}case 9:{temp=year_code[table_addr+1]&0x08;if (temp==0)return(0);else return(1);}case 10:{temp=year_code[table_addr+1]&0x04;if (temp==0)return(0);else return(1);}case 11:{temp=year_code[table_addr+1]&0x02;if (temp==0)return(0);else return(1);}case 12:{temp=year_code[table_addr+1]&0x01;if (temp==0)return(0);else return(1);}case 13:{temp=year_code[table_addr+2]&0x80;if (temp==0)return(0);else return(1);}}}void Conversion(bit c,uchar year,uchar month,uchar day){ //c=0 为21世纪,c=1 为19世纪输入输出数据均为BCD数据uchar temp1,temp2,temp3,month_p;uint temp4,table_addr;bit flag2,flag_y;/* temp1=year/16; //BCD->hex 先把数据转换为十六进制temp2=year%16;year=temp1*10+temp2;temp1=month/16;temp2=month%16;month=temp1*10+temp2;temp1=day/16;temp2=day%16;day=temp1*10+temp2; *///定位数据表地址if(c==0){table_addr=(year+0x64-1)*0x3;}else {table_addr=(year-1)*0x3;}//定位数据表地址完成//取当年春节所在的公历月份temp1=year_code[table_addr+2]&0x60;temp1=_cror_(temp1,5);//取当年春节所在的公历月份完成//取当年春节所在的公历日temp2=year_code[table_addr+2]&0x1f;//取当年春节所在的公历日完成// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月if(temp1==0x1){temp3=temp2-1;}else{temp3=temp2+0x1f-1;}// 计算当年春年离当年元旦的天数完成//计算公历日离当年元旦的天数,为了减少运算,用了两个表//day_code1[9],day_code2[3]//如果公历月在九月或前,天数会少于0xff,用表day_code1[9],//在九月后,天数大于0xff,用表day_code2[3]//如输入公历日为8月10日,则公历日离元旦天数为day_code1[8-1]+10-1//如输入公历日为11月10日,则公历日离元旦天数为day_code2[11-10]+10-1if (month<10){temp4=day_code1[month-1]+day-1;}else{temp4=day_code2[month-10]+day-1;}if ((month>0x2)&&(year%0x4==0)){ //如果公历月大于2月并且该年的2月为闰月,天数加1temp4+=1;}//计算公历日离当年元旦的天数完成//判断公历日在春节前还是春节后if (temp4>=temp3){ //公历日在春节后或就是春节当日使用下面代码进行运算temp4-=temp3;month=0x1;month_p=0x1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月flag2=get_moon_day(month_p,table_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0flag_y=0;if(flag2==0)temp1=0x1d; //小月29天else temp1=0x1e; //大月30天temp2=year_code[table_addr]&0xf0;temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0则该年无闰月while(temp4>=temp1){temp4-=temp1;month_p+=1;if(month==temp2){flag_y=~flag_y;if(flag_y==0)month+=1;}else month+=1;flag2=get_moon_day(month_p,table_addr);if(flag2==0)temp1=0x1d;else temp1=0x1e;}day=temp4+1;}else{ //公历日在春节前使用下面代码进行运算temp3-=temp4;if (year==0x0){year=0x63;c=1;}else year-=1;table_addr-=0x3;month=0xc;temp2=year_code[table_addr]&0xf0;temp2=_cror_(temp2,4);if (temp2==0)month_p=0xc;else month_p=0xd; ///*month_p为月份指向,如果当年有闰月,一年有十三个月,月指向13,无闰月指向12*/flag_y=0;flag2=get_moon_day(month_p,table_addr);if(flag2==0)temp1=0x1d;else temp1=0x1e;while(temp3>temp1){temp3-=temp1;month_p-=1;if(flag_y==0)month-=1;if(month==temp2)flag_y=~flag_y;flag2=get_moon_day(month_p,table_addr);if(flag2==0)temp1=0x1d;else temp1=0x1e;}day=temp1-temp3+1;}c_moon=c; //HEX->BCD ,运算结束后,把数据转换为BCD数据temp1=year/10;temp1=_crol_(temp1,4);temp2=year%10;year_moon=temp1|temp2;temp1=month/10;temp1=_crol_(temp1,4);temp2=month%10;month_moon=temp1|temp2;temp1=day/10;temp1=_crol_(temp1,4);temp2=day%10;day_moon=temp1|temp2;year_moon=year;month_moon=month;day_moon=day;}void delay2(uint N){int i;for(i=0;i<N;i++);}uchar Reset(void){uchar deceive_ready;delay2(10);DQ=0; //拉低DQ线delay2(29); //延时至少480us~960usDQ=1; //将DQ线设置位逻辑高delay2(3); //延时等待deceive_ready响应deceive_ready=DQ; //采样deceive_ready信号delay2(25); //等待时序结束return(deceive_ready); //有deceive_ready信号时返回0,否则返回1}uchar read_bit(void){uchar i;DQ=0; //拉低DQ线开始时序DQ=1; //升高DQ线for(i=0;i<3;i++); //延时至时序开始15usreturn(DQ); //返回DQ值}void write_bit(uchar bitval){DQ=0; //拉低DQ线开始时序if(bitval==1)DQ=1; //如果写逻辑为高delay2(5); //延时DQ=1; //升高DQ线}/****************************************************************************** /void write_byte(uchar val){uchar i,temp;for(i=0;i<8;i++){temp=val>>i; //将val位右移i位赋值给比temptemp=temp&0x01; //取temp最低位write_bit(temp);delay2(5); //延时至时序结束}}/****************************************************************************** /uchar read_byte(void){uchar i,m,receive_data;m=1;receive_data=0; //初始化for(i=0;i<8;i++){if(read_bit()){receive_data=receive_data+(m<<i);} //每读一位数据据,左移一位delay2(6); //延时至时序结束}return(receive_data); //返回value}void wr_byte(data_temp,n){ uchar i,m;m=temp_table1[n];for(i=0;i<8;i++){if((m&0x80)==0x80)data_164 =1; //164数据线elsedata_164 =0;m<<=1;clk_164 =0; //164时钟线clk_164 =1; //164时钟线}for(i=0;i<8;i++){if(data_temp&0x80)data_164 =1; //164数据线elsedata_164 =0;data_temp<<=1; //clk_164 =0; //164时钟线clk_164 =1; //164时钟线}clk1_164=0;// delay(3);clk1_164=1;}void Get_temp(void){Reset();write_byte(jump_ROM); //发跳过ROM命令write_byte(start); //发启动转换命令Reset();write_byte(jump_ROM); //发跳过ROM命令write_byte(read_EEROM); //发跳过ROM命令TMPL=read_byte(); //读低8位温度值TMPH=read_byte(); //读高8位温度值}void keyskan(){if(m0>0){if(key0==1){temp++;if(temp>8)temp=0;m0=0;tt1=0;}}if(temp==1){if(m1>0){if(key1==1){miao++;if(miao==60)miao=0;m1=0;tt1=0;}}if(m2>0){if(key2==1){miao--;if(miao==(-1))miao=60;m2=0;tt1=0;}}}if(temp==2){if(m1>0){if(key1==1){fen++;if(fen==60)fen=0;m1=0;tt1=0;}}if(m2>0){if(key2==1){fen--;if(fen==(-1))fen=60;m2=0;tt1=0;}}}if(temp==3){if(m1>0){if(key1==1){shi++;if(shi==24)shi=0;m1=0;tt1=0;}}if(m2>0){if(key2==1){shi--;if(shi==(-1))shi=23;m2=0;tt1=0;}}}if(temp==4){if(m1>0){if(key1==1){yyear++;if(yyear>99)yyear=0;m1=0;tt1=0;}}if(m2>0){if(key2==1){yyear--;if(yyear==(-1))yyear=99;m2=0;tt1=0;}}}if(temp==5){if(m1>0){if(key1==1){ymonth++;if(ymonth>12)ymonth=1;m1=0;tt1=0;}}if(m2>0){if(key2==1){ymonth--;if(ymonth<1)ymonth=12;m2=0;tt1=0;}}}if(temp==6){if(m1>0){if(key1==1){yday++;if(ymonth==1){if(yday>31)yday=1;}if(ymonth==2){if(yyear%4==0){if(yday>29)yday=1;}if(yyear%4!=0){if(yday>28)yday=1;}}if(ymonth==3){if(yday>31)yday=1;}if(ymonth==4) {if(yday>30)yday=1;}if(ymonth==5) {if(yday>31)yday=1;}if(ymonth==6) {if(yday>30)yday=1;}if(ymonth==7) {if(yday>31)yday=1;}if(ymonth==8) {if(yday>31)yday=1;}if(ymonth==9) {if(yday>30)yday=1;}if(ymonth==10) {if(yday>31)yday=1;}if(ymonth==11) {if(yday>30)yday=1;}if(ymonth==12) {。

DS1302时钟芯片C51源代码源程序

DS1302时钟芯片C51源代码源程序

DS1302时钟芯片C51源代码源程序DS1302--- 51C语言代码,头文件与对应C代码已分离开,直接可用(自己写,绝对可用。

)其中星期几能直接由日期按公式推算出来,time.week=(time.year/100)/4-2*(time.year/100)+(time.year%100)+(time.year%100)/4+26*(tim e.month+1)/10 +time.day-1;(详情请搜:蔡乐公式)///////////////////////////////////////////////////////////////DS1302头文件:#ifndef _ds1302_h_#define _ds1302_h_#define uchar unsigned char#define uint unsigned int#define ulong unsigned long#define u32 unsigned long intsbit sdinn = P2^6;sbit sclkk = P2^5;sbit cee = P2^7;#define second_w 0x80#define second_r 0x81#define minute_w 0x82#define minute_r 0x83#define hour_w 0x84#define hour_r 0x85#define day_w 0x86#define day_r 0x87#define month_w 0x88#define month_r 0x89#define year_w 0x8c#define year_r 0x8d#define week_w 0x8a#define week_r 0x8b#define protect_w 0xc0#define protect_r 0xc1#define clock_base_w 0xc2#define clock_base_r 0xc3#define clock_state_w 0xcc#define clock_state_r 0xcd#define D_protect 0x77typedef struct{uint year;uint month;uint day;uint hour;uint minute;uint second;uint week;} Time_info;void DS1302_Writebyte(uchar dat);void time_write2DS1302(Time_info Time);void time_read_from_DS1302(Time_info *Time);uchar DS1302_Readbyte();void DS1302Write(uchar cmd, uchar dat);uchar DS1302Read(uchar cmd);void Init_DS1302(void);void display_date(unsigned char X,unsigned char Y,Time_info Read_T); void writeDate2DS1302(Time_info Time);void time_HMS2DS1302(Time_info Time);void Alarm_respond();void sendtime2computer();#endif///////////////////////////////////////////////////////////DS1302-C代码:#include "option.h"xdata uchar trans_buf;extern xdata uchar Date_Index;extern xdata uchar Time_Index;extern xdata uchar Write_date_buff[15];extern xdata uchar Alarm_buff[7];extern xdata uchar Alarm_arrive;extern xdata uchar arriveminute;extern xdata uchar Alarm_state;extern xdata Time_info Read_Time;xdata uchar tiao_buff[30];xdata uint hour,minute,second;xdata uchar old_second=250;void delay(unsigned long int i){while(i--);}void time_write2DS1302(Time_info Time)DS1302Write(0x8e,0x00);//写保护关trans_buf = ((Time.year%100/10)<<4)+((Time.year%10)&0X0F); // 写入年DS1302Write(year_w, trans_buf);trans_buf = (((Time.month%100/10)<<4)&0x10) + ((Time.month%10)&0X0F); //写入月DS1302Write(month_w, trans_buf);trans_buf = (((Time.day%100/10)<<4)&0x30) + ((Time.day%10)&0X0F);//写入日DS1302Write(day_w, trans_buf);trans_buf = (((Time.hour%100/10)<<4)&0x30)+((Time.hour%10)&0X0F);//写入时DS1302Write(hour_w, trans_buf);trans_buf = (((Time.minute%100/10)<<4)&0x70) + ((Time.minute%10)&0X0F); //写入分DS1302Write(minute_w, trans_buf);trans_buf = (((Time.second%100/10)<<4)&0x70) + ((Time.second%10)&0X0F); //写入秒DS1302Write(second_w, trans_buf);trans_buf = Time.week%7; //写入星期DS1302Write(week_w, trans_buf);DS1302Write(0x8e,0x80); //开写保护}void writeDate2DS1302(Time_info Time){DS1302Write(0x8e,0x00);//写保护关trans_buf = ((Time.year%100/10)<<4)+((Time.year%10)&0X0F); // 写入年DS1302Write(year_w, trans_buf);trans_buf = (((Time.month%100/10)<<4)&0x10) +((Time.month%10)&0X0F); //写入月DS1302Write(month_w, trans_buf);trans_buf = (((Time.day%100/10)<<4)&0x30) + ((Time.day%10)&0X0F);//写入日DS1302Write(day_w, trans_buf);Time.week = (int)(Time.year/100)/4-(int)2*(Time.year/100)+(int)(Time.year%100)+(int)(Time.year%100)/4+(int)26*(Time.mo nth+1)/10+(int)Time.day-1;// printf("--week:%5d--",Time.week);Time.week %= 7;if(Time.week == 0) Time.week = 7;trans_buf = Time.week; //写入星期DS1302Write(week_w, trans_buf);DS1302Write(0x8e,0x80); //开写保护}void time_HMS2DS1302(Time_info Time){DS1302Write(0x8e,0x00);//写保护关trans_buf = (((Time.hour%100/10)<<4)&0x30)+((Time.hour%10)&0X0F);//写入时DS1302Write(hour_w, trans_buf);trans_buf = (((Time.minute%100/10)<<4)&0x70) + ((Time.minute%10)&0X0F); //写入分DS1302Write(minute_w, trans_buf);trans_buf = (((Time.second%100/10)<<4)&0x70) + ((Time.second%10)&0X0F); //写入秒DS1302Write(second_w, trans_buf);DS1302Write(0x8e,0x80); //开写保护}void time_read_from_DS1302(Time_info *Time){Time->year = DS1302Read(year_r);Time->month = DS1302Read(month_r);Time->day = DS1302Read(day_r);Time->hour = DS1302Read(hour_r);Time->minute = DS1302Read(minute_r);Time->second = DS1302Read(second_r);Time->week = DS1302Read(week_r);Time->year = 2000+((Time->year>>4)*10)+(Time->year&0x0f);Time->month = ((Time->month>>4)*10) + (Time->month&0x0f);Time->day = (Time->day>>4)*10 + (Time->day&0x0f);Time->hour = (Time->hour>>4)*10 + (Time->hour&0x0f);Time->minute = ((Time->minute>>4)*10) + (Time->minute&0x0f);Time->second = (Time->second>>4)*10 + (Time->second&0x0f); }void DS1302_Writebyte(uchar dat){uchar i;sclkk = 0;delay(300);for(i = 0; i < 8; i++){sdinn = dat & 0x01;delay(90);sclkk = 1;delay(180);sclkk = 0;dat >>= 1;}}uchar DS1302_Readbyte(){uchar i, dat = 0;delay(90);for(i = 0; i < 8; i++){dat >>=1;if(sdinn)dat |= 0x80;sclkk = 1;delay(60);sclkk = 0;delay(60);}sdinn = 0;return dat;}void DS1302Write(uchar cmd, uchar dat) {cee = 0;sclkk = 0;cee = 1;DS1302_Writebyte(cmd);DS1302_Writebyte(dat);sclkk = 1;cee = 0;}uchar DS1302Read(uchar cmd) {uchar dat = 0;cee = 0;sclkk = 0;cee = 1;DS1302_Writebyte(cmd);dat = DS1302_Readbyte();sclkk = 1;cee = 0;return dat;}void Init_DS1302(void){Time_info time;uchar init_state;DS1302Write(0x8e,0x00);//写保护关init_state = DS1302Read(protect_r); DS1302Write(0x8e,0x80); //开写保护if(init_state != D_protect){time.year = 2014;time.month = 12;time.day = 5;time.hour = 18;time.minute = 57;time.second = 50; //日期换算为星期[c/4]-2c+y+[y/4]+[26(m+1)/10]+d-1time.week = (time.year/100)/4-2*(time.year/100)+(time.year %100)+(time.year%100)/4+26*(time.month+1)/10+time.da y-1;time.week %= 7;if(time.week == 0) time.week = 7;time_write2DS1302(time);DS1302Write(0x8e,0x00);//写保护关DS1302Write(protect_w,D_protect); //初始化一次标示DS1302Write(0x8e,0x80); //开写保护}DS1302Write(0x8e,0x00);//写保护关Alarm_buff[0] = DS1302Read(clock_base_r+0);Alarm_buff[1] = DS1302Read(clock_base_r+2);Alarm_buff[2] = DS1302Read(clock_base_r+4);Alarm_buff[3] = DS1302Read(clock_base_r+6);Alarm_buff[4] = DS1302Read(clock_base_r+8);Alarm_buff[5] = DS1302Read(clock_base_r+10);Alarm_state = DS1302Read(clock_state_r);DS1302Write(0x8e,0x80); //开写保护}void display_date(unsigned char X,unsigned char Y,Time_info Read_T) {xdata char dis_info[12];sprintf(dis_info, "%04d/%02d/%02d", (int)Read_T.year, (int) Read_T.month, (int)Read_T.day);LCD_write_english_string(X,Y,dis_info);sprintf(dis_info, "%02d/%02d/%02d", (int)Read_T.hour, (int) Read_T.minute, (int)Read_T.second);LCD_write_english_string(X+6,Y+1,dis_info);sprintf(dis_info, "Week:%d", (int)Read_T.week);LCD_write_english_string(X+12,Y+2,dis_info);}void Alarm_respond(){hour = (uint)Alarm_buff[0]*10+Alarm_buff[1];minute = (uint)Alarm_buff[2]*10+Alarm_buff[3];second = (uint)Alarm_buff[4]*10+Alarm_buff[5];if(Alarm_state==1){time_read_from_DS1302(&Read_Time);if((old_second < Read_Time.second)||(Read_Time.second==0)){if(arriveminute!=Read_Time.minute) Alarm_arrive = 0;if((Read_Time.hour == hour)&&(Read_Time.minute== minute)&&(Read_Time.second == second)){Alarm_arrive = 1;arriveminute = Read_Time.minute;}}old_second = Read_Time.second;}}void sendtime2computer(){time_read_from_DS1302(&Read_Time);printf("time%04d/%02d/%02d/%02d:%02d:%02d", Read_Time.year, Read_Time.month, Read_Time.day, Read_Time.hour, Read_Time.minute,Read_Time.second);printf("%d--\n",Read_Time.week);}。

完整51单片机多功能电子钟程序代码

完整51单片机多功能电子钟程序代码

完整51单片机多功能电子钟程序代码#include "regx52.h"#define uint8 unsigned charcode uint8 LED[] = {0xc0, 0xf9, 0xa4, 0xb0,0x99, 0x92, 0x82, 0xf8,0x80, 0x90, 0x88, 0x83,0xc6, 0xa1, 0x86, 0x8e}; unsigned char riqi[8]; static uint8 keyReleas;uint8 disptmp;uint8 pos;uint8 buffer[8];code uint8 BIT[8] = {0xfe, 0xfd, 0xfb, 0xf7,0xef, 0xdf, 0xbf, 0x7f}; uint8 flag2ms;uint8 t2msCnt;uint8 halfSecCnt;uint8 IsTimerRun;uint8 mode;void GlobalvarInit();void T0Init();void TimeRun();unsigned char KeyScan();void Command();void Command();void T0IntSvr() interrupt 1 using 1{TL0 = (65536-2000)%256;TH0 = (65536-2000)/256;switch(mode){case 0:disptmp=LED[buffer[pos]]; switch(pos){case 2:case 4:case 6:disptmp&=~(1<<7); break;}break;case 1:disptmp=LED[riqi[pos]]; switch(pos){case 2:case 4:disptmp&=~(1<<7); break;}break;}P2 = 0xff;P0 = disptmp;P2 = BIT[pos];pos++;if(pos>7)pos = 0;flag2ms = 1;}void main(){GlobalvarInit();T0Init();EA = 1;while(1){TimeRun();Command();}}void TimeRun(){if(IsTimerRun == 0) return; if(flag2ms != 1) return; flag2ms = 0;t2msCnt++;if(t2msCnt < 250) return; t2msCnt = 0; halfSecCnt++;if(halfSecCnt < 2) return; halfSecCnt = 0;buffer[0]++;if(buffer[0] < 10) return; buffer[0] = 0;buffer[1]++;if(buffer[1] < 6) return; buffer[1] = 0;buffer[2]++;if(buffer[2] < 10) return;buffer[2] = 0;buffer[3]++;if(buffer[3] < 6) return;buffer[3] = 0;buffer[4]++;switch(buffer[5]){case 0:case 1:if(buffer[4] < 10) return; buffer[4] = 0;break; case 2: if(buffer[4] < 4) return; buffer[4] = 0;break; }buffer[5]++;if(buffer[5] < 3) return;buffer[5] = 0;riqi[0]++;switch(riqi[1]){case 0:if( riqi[0] < 10) return; riqi[0] = 1;break; case 1: case 2:if( riqi[0] < 10) return; riqi[0] = 0;break; case 3:if( riqi[0] < 2) return; riqi[0] = 0;break;}riqi[1]++;if(riqi[1]<4) return;riqi[1]=0;riqi[2]++;switch(riqi[3]){case 0:if( riqi[2] < 10) return; riqi[2] = 1;break;case 1:if( riqi[2] < 3) return; riqi[2] = 0;break; }riqi[3]++;if(riqi[3]<1) return;riqi[3]=0;riqi[4]++;if(riqi[4]<10) return;riqi[4]=0;riqi[5]++;if(riqi[5]<10) return;riqi[5]=0;riqi[6]++;if(riqi[6]<10) return;riqi[6]=0;riqi[7]++;if(riqi[7]<10) return;riqi[7]=0;}void T0Init(){TMOD = TMOD & 0xf0 | 0x01;// 2msTL0 = (65536-2000)%256;TH0 = (65536-2000)/256;ET0 = 1;TR0 = 1;}// 全局变量初始化void GlobalvarInit(){uint8 i;for (i = 0; i < 8; i++){buffer[i] = 0;}pos = 1;flag2ms = 0;t2msCnt = 0; IsTimerRun = 1;mode = 0;riqi[0]= 0;riqi[1]= 2;riqi[2]=6;riqi[3]=0;riqi[4]=3;riqi[5]=1;riqi[6]=0;riqi[7]=2;}unsigned char KeyScan() { unsigned char key;P3 = 0x0f;if ((P3 & 0x0f) == 0x0f) {return 0;}P3 = 0x7f;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0x70);}P3 = 0xbf;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xb0);}P3 = 0xdf;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xd0);}P3 = 0xef;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xe0);}return 0;}static uint8 keyRelease = 1; void Key_bd(){buffer[7] = 0x0b; buffer[6] = 0x0d; }void Key_be() {switch(mode) {case 0:mode = 1; break;case 1:mode = 0; break;}}void Key_0x77() {if(buffer[4]>3) {buffer[5]++;if(buffer[5]>1) buffer[5]=0;}else{buffer[5]++;if(buffer[5]>2) buffer[5]=0;}}void Key_0x7b(){switch(buffer[5]){case 0:case 1:buffer[4]++;if(buffer[4]>9)buffer[4]=0; break;case 2:buffer[4]++;if(buffer[4]>3)buffer[4]=0;break; }}void Key_0xee(){switch(riqi[1]){case 0: riqi[0]++;if(riqi[0]>9) riqi[0]=1;break;case 1:case 2:riqi[0]++;if(riqi[0]>9) riqi[0]=0;break;case 3:riqi[0]++;if(riqi[0]>1)riqi[0]=0;break;}}void Key_0xeb() {switch(riqi[3]){case 0:riqi[2]++;if(riqi[2]>9) riqi[2]=1; break;case 1:riqi[2]++;if(riqi[2]>2) riqi[2]=0; break;}}void Key_0xed() {if(riqi[0]<=1){riqi[1]++;if(riqi[1]>3)riqi[1]=0;}if(riqi[0]>1){riqi[1]++;if(riqi[1]>2)riqi[1]=0;}}void Key_0xe7(){if(riqi[2]>2)riqi[3]=0;else{riqi[3]++;if(riqi[3]>1)riqi[3]=0;}}void Command(){uint8 key;key = KeyScan();if (key == 0){keyRelease = 1;return;}if(keyRelease == 0)return;keyRelease = 0;switch(key){case 0xbe: Key_be(); break; // Key_8 case 0xbd: Key_bd(); break; // Key_9 case 0xbb: IsTimerRun = 1; break;// Key_A case 0xb7: IsTimerRun = 0; break;// Key_Bcase 0x77: Key_0x77();break;// Key_Fcase 0x7b: Key_0x7b();break;// Key_Ecase 0x7d: buffer[3]++;if(buffer[3]>5)buffer[3]=0;;break;// Key_D case 0x7e: buffer[2]++;if(buffer[2]>9)buffer[2]=0;;break;// Key_C case 0xde: riqi[4]++;if(riqi[4]>9)riqi[4]=0; break;// Key_4 case 0xdd: riqi[5]++;if(riqi[5]++>9)riqi[5]=0; break;// Key_5 case 0xdb: riqi[6]++;if(riqi[6]>9)riqi[6]=0; break;// Key_6case 0xd7: riqi[7]++;if(riqi[7]>9)riqi[7]=0; break;// Key_7case 0xee: Key_0xee(); break;// Key_0case 0xed: Key_0xed();break;// Key_1case 0xeb: Key_0xeb(); break;// Key_2case 0xe7: Key_0xe7();break;// Key_3}}。

8位数码管显示电子时钟c51单片机程序

8位数码管显示电子时钟c51单片机程序

8位数码管显示电子时钟c51单片机程序时间:2012-09-10 13:52:26 来源:作者:/*8位数码管显示时间格式 05—50—00 标示05点50分00秒S1 用于小时加1操作S2 用于小时减1操作S3 用于分钟加1操作S4 用于分钟减1操作*/#include<reg52.h>sbit KEY1=P3^0; //定义端口参数sbit KEY2=P3^1;sbit KEY3=P3^2;sbit KEY4=P3^3;sbit LED=P1^2; //定义指示灯参数code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0—9unsigned char StrTab[8]; //定义缓冲区unsigned char minute=19,hour=23,second; //定义并初始化为 12:30:00void delay(unsigned int cnt){while(--cnt);}/******************************************************************//* 显示处理函数*//******************************************************************/void Displaypro(void){StrTab[0]=tab[hour/10]; //显示小时StrTab[1]=tab[hour%10];StrTab[2]=0x40; //显示"-"StrTab[3]=tab[minute/10]; //显示分钟StrTab[4]=tab[minute%10];StrTab[5]=0x40; //显示"-"StrTab[6]=tab[second/10]; //显示秒StrTab[7]=tab[second%10];}main(){TMOD |=0x01; //定时器0 10ms inM crystal 用于计时TH0=0xd8; //初值TL0=0xf0;ET0=1;TR0=1;TMOD |=0x10; //定时器1用于动态扫描TH1=0xF8; //初值TL1=0xf0;ET1=1;TR1=1;EA =1;Displaypro(); //调用显示处理函数while(1){if(!KEY1) //按键1去抖以及动作{delay(10000);if(!KEY1){hour++;if(hour==24)hour=0; //正常时间小时加1 Displaypro();}}if(!KEY2) //按键2去抖以及动作{delay(10000);if(!KEY2){hour--;if(hour==255)hour=23; //正常时间小时减1 Displaypro();}}if(!KEY3) //按键去抖以及动作{delay(10000);if(!KEY3){minute++;if(minute==60)minute=0; //分加1Displaypro();}}if(!KEY4) //按键去抖以及动作{delay(10000);if(!KEY4){minute--;if(minute==255)minute=59; //分减1Displaypro();}}}}/******************************************************************//* 定时器1中断 */ /******************************************************************/void time1_isr(void) interrupt 3 using 0 //定时器1用来动态扫描{static unsigned char num;TH1=0xF8; //重入初值TL1=0xf0;switch (num){case 0:P2=0;P0=StrTab[num];break; //分别调用缓冲区的值进行扫描 case 1:P2=1;P0=StrTab[num];break;case 2:P2=2;P0=StrTab[num];break;case 3:P2=3;P0=StrTab[num];break;case 4:P2=4;P0=StrTab[num];break;case 5:P2=5;P0=StrTab[num];break;case 6:P2=6;P0=StrTab[num];break;case 7:P2=7;P0=StrTab[num];break;default:break;}num++; //扫描8次,使用8个数码管if(num==8)num=0;}/******************************************************************//* 定时器0中断 */ /******************************************************************/void tim(void) interrupt 1 using 1{static unsigned char count; //定义内部局部变量TH0=0xd8; //重新赋值TL0=0xf0;count++;switch (count){case 0:case 20:case 40:case 60:case 80:Displaypro();break; //隔一定时间调用显示处理case 50:P1=~P1;break; //半秒 LED 闪烁default:break;}if (count==100){count=0;second++; //秒加1if(second==60){second=0;minute++; //分加1if(minute==60){minute=0;hour++; //时加1if(hour==24)hour=0;}}}}基于单片机的LCD1602控制总线模式时间:2012-09-10 13:50:39 来源:作者:第一行显示"Welcome";第二行显示="Happy day";若要显示其他字符,请直接往数组LCMLineOne[16]和LCMLineTwo[16]填充相应的代码。

51单片机数码管电子时钟C程序

51单片机数码管电子时钟C程序

//**单片机stc89c52, 8位共阴数码管12M晶振//*******P0 位选,P2 段选❖******//#include 〃reg52・ h〃#define uchar unsigned char#define uint unsigned intuchar code tab[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40, Oxff}; uchar n;uchar hh, mm, ss;uchar nhh, nmm, nss;uint year;uchar day, mon, week;uchar hhs, hhg, mms, mmg, sss, ssg;uchar days,dayg, mons, mong;uchar nhhs, nhhg, nmms, nmmg, nsss, nssg;uchar setl=l, set2=l;sbit dula=P3 3;sbit fm=P3 2;sbit kl二P3"4;sbit k2二P3"5;sbit k3二P3飞;sbit k4二P3"7;uchar tablel[] = {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 〃闰年uchar table2[] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //非闰年void jishi ();void baoshi ();void alarm();void set_time();void set_alarm();void set_mdw();void key_change():void key_set ();void delay (int m) 〃延时程序,延时m*0. 5msuint i;uint j;for (i=0;i<m;i++){for(j=0;j<500;j++){}}}void timerO() interrupt 1 {TMOD二0x01;TH0=0x3c;TLO=Oxbl;n++;JishiO;}main()TMOD二0x01;TH0=0x3c;TLO二Oxbl;TRO二1;EA二1;ETO二1;hh=12;mm=00;ss=00;nhh二7;nmm=30;nss=0;year=2012;mon=01;day=01;week=7; while(1){hhs=hh/10%10; // 时分秒hhg=hh%10;mms=mm/10%10;mmg=mm%10;sss=ss/10%10;ssg=ss%10;nhhs=nhh/10%10; 〃闹钟nhhg=nhh%10; nmms=nmm/10%10;nmmg=nmm%10; nsss=nss/10%10;nssg=nss%10; days=day/10%10;//月Hday萨day%10; mons=mon/10%10;mong=mon%10;key_change () ; //kl 按键扫描key_set (); //k2 按键扫描set_time() ; //设置时间set.mdwO ; //设置月日星期set_alarm() ; //设置闹钟辻(setl-1) //正常走时显示dula=l: P2=tab [hhs] ; dula=O: P2=0xff; PO=Oxef; delay (1) ;P0二Oxff; //时十位dula二1;P2二tab[hhg]; dula=0;P2=0xff;PO=Oxdf;delay(1) ; PO二Oxff;//时个位dula=l;P2=tab[10]:dula=0;P2=0xff; PO=Oxbf;delay(1); PO二Oxffdula=l: P2=tab [mms] : dula=0: P2 二Oxff ;P0 二0x7f; delay (1) ;P0 二Oxff; //分十位dula=l; P2=tab [mmg] ; dula=0; P2=0xf f; P0=0xfe; delay (1) ; PO 二Oxff;//分个位dula=l;P2=tab[10]; dula=0:P2=0xff;PO=Oxfd:delay(1); PO二Oxffdula二1;P2二tab[sss]; dula=0;P2=0xff;PO=Oxfb:delay(1) ; PO二Oxff;//秒十位dula二1;P2二tablssg]; dula=0:P2=0xff;P0=0xf7; delay(1) ;P0二Oxff; //秒个位}辻(set 1=2) //设置时间dula=l; P2=tab [hhs] ; dula=0; P2=Oxff; PO=Oxef; delay (1) ;P0二Oxff; //时十位dula二1;P2二tablhhg]; dula=0:P2=0xff;PO=Oxdf;delay(1) ; PO二Oxff;//时个位dula=l:P2=tab[10]:dula=0;P2=0xff; PO=Oxbf;delay(1); PO二Oxffdula=l: P2=tab [mms] : dula=0: P2 二Oxff ;P0 二0x7f; delay (1) ;P0 二Oxff; //分十位dula=l; P2=tab [mmg] ; dula=0: P2=0xf f: P0=0xfe; delay (1) ; PO 二Oxff;//分个位dula=l;P2=tab[10]; dula=0:P2=0xff;PO=Oxfd:delay(1); PO二Oxffdula二1;P2二tab[sss]; dula=0;P2=0xff;PO=Oxfb:delay(1) ; PO二Oxff;//秒十位dula二1;P2二tablssg]; dula=0:P2=0xff;P0=0xf7; delay(1) ;P0二Oxff; //秒个位}辻(set 1-3) //正常显示月日-星期(肆匕诂S )M心T P 鏗\m x o u o 匕(DAPWZJXOUOddxouzdoudnp 二席*〕qpf Zd二 H d n p 创+忌、、d x o u o d - (I )A p w q J x o u o d d x o u z d o H £n p =s 〕q p f z d =%-n p 、、—、、、、QJXOUOd - (I)xp^p&Jxouod2Jxouzdo%w 〔OI 〕qeTzd=%-np 心T p g 'm x o u o d - u)APW*xouoddxouzd oUPTnp 二"App 〕qpfzd二%-np^+、、dxouocr(DAP^PQZXOUOmxouzdou d n p 二 SAPP 〕qpfzd 二%-np 、、—、、、、QJXOUOdJI) A32P cu q x o u o d 世o %I n p 二2〕q e F CN d 二%H T P 鏗\m x o u o 匕(DAPWZJXOUOddxouzdoudnp 二席*〕qpf Zd二 H d n p 创+忌、、d x o u o d - (I )A p w q J x o u o d d x o u z d o H £n p =s 〕q p f z d =%-n p 、、—、、、、心 J x o u o d - (I)xp^p&Jxouod2Jxouzdo%w 〔OI 〕qeTzd=%-np 心T p g 'm x o u o d - u)APW*xouoddxouzd oUPTnp 二"App 〕qpfzd 二%-np ^+0、、d x ouocr(DAP^PQZXOUOmxouzdoudnp二SAPP〕qpfzd二%-np 、、—、、、、QJXOUOd J I ) A32Pcu q x o u o d £0 世dula=l; P2=tab [nhhs]; dula=O; P2二Oxff; PO二Oxef; delay (1) ; PO二Oxff; 〃时十位dula=l;P2=tab[nhhg]; dula=0;P2=0xff;PO=Oxdf; delay (1) ; PO二Oxff;//时个位dula=l;P2=tab[10];dula=0;P2=0xff; PO=Oxbf;delay(1); PO二Oxff dula=l:P2=tab [nmms] : dula=0; P2=0xff ;P0=0x7f; delay (1) ;PO=Oxff; //分十位dula=l;P2=tab[nmmg] :dula=0;P2=0xff;PO=Oxfe; delay (1) ; PO=Oxff;//分个位dula=l;P2=tab[10]; dula=0:P2=0xff;PO=Oxfd:delay(1) ; PO二Oxffdula=l ;P2=tab[nsss] : dula=0;P2=0xff ;PO=Oxfb; delay (1) ; PO二Oxff;//秒十位dula=l;P2=tab[nssg] ; dula=0: P2=0xff; PO=Oxf7; delay (1) ;PO=Oxff; //秒个位} if(setl—6) //设置闹钟定时dula=l; P2=tab [nhhs] ; dula=0; P2二Oxff; PO二Oxef; delay (1) ; PO二Oxff; 〃时十位dula=l;P2=tab[nhhg]; dula=0;P2=0xff;PO=Oxdf; delay (1) ; PO二Oxff;//时个位dula=l;P2=tab[10];dula=0;P2=0xff; PO=Oxbf;delay(1); PO二Oxff dula=l:P2=tab [nmms] : dula=0; P2=0xff ;P0=0x7f; delay (1) ;PO=Oxff; //分十位dula=l;P2=tab[nmmg] :dula=0;P2=0xff;PO=Oxfe;delay(1) ; PO二Oxff;//分个位dula=l;P2=tab[10]; dula=0;P2=0xff;PO=Oxfd:delay(1); PO二Oxff dula=l;P2=tab[nsss]; dula二0;P2二Oxff;P0二Oxfb;delay(l) ; PO二Oxff;//秒十位dula=l;P2=tab[nssg] ; dula=0: P2=0xff; PO=Oxf7; delay (1) ;P0二Oxff; //秒个位}baoshi () : //整点报时alarm () ; //闹钟}}void JishiO 〃计时函数辻(n二二20){n=0;ss++;辻(ss=60){ss二0;mm++;if(mm二二60){mm^O;hh++;辻(hh=24){hh二0;day++;week++;if(week==8){week二0;}if (year%4==0&&year%100! =0 year%400==0) //闰年if(day=二tablel[mon]+l){day=0;mon++;if(mon==13){mon二0;year++;}}else //非闰年{辻(day—table2 [mon]+l){day=0;mon++;辻(mon—13){mon二0;year++;}}}}}}}}uchar incone (uchar n) //加 1 函数{辻(k3=0){delay(20);if(k3==0){n++;while(!k3);}}return(n);}uchar decone(uchar m) //减 1 函数{ if (k4==0){delay(20);if(k4==0)辻(m<0){m 二0;}}}}void key_change() //kl 按键扫描{ if(kl==0){delay(20);if(kl==0){setl++;while(!kl);if(setl==7){setl二1;}}void key_set() //k2 按键扫描{if(k2==0){delay(20);{set2++;while(!k2);辻(set2=4){set2=l;}}}}void baoshi () //整点报时函数{ fm=O;}if(ss==l){fm=l;}}void alarm() //闹钟函数{uchar x;if(nhh==0){goto end;}if(hh==nhh&&mm二二nmm&&ss==nss)fm=O; delay(30); fm=l; delay(20); fm=0; delay(30); fm=l; delay(20); fm=0; delay(30); fm=l; delay(20); fm=0; delay(30); fm=l; delay (180);end:;}void set_time() //设置时间函数{辻(setl==2){辻(set2==l){hh=incone(hh);if(hh==24){hh=0;}if(hh==-l){hh二23;}hh二decone(hh);}mm=incone(mm); if(mm==60) {mm 二0;}if(mm==-l) {mm=59;}mm=decone(mm); }辻(set2=3){ss=incone(ss);辻(ss=60){ss=O;}if (ss—-1){ss=59;}ss二decone (ss);}void set_mdw() //设置月日星期函数{ 辻(setl==4) {辻(set2==l){mon=incone(mon);if(mon二二13){mon=l;}mon=decone(mon);if(mon==0){mon二12;}}if(set2==2)day=incone(day);if(day==32){day=l;} day=decone(day);if (day—0){day=31;}}辻(set2=3){week=incone(week);if(week==8){week二1;}week二decone(week);if(week==0){week=7;}}}void set_alarm() //设置闹钟函数{ if(setl二二6){辻(set2=l){nhh二incone(nhh);辻(nhh二二24) {nhh二0;}辻(nhh—-1){nhh二23;}nhh二decone(nhh); }if(set2==2){nmm二incone(nmm); if (nmm—60) {nmm二0;}nmm二decone(nmm); if (nmm 二二 T){nmm=59;}}辻(set2二二3) {nss=incone (nss); if (nss=60){nss=0;}nss二decone(nss); if(nss==-l){nss二59;}}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
EA=1;//开总中断
/*设置计数器0提供时钟脉冲*/
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
/*设置计数器1提供音乐所需的频率*/
TH1=0;
TL1=0;
ET1=1;
TR1=0;
/*初始化LCD */
write_com(0x38);
putIntToString(2,value/10);
write_data('.');
putIntToString(1,value%10);
write_data(0xdf);
write_data('C');
}
void warning(int value){
write_com(0x01);//清屏
putStringtoLcd(0x80+0x00,"Happy Each Hour");
{ int h_carry=0;//初始时无进位,同时相当于关掉进位
int m_carry=0;
int s_carry=0;
int mo_carry=0;
int d_carry=0;
if(sc==60)
{ sc=0;
s_carry=1;
}
if(s_carry)
{ if(mc==59)
{ mc=0;
m_carry=1;
while(*p!='\0')
{write_data(*p);
p++;
}
}
void scan_key_function()//校时键扫描
{ if(!key_alarm)//在设置闹钟界面下,校时键无效
if(!key1)
{ delay(10);
if(!key1);
{ TR0=!TR0;
if(key_function)//是校时界面,则退出校时界面,并将当前时钟值设置为实际时钟值
value/=10;
}
while(n++,n<num);
while(n--)
write_data(buf[n]);
}
void putStringtoLcd(char addr,char* p)//将字符串送到LCD指定的地方显示
{
write_com(0x0c);//关光标显示
write_com(addr);
}
}
/*负责时钟计时*/
void time0() interrupt 1
{ TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
if(num>=20)
{ sc++;
num=0;
time_cal();
if(!key_alarm&&!music&&!iswarning)
}
/*主函数*/
int main(){
init();
putIntToLcd();
putTemp(wendu=get_temp());
delay(2000);
while(1)
{ scan_key_function();
sacn_key_alarm();
if(key_function)
{scan_key();showGuangBiao(wei);}
{ y=yc;mo=moc;d=dc;h=hc;m=mc;s=sc;
putIntToLcd();
putTemp(wendu=get_temp());
}
istone=1;
}
}
/*负责产生音频信号*/
void time1() interrupt 3
{ TH1=timeh;
TL1=timel;
beep=~beep;
}
/*led.c
实现对LCD1602操作的各函数*/
#include"led.h"
void delay(uint z)//延时z ms
{ uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_data(uchar date)//写数据
{
lcdrs=1;
/*led.h
负责声明全局变量
*/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
/*各数据在LCD中显示的地址*/
#define Year 0x80+0x00
#define Month 0x80+0x05
#define Day 0x80+0x08
tone(sc-52);
istone=0;
}
if(mc==0&&sc==0)
{
iswarning=1;
if(istone==1)
tone(8);
istone=0;
warning(wendu);
}
if(iswarning==1&&sc>5)
{ iswarning=0;
write_com(0x01);
}
int key_alarm=0;//功能键标志
int num=0;//计数变量
int idata wendu;
int iswarning;
int istone;
/*当数据段空间不够时,可用idata修饰全局变量*/
int idata s,m,h,y=2013,mo=1,d=12;//显示在LED上的年月日时分秒变量
else if(key_alarm)
{ scan_key();showGuangBiao(wei);}
if((hc==h_alarm)&&(mc==m_alarm)&&(sc==0))
{ music=1;
song();
}
if(mc==59&&sc>=53&&sc<=59)
{
if(istone==1)
putIntToString(4,y);
write_data('-');
write_com(Month);
putIntToString(2,mo);
write_data('-');
write_com(Day);
putIntToString(2,d);
}
/*方法计算出年月日时分秒*/
void time_cal()
extern uchar timeh,timel;
/*******全局变量声明******/
/*******public方法声明****/
void time_cal();
void write_data(uchar date);
void write_com(uchar com);
void init(void);
void scan_key();
void add(int value);
void mius(int value);
void showGuangBiao(int value);
void song();
void delay_music(uint z);
void sacn_key_alarm();
void putStringtoLcd(char addr,char* p);
P0=date;
lcden=1;
delay(2);
lcden=0;
}
void write_com(uchar com)//写命令
{
lcdrs=0;
P0=com;
lcden=1;
delay(2);
lcden=0;
}
void init()//初始化
{
lcden=0;//LCD初始化使能端无效
TMOD=0x11;//设置计数器0,1均为工作方式1
void tempchange();
uint get_temp();
void putTemp(uint value);
void tone(int value);
/*******public方法声明****/
/*main.c
主功能函数
*/
#include"led.h"
int key_function=0;//功能键标志
int idata sc,mc,hc,yc=2013,moc=1,dc=12;//实际年月日时分秒变量
int idata date[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};//平年与闰年每个月的天数
{ dc=1;
d_carry=1;}
else
dc++;
}
if(d_carry)
{ if(moc==12)
{moc=1;
mo_carry=1;}
else moc++;
}
if(mo_carry)
yc++;
}
void putTemp(uint value){
tempchange();
write_com(Weidu);
void delay(uint z);
相关文档
最新文档