完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
是基于AT89C52单片机的电子时钟设计,显示年月日 星期 时间 可设置闹钟,能显示温度!!!下边是程序,程序是别人给的,谁鉴定下呢,我是新手,不懂。。。。。
#include #include #define uint unsigned int #define uchar unsigned char ////////时钟芯片控制接口/////////////////////// ***it sck=P1^0; ***it io=P1^1; ***it rst=P1^2; ///////1602液晶控制接口///////////////// ***it rs=P2^6; ***it en=P2^7; ////////按键//////////////////////////// ***it key1=P3^3; ***it key2=P3^4; ***it key3=P3^5; ***it key4=P3^6; /////////////////////////////////////// ***it beep=P2^0;//蜂鸣器接口 ***it DQ=P2^1;//温度传感器接口 uint wen_du; ////////////////////////// uchar writetime[7]; //////////////时钟读写地址///////////////////////////////// uchar writeadd[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; uchar readadd[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; uchar nian,zhou,yue,ri,shi,fen,miao;//时间暂存变量 uchar shi1,fen1;//闹钟暂存变量 uchar date_flag;//调整年月日时分秒 uchar alarm_flag;//调整闹钟 uchar num,cc;//闹钟提示音次数 uchar num1;//整点报时 void delay(uchar ms)//延时 { uchar x,y; for(x=ms;x>0;x--) for(y=110;y>0;y--); } /***********ds18b20延迟子函数(晶振12MHz )*******/ void delay_18B20(uint i) { while(i--); } /**********ds18b20初始化函数**********************/ void Init_DS18B20() { uchar x=0; DQ=1; //DQ复位 delay_18B20(8); //稍做延时 DQ=0; //单片机将DQ拉低 delay_18B20(80); //精确延时 大于 480us DQ=1; //拉高总线 delay_18B20(14); x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败 delay_18B20(20); } /***********ds18b20读一个字节**************/ uchar ReadOneChar() { uchar i=0; uchar dat = 0; for (i=8;i>0;i--) { DQ = 0; // 给脉冲信号 dat>>=1; DQ = 1; // 给脉冲信号 if(DQ) dat|=0x80; delay_18B20(4); } return(dat); } /*************ds18b20写一个字节****************/ void WriteOneChar(uchar dat) { uchar i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; delay_18B20(5); DQ = 1; dat>>=1; } } /**************读取ds18b20当前温度************/ void deal_with() { uint temp1,temp2; uchar a=0; uchar b=0; uchar t=0; Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 delay_18B20(100); // this message is wery important Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 delay_18B20(100); a=ReadOneChar(); //读取温度值低位 b=ReadOneChar(); //读取温度值高位 temp1=b<<4; temp1+=(a&0xf0)>>4; temp2=a&0x0f; wen_du=((b*256+a)>>4)*10; //当前采集温度值除16得实际温度值 } //////////////////////////////////////////////////////// void ds1302_write_byte(uchar date)//ds1302写字节 { uchar i; for(i=0;i<8;i++) { sck=0; io=date&0x01; date=date>>1; sck=1; } } void ds1302_write(uchar add,uchar date)//向ds1302的地址里面写数据 { rst=0;_nop_(); sck=0;_nop_(); rst=1;_nop_(); ds1302_write_byte(add); ds1302_write_byte(date); rst=0;_nop_(); sck=0;_nop_(); io=1;sck=1; } uchar ds1302_read(uchar add) //从ds1302的地址里面读出数据 { uchar i,date; rst=0;_nop_(); sck=0;_nop_(); rst=1;_nop_(); ds1302_write_byte(add); for(i=0;i<8;i++) { date=date>>1; sck=0; if(io) date=date|0x80; sck=1; } rst=0;_nop_(); sck=0;_nop_(); io=1;sck=1; return date; } void yj1602_write_com(uchar com) //液晶写地址 { en=0;delay(1); en=1;delay(1); rs=0;delay(1); P0=com; en=0;delay(1); } void yj1602_write_date(uchar date) //液晶写数据 { en=0;delay(1); en=1;delay(1); rs=1;delay(1); P0=date; en=0;delay(1); } void yj1602_init() //液晶初始化 { yj1602_write_com(0x38); yj1602_write_com(0x0c); yj1602_write_com(0x06); yj1602_write_com(0x01); } void write_1602(uchar add,uchar date)//时钟读取数据液晶显示转换 { uchar shi,ge; shi=date/16; ge=date%16; yj1602_write_com(0x80+add); yj1602_write_date(0x30+shi); //0x30码制转换 yj1602_write_date(0x30+ge); } void write_alarm(uchar add,uchar date)//闹钟数据液晶显示转换 { uchar shi,ge; shi=date/10; ge=date%10; yj1602_write_com(0x80+add); yj1602_write_date(0x30+shi); //0x30码制转换 yj1602_write_date(0x30+ge); } void write_1602_tem(uchar add,uint date)//环境温度数据液晶显示转换 { uchar bai,shi,ge,dian; bai=date/1000; shi=date%1000/100; ge=date%100/10; dian=date%10; yj1602_write_com(0x80+add); if(bai==1)yj1602_write_date(0x30+bai); yj1602_write_date(0x30+shi); //0x30码制转换 yj1602_write_date(0x30+ge); yj1602_write_date('.'); yj1602_write_date(0x30+dian); yj1602_write_date(0Xdf); yj1602_write_date('C'); } void display() //显示年月日星期时分秒 温度 { yj1602_write_com(0x80); yj1602_write_date('2'); yj1602_write_date('0'); writetime[0]=ds1302_read(readadd[0]); write_1602(2,writetime[0]); yj1602_write_date('-'); writetime[2]=ds1302_read(readadd[2]); write_1602(5,writetime[2]); yj1602_write_date('-'); writetime[3]=ds1302_read(readadd[3]); write_1602(8,writetime[3]); writetime[1]=ds1302_read(readadd[1]); if(writetime[1]==1) { yj1602_write_com(0x80+12); yj1602_write_date('M'); yj1602_write_date('O'); yj1602_write_date('N'); } if(writetime[1]==2) { yj1602_write_com(0x80+12); yj1602_write_date('T'); yj1602_write_date('U'); yj1602_write_date('E'); } if(writetime[1]==3) { yj1602_write_com(0x80+12); yj1602_write_date('W'); yj1602_write_date('E'); yj1602_write_date('D'); } if(writetime[1]==4) { yj1602_write_com(0x80+12); yj1602_write_date('T'); yj1602_write_date('H'); yj1602_write_date('U'); } if(writetime[1]==5) { yj1602_write_com(0x80+12); yj1602_write_date('F'); yj1602_write_date('R'); yj1602_write_date('I'); } if(writetime[1]==6) { yj1602_write_com(0x80+12); yj1602_write_date('S'); yj1602_write_date('A'); yj1602_write_date('T'); } if(writetime[1]==7) { yj1602_write_com(0x80+12); yj1602_write_date('S'); yj1602_write_date('U'); yj1602_write_date('N'); } writetime[4]=ds1302_read(readadd[4]); write_1602(0x41,writetime[4]); yj1602_write_date(':'); writetime[5]=ds1302_read(readadd[5]); write_1602(0x44,writetime[5]); yj1602_write_date(':'); writetime[6]=ds1302_read(readadd[6]); write_1602(0x47,writetime[6]); write_1602_tem(0x4a,wen_du); } void set_display()//a设置闹钟时的显示函数 { yj1602_write_com(0x80); yj1602_write_date('S'); yj1602_write_date('e'); yj1602_write_date('t'); yj1602_write_date('-'); yj1602_write_date('a'); yj1602_write_date('l'); yj1602_write_date('a'); yj1602_write_date('r'); yj1602_write_date('m'); yj1602_write_date('-'); yj1602_write_date('c'); yj1602_write_date('l'); yj1602_write_date('o'); yj1602_write_date('c'); yj1602_write_date('k'); write_alarm(0x44,shi1); yj1602_write_date(':'); write_alarm(0x47,fen1); yj1602_write_date(':'); yj1602_write_date('0'); yj1602_write_date('0'); } void tui_chu()//退出时间调整 { if(key4==0) { delay(10); if(key4==0) { date_flag=0; yj1602_write_com(0x82); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x85); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x88); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x8c); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+0x41); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+0x44); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+0x47); yj1602_write_date('*'); yj1602_write_date('*'); ds1302_write(0x8e,0x80); } while(!key4); } } void didi() //滴滴声 { beep=0;delay(500); beep=1;delay(500); } void keyscan() //按键扫描控制 { uchar j,k; if(key1==0) { yj1602_init(); delay(10); if(key1==0) { didi(); //滴滴声 display(); ds1302_write(0x8e,0x00); date_flag=1; yj1602_write_com(0x82); yj1602_write_date('*');//调整年 yj1602_write_date('*'); nian=ds1302_read(readadd[0]); j=nian/16; k=nian%16; nian=j*10+k; } while(!key1); } ////////////////////////调整年/////////////////////////////////////// while(date_flag==1) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 date_flag=2; yj1602_write_com(0x80+5); yj1602_write_date('*');//调整月 yj1602_write_date('*'); yue=ds1302_read(readadd[2]); j=yue/16; k=yue%16; yue=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 nian++; yj1602_write_com(0x82); yj1602_write_date('*'); yj1602_write_date('*'); if(nian>=100) //年 nian=0; j=nian/10; k=nian%10; writetime[0]=j*16+k; ds1302_write(writeadd[0],writetime[0]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(nian==0) nian=100; yj1602_write_com(0x82); yj1602_write_date('*'); yj1602_write_date('*'); nian--; j=nian/10; k=nian%10; writetime[0]=j*16+k; ds1302_write(writeadd[0],writetime[0]); } while(!key3); } } ///////////// 调整月/////////////////////////////////////////////// while(date_flag==2) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 date_flag=3; yj1602_write_com(0x88); yj1602_write_date('*');//调整日 yj1602_write_date('*'); ri=ds1302_read(readadd[3]); j=ri/16; k=ri%16; ri=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 yj1602_write_com(0x85); yj1602_write_date('*'); yj1602_write_date('*'); yue++; if(yue>=13) yue=1; j=yue/10; k=yue%10; writetime[2]=j*16+k; ds1302_write(writeadd[2],writetime[2]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(yue<=1) yue=13; yj1602_write_com(0x85); yj1602_write_date('*'); yj1602_write_date('*'); yue--; j=yue/10; k=yue%10; writetime[2]=j*16+k; ds1302_write(writeadd[2],writetime[2]); } while(!key3); } } ///////////// 调整日/////////////////////////////////////////////// while(date_flag==3) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 date_flag=4; yj1602_write_com(0x80+12); yj1602_write_date('*');//调整星期 yj1602_write_date('*'); yj1602_write_date('*'); shi=ds1302_read(readadd[1]); j=zhou/16; k=zhou%16; zhou=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 yj1602_write_com(0x88); yj1602_write_date('*'); yj1602_write_date('*'); ri++; if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12)) { if(ri==32) ri=1; } if(yue==2) { if(nian%4==0) { if(ri==30) ri=1; } else { if(ri==29) ri=1; } } if((yue==4)||(yue==6)||(yue==9)||(yue==11)) { if(ri==31) ri=1; } j=ri/10; k=ri%10; writetime[3]=j*16+k; ds1302_write(writeadd[3],writetime[3]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12)) { if(ri==1) ri=32; } if(yue==2) { if(nian%4==0) { if(ri==1) ri=30; } else { if(ri==1) ri=29; } } if((yue==4)||(yue==6)||(yue==9)||(yue==11)) { if(ri==1) ri=31; } yj1602_write_com(0x88); yj1602_write_date('*'); yj1602_write_date('*'); ri--; j=ri/10; k=ri%10; writetime[3]=j*16+k; ds1302_write(writeadd[3],writetime[3]); } while(!key3); } } ///////////////调整周////////////////////////////////////////////////////// while(date_flag==4) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 date_flag=5; yj1602_write_com(0x80+0x40+1); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); shi=ds1302_read(readadd[4]); j=shi/16; k=shi%16; shi=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 yj1602_write_com(0x80+12); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_date('*'); zhou++; if(zhou==8)zhou=1; j=zhou/10; k=zhou%10; writetime[1]=j*16+k; ds1302_write(writeadd[1],writetime[1]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(shi==0)zhou=7; yj1602_write_com(0x80+12); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_date('*'); zhou--; if(zhou==0)zhou=7; j=zhou/10; k=zhou%10; writetime[1]=j*16+k; ds1302_write(writeadd[1],writetime[1]); } while(!key3); } } ///////////// 调整时/////////////////////////////////////////////// while(date_flag==5) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 date_flag=6; yj1602_write_com(0x80+0x44); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); fen=ds1302_read(readadd[5]); j=fen/16; k=fen%16; fen=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 yj1602_write_com(0x80+0x41); yj1602_write_date('*'); yj1602_write_date('*'); shi++; if(shi==24)shi=0; j=shi/10; k=shi%10; writetime[4]=j*16+k; ds1302_write(writeadd[4],writetime[4]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(shi==0)shi=24; yj1602_write_com(0x80+0x41); yj1602_write_date('*'); yj1602_write_date('*'); shi--; j=shi/10; k=shi%10; writetime[4]=j*16+k; ds1302_write(writeadd[4],writetime[4]); } while(!key3); } } ///////////// 调整分/////////////////////////////////////////////// while(date_flag==6) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 date_flag=7; yj1602_write_com(0x80+0x47); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); miao=ds1302_read(readadd[6]); j=miao/16; k=miao%16; miao=j*10+k; } while(!key1);yj1602_init(); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 yj1602_write_com(0x80+0x44); yj1602_write_date('*'); yj1602_write_date('*'); fen++; if(fen==60)fen=0; j=fen/10; k=fen%10; writetime[5]=j*16+k; ds1302_write(writeadd[5],writetime[5]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(fen==0)fen=60; yj1602_write_com(0x80+0x44); yj1602_write_date('*'); yj1602_write_date('*'); fen--; j=fen/10; k=fen%10; writetime[5]=j*16+k; ds1302_write(writeadd[5],writetime[5]); } while(!key3); } } ///////////// 调整秒/////////////////////////////////////////////// while(date_flag==7) { display(); tui_chu();//退出时间调整 if(key1==0) { delay(10); if(key1==0) { didi();//滴滴声 didi();//滴滴声 date_flag=0; ds1302_write(0x8e,0x80); } while(!key1);yj1602_init(); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 yj1602_write_com(0x80+0x47); yj1602_write_date('*'); yj1602_write_date('*'); miao++; if(miao==60)miao=0; j=miao/10; k=miao%10; writetime[6]=j*16+k; ds1302_write(writeadd[6],writetime[6]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(fen==0)fen=60; yj1602_write_com(0x80+0x47); yj1602_write_date('*'); yj1602_write_date('*'); miao--; j=miao/10; k=miao%10; writetime[6]=j*16+k; ds1302_write(writeadd[6],writetime[6]); } while(!key3); } } if(key4==0) { yj1602_init(); delay(10); if(key4==0) { didi();//滴滴声 set_display(); alarm_flag=1; yj1602_write_com(0x80+0x44); yj1602_write_date('*');//调整闹钟 yj1602_write_date('*'); } while(!key4); } while(alarm_flag==1) { set_display(); if(key4==0) { delay(10); if(key4==0) { didi();//滴滴声 set_display(); alarm_flag=2; yj1602_write_com(0x80+0x47); yj1602_write_date('*');//调整闹钟 yj1602_write_date('*'); } while(!key4); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 shi1++; yj1602_write_com(0x80+0x44); yj1602_write_date('*');//调整闹钟 yj1602_write_date('*'); if(shi1==24)shi1=0; } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(shi1==0)shi1=24; yj1602_write_com(0x80+0x44); yj1602_write_date('*');//调整闹钟 yj1602_write_date('*'); shi1--; } while(!key3); } } while(alarm_flag==2) { set_display(); if(key4==0) { delay(10); if(key4==0) { didi();//滴滴声 didi();//滴滴声 set_display(); alarm_flag=0; yj1602_init(); } while(!key4); } if(key2==0) { delay(10); if(key2==0) { didi();//滴滴声 fen1++; yj1602_write_com(0x80+0x47); yj1602_write_date('*');//调整闹钟 yj1602_write_date('*'); if(fen1==60)fen1=0; } while(!key2); } if(key3==0) { delay(10); if(key3==0) { didi();//滴滴声 if(fen1==0)fen1=60; yj1602_write_com(0x80+0x47); yj1602_write_date('*');//调整闹钟 yj1602_write_date('*'); fen1--; } while(!key3); } } } void nao_zhong()//闹钟 { uchar j,k; uchar shi_da,fen_da,miao_da; shi_da=ds1302_read(readadd[4]); j=shi_da/16; k=shi_da%16; shi_da=j*10+k; fen_da=ds1302_read(readadd[5]); j=fen_da/16; k=fen_da%16; fen_da=j*10+k; miao_da=ds1302_read(readadd[6]); j=miao_da/16; k=miao_da%16; miao_da=j*10+k; if((shi1!=0)||(fen1!=0)) { if((shi_da==shi1)&&(fen_da==fen1)&&(miao_da==0))//如果到达设定时间,开启定时器报警20秒 { TR0=1; num=0; beep=0; } } if((7<=shi_da)&&(shi_da<=20)) //七点到晚上八点 { if((fen_da==0)&&(miao_da<=1))//整点报时,蜂鸣器提示音 { TR0=1; num=0; beep=0; } } } void main() { yj1602_init(); TH0=(65536-50000)/256; TL0=(65536-50000)%256; TMOD=0x01; EA=1; ET0=1; TR0=0; while(1) { deal_with();//温度处理函数 display();//显示函数 keyscan();//按键调整函数 nao_zhong();//闹钟 } } void T0_init() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; num++; if(num==10) { num=0; beep=~beep; cc++; if(cc==20) { cc=0; beep=1; TR0=0; } } } |
|
相关推荐
1个回答
|
|
没人么? 大神呢
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
644 浏览 0 评论
求助一下关于51系列单片机的Timer0的计时问题,TH0、TL0+1的时间是怎么算的?
1819 浏览 1 评论
【RA-Eco-RA4E2-64PIN-V1.0开发板试用】开箱+Keil环境搭建+点灯+点亮OLED
1262 浏览 0 评论
【敏矽微ME32G070开发板免费体验】使用coremark测试敏矽微ME32G070 跑分
1136 浏览 0 评论
【敏矽微ME32G070开发板免费体验】开箱+点灯+点亮OLED
1357 浏览 2 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
12028 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 11:53 , Processed in 0.560191 second(s), Total 74, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号