万年历及温度测量.docx
- 文档编号:15114848
- 上传时间:2023-06-30
- 格式:DOCX
- 页数:21
- 大小:16.98KB
万年历及温度测量.docx
《万年历及温度测量.docx》由会员分享,可在线阅读,更多相关《万年历及温度测量.docx(21页珍藏版)》请在冰点文库上搜索。
万年历及温度测量
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitrs=P2^4;
sbitrw=P2^5;
sbiten=P2^6;
sbitkey1=P1^0;
sbitkey2=P1^5;
sbitkey3=P1^6;
/////////////////////////////////
sbitDQ=P1^7;//ds18b20端口
/////////////////////////////
ucharcount,n,n1,TempH,TempL;
uintnum1,temp;
charshi,fen,miao,nian,yue,ri,day,nian1;
ucharcodetable[]="2010-06-21MON";
ucharcodetable1[]="00:
00:
00C";
ucharcodetable2[]="MON";
ucharcodetable3[]="TUE";
ucharcodetable4[]="WED";
ucharcodetable5[]="THU";
ucharcodetable6[]="FRI";
ucharcodetable7[]="SAT";
ucharcodetable8[]="SUN";
voiddisplay();
unsignedintReadTemperature(void);
voiddelay(ucharz)
{
ucharx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voiddelayus(uinti)
{
while(i--);
}
voidwrite_com(ucharcom)//1602液晶写指令
{
rs=0;
en=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}//1602液晶写指令
voidwrite_date(uchardate)//1602液晶写数据
{
rs=1;
en=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}//1602液晶写数据
voidwrite_sfm(ucharadd,uchardate)//时分秒
{
ucharshi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
//////////////////////DS18B20温度显示
voidwrite_wen(ucharadd,ucharwen)//DS18B20温度显示
{
ucharshi,ge;
shi=wen/10;
ge=wen%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
/////////////////////////////////////
voidwrite_nyr(ucharadd,uchardate)//年月日
{
ucharshi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
voidinit()//初始化程序
{
ucharnum;
rw=0;
en=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}//初始化程序
voidkeyscan()//键盘扫描程序
{
if(key1==0)
{
count++;
delay(5);
while(!
key1);//kye1键是功能键
if(count==1)//按一下在秒针处闪烁
{
TR0=0;
write_com(0x80+0x40+8);
write_com(0x0f);
delay(5);
}
if(count==2)//按2下在分针处闪烁
{
TR0=0;
write_com(0x80+0x40+5);
write_com(0x0f);
delay(5);
}
if(count==3)//按3下在时针处闪烁
{
TR0=0;
write_com(0x80+0x40+2);
write_com(0x0f);
delay(5);
}
if(count==4)//按4下在星期针处闪烁
{
TR0=0;
write_com(0x80+14);
write_com(0x0f);
delay(5);
}
if(count==5)//按5下在日针处闪烁
{
TR0=0;
write_com(0x80+10);
write_com(0x0f);
delay(5);
}
if(count==6)
{
TR0=0;
write_com(0x80+7);
write_com(0x0f);
delay(5);
}
if(count==7)
{
TR0=0;
write_com(0x80+4);
write_com(0x0f);
delay(5);
}
if(count==8)
{
TR0=0;
write_com(0x80+1);
write_com(0x0f);
delay(5);
}
if(count==10)
{
count=0;
TR0=1;
write_com(0x0c);
}
}
if(count!
=0)
{
if(key2==0
{
delay(5);
while(!
key2);
if(count==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(7,miao);
write_com(0x80+0x40+8);
}
if(count==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(4,fen);
write_com(0x80+0x40+5);
}
if(count==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(1,shi);
write_com(0x80+0x40+2);
}
if(count==4)
{
n++;
if(n==8)
n=0;
write_com(0x80+12);
switch(n)
{
case1:
for(n1=0;n1<3;n1++)
write_date(table2[n1]);break;
case2:
for(n1=0;n1<3;n1++)
write_date(table3[n1]);break;
case3:
for(n1=0;n1<3;n1++)
write_date(table4[n1]);break;
case4:
for(n1=0;n1<3;n1++)
write_date(table5[n1]);break;
case5:
for(n1=0;n1<3;n1++)
write_date(table6[n1]);break;
case6:
for(n1=0;n1<3;n1++)
write_date(table7[n1]);break;
case7:
for(n1=0;n1<3;n1++)
write_date(table8[n1]);break;
}
}
if(count==5)
{
ri++;
if(((yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12&&ri==32)||((yue==4||yue==6||yue==9||yue==11)&&(ri==31))||((nian%4==0&&nian%100!
=0)||(nian%400==0)&&(yue==2)&&(ri==30))||((nian%4!
=0)&&(yue==2)&&(ri==29))))
ri=0;
write_nyr(9,ri);
write_com(0x80+10);
}
if(count==6
{
yue++;
if(yue==13)
yue=0;
write_nyr(6,yue);
write_com(0x80+7);
}
if(count==7)
{
nian++;
if(nian==100)
nian=0;
write_nyr(3,nian);
write_com(0x80+4);
}
if(count==8)//按一下年加一
{
nian1++;
if(nian1==100)
nian1=0;
write_nyr(1,nian1);
write_com(0x80+2);
}
}
if(key3==0)
{
delay(5);
while(!
key3);
if(count==1
{
miao--;
if(miao==-1)
miao=59;
write_sfm(7,miao);
write_com(0x80+0x40+8);
}
if(count==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(4,fen);
write_com(0x80+0x40+5);
}
if(count==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(1,shi);
write_com(0x80+0x40+2);
}
if(count==4)
{
n--;
if(n==0)
n=7;
write_com(0x80+12);
switch(n)
{
case1:
for(n1=0;n1<3;n1++)
write_date(table2[n1]);break;
case2:
for(n1=0;n1<3;n1++)
write_date(table3[n1]);break;
case3:
for(n1=0;n1<3;n1++)
write_date(table4[n1]);break;
case4:
for(n1=0;n1<3;n1++)
write_date(table5[n1]);break;
case5:
for(n1=0;n1<3;n1++)
write_date(table6[n1]);break;
case6:
for(n1=0;n1<3;n1++)
write_date(table7[n1]);break;
case7:
for(n1=0;n1<3;n1++)
write_date(table8[n1]);break;
}
}
if(count==5)
{
if((yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)&&(ri==-1))
ri=31;
if((yue==4||yue==6||yue==9||yue==11)&&(ri==-1))
ri=30;
if((nian%4==0&&nian%100!
=0)||(nian%400==0)&&(yue==2)&&(ri==-1))
ri=29;
if((nian%4!
=0)&&(yue==2)&&(ri==-1))
ri=28;
write_nyr(9,ri);
write_com(0x80+10);
}
if(count==6)
{
yue--;
if(yue==-1)
yue=12;
write_nyr(6,yue);
write_com(0x80+7);
}
if(count==7)
{
nian--;
if(nian==-1)
nian=99;
write_nyr(3,nian);
write_com(0x80+4);
}
if(count==8)
{
nian1--;
if(nian1==-1)
nian=99;
write_nyr(1,nian);
write_com(0x80+2);
}
}
}
}
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num1++;
if(num1==20)
{
num1=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
ri++;
if(ri==31)
{
ri=0;
yue++;
if(yue==12)
{
yue=0;
nian++;
write_nyr(3,nian);
}
write_nyr(6,yue);
}
write_nyr(9,ri);
}
write_sfm(1,shi);
}
write_sfm(4,fen);
}
write_sfm(7,miao);
}
////////////////////////////////
display();
////////////////////////////////
}
/***************************************************************************************/
voidInit_DS18B20(void)
{
unsignedcharx=0;
DQ=1;
delayus(8);
DQ=0;
delayus(80);
DQ=1;
delayus(10);
x=DQ;
delayus(5);
}//18b20
unsignedcharReadOneChar(void)
{
unsignedchari=0;
unsignedchardat=0;
for(i=8;i>0;i--)
{
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delayus(5);
}
return(dat);
}
voidWriteOneChar(unsignedchardat)
{
unsignedchari=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delayus(5);
DQ=1;
dat>>=1;
}
}
unsignedintReadTemperature(void){
unsignedchara=0;
unsignedintb=0;
unsignedintt=0;
Init_DS18B20();
WriteOneChar(0xCC);WriteOneChar(0x44);delayus(200);
Init_DS18B20();
WriteOneChar(0xCC);WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
b<<=8;
t=a+b;
return(t);
}//读取温度
//*************************************
voiddisplay()//DS18B20显示函数
{
temp=ReadTemperature();
TempH=temp>>4;
TempL=temp&0x0F;
TempL=TempL*6/10;
write_com(0xc0+10);
if(temp&0x8000)
{write_date(0x2d);
temp=~temp;
temp=temp+1;
}
else
{write_date(0x2b);}
write_date(0x30+(TempH%100)/10);
delay
(1);
write_date(0x30+(TempH%100)%10);
delay
(1);
write_date(0x2e);
delay
(1);
write_date(0x30+TempL);
delay
(1);
write_date(0x43);
delay
(1);
}//DS18B20显示函数
/////////主函数//////////
voidmain()
{
init();
while
(1)
{
keyscan();
}
}/////////主函数//////////
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 万年历 温度 测量
![提示](https://static.bingdoc.com/images/bang_tan.gif)