51单片机c语言电子万年历完整程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

该程序为51单片机c语言电子万年历#includereg52.h//#includestdio.h#defineucharunsignedchar#defineuintunsignedintsbitlcden=P3^4;sbitlcdrs=P3^5;sbitDATA=P0^7;sbitRST=P0^5;sbitSCLK=P0^6;sbitmenu=P3^0;//菜单sbitadd=P3^1;//加一sbitdec=P3^7;//减一sbitled0=P1^0;sbitled1=P1^1;sbitled2=P1^2;sbitled3=P1^3;sbitds=P3^2;//sbitbeep=P3^3;uinttemp;floatf_temp;uintwarn_l1=270;uintwarn_l2=250;uintwarn_h1=300;uintwarn_h2=320;uintget_temp();voiddelayms(uintx);voidwrite_com(ucharcom);voidwrite_data(uchardate);voidinit();voiddis_temp(uintt);voidWrite1302(uchardat);voidWriteSet1302(ucharCmd,uchardat);ucharRead1302(void);ucharReadSet1302(ucharCmd);voidInit_DS1302(void);voidDisplaySecond(ucharx);voidDisplayMinute(ucharx);voidDisplayHour(ucharx);voidDisplayDay(ucharx);voidDisplayMonth(ucharx);voidDisplayYear(ucharx);voidDisplayWeek(ucharx);voiddis_temp(uintt);voidread_date(void);voidturn_val(charnewval,ucharflag,ucharnewaddr,uchars1num);voidkey_scan(void);charcodetable[]=0123456789;ucharcodetable2[]=TUEWESTHUFRISATSUNMON;ucharsecond,minute,hour,day,month,year,week,count=0;ucharReadValue,num,time;voiddelayms(uintx){uinti,j;for(i=x;i0;i--)for(j=110;j0;j--);}////////////////////////////////////////////////////////////voidwrite_com(ucharcom){lcdrs=0;P2=com;delayms(5);lcden=1;delayms(5);lcden=0;}voidwrite_data(uchardate){lcdrs=1;P2=date;delayms(5);lcden=1;delayms(5);lcden=0;}voidinit(){lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}/////////////////////////////////////////////////////////////////voidWrite1302(uchardat){uchari;SCLK=0;//拉低SCLK,为脉冲上升沿写入数据做好准备delayms(2);//稍微等待,使硬件做好准备for(i=0;i8;i++)//连续写8个二进制位数据{DATA=dat&0x01;//取出dat的第0位数据写入1302delayms(2);//稍微等待,使硬件做好准备SCLK=1;//上升沿写入数据delayms(2);//稍微等待,使硬件做好准备SCLK=0;//重新拉低SCLK,形成脉冲dat=1;//将dat的各数据位右移1位,准备写入下一个数据位}}voidWriteSet1302(ucharCmd,uchardat){RST=0;//禁止数据传递SCLK=0;//确保写数居前SCLK被拉低RST=1;//启动数据传输delayms(2);//稍微等待,使硬件做好准备Write1302(Cmd);//写入命令字Write1302(dat);//写数据SCLK=1;//将时钟电平置于已知状态RST=0;//禁止数据传递}ucharRead1302(void){uchari,dat;delayms(2);//稍微等待,使硬件做好准备for(i=0;i8;i++)//连续读8个二进制位数据{dat=1;//将dat的各数据位右移1位,因为先读出的是字节的最低位if(DATA==1)//如果读出的数据是1dat|=0x80;//将1取出,写在dat的最高位SCLK=1;//将SCLK置于高电平,为下降沿读出delayms(2);//稍微等待SCLK=0;//拉低SCLK,形成脉冲下降沿delayms(2);//稍微等待}returndat;//将读出的数据返回}ucharReadSet1302(ucharCmd){uchardat;RST=0;//拉低RSTSCLK=0;//确保写数居前SCLK被拉低RST=1;//启动数据传输Write1302(Cmd);//写入命令字dat=Read1302();//读出数据SCLK=1;//将时钟电平置于已知状态RST=0;//禁止数据传递returndat;//将读出的数据返回}voidInit_DS1302(void){WriteSet1302(0x8E,0x00);//根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)4|(0%10)));//根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)4|(59%10)));//根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)4|(23%10)));//根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((28/10)4|(28%10)));//根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((2/10)4|(2%10)));//根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((14/10)4|(14%10)));//nian//WriteSet1302(0x8a,((4/10)4|(4%10)));}/////////////////////////////////////////////////////////////////voidDisplaySecond(ucharx){uchari,j;i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);write_com(0x80+0x48);write_data('');dis_temp(get_temp());}voidDisSecond(ucharx){uchari,j;ReadValue=ReadSet1302(0x81);second=((ReadValue&0x70)4)*10+(ReadValue&0x0F);i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);}voidDisplayMinute(ucharx){uchari,j;i=x/10;j=x%10;write_com(0x80+0x43);write_data(i+0x30);write_com(0x80+0x44);write_data(j+0x30);write_com(0x80+0x45);write_data(':');}voidDisplayHour(ucharx){uchari,j;i=x/10;j=x%10;write_com(0x80+0x40);write_data(i+0x30);write_com(0x80+0x41);write_data(j+0x30);write_com(0x80+0x42);write_data(':');}voidDisplayDay(ucharx){uchari,j;i=x/10;j=x%10;write_com(0x89);write_data(i+0x30);write_com(0x8a);write_data(j+0x30);}voidDisplayMonth(ucharx){uchari,j;i=x/10;j=x%10;write_com(0x86);write_data(i+0x30);write_com(0x87);write_data(j+0x30);write_com(0x88);write_data('/');}voidDisplayYear(ucharx){uchari,j;i=x/10;j=x%10;write_com(0x81);write_data(2+0x30);write_com(0x82);write_data(0+0x30);write_com(0x83);write_data(i+0x30);write_com(0x84);write_data(j+0x30);write_com(0x85);write_data('/');}voidDisplayWeek(ucharx){uchari;x=x*3;//write_com(0x8c);write_data(table2[x]);//write_com(0x8d);write_data(table2[x+1]);//write_com(0x8e);write_data(table2[x+2]);write_com(0x8c);for(i=0;i3;i++){write_data(table2[x]);x++;}}voidread_date(void){ReadValue=ReadSet1302(0x81);second=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=ReadSet1302(0x83);minute=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=ReadSet1302(0x85);hour=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=ReadSet1302(0x87);day=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=ReadSet1302(0x89);month=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=ReadSet1302(0x8d);year=((ReadValue&0x70)4)*10+(ReadValue&0x0F);ReadValue=ReadSet1302(0x8b);//读星期week=ReadValue&0x07;DisplayYear(year);DisplayMonth(month);DisplayDay(day);DisplayWeek(week);DisplayHour(hour);DisplayMinute(min

1 / 26
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功