带电子温度计的万年历讲解.docx
- 文档编号:11005096
- 上传时间:2023-02-24
- 格式:DOCX
- 页数:16
- 大小:43.32KB
带电子温度计的万年历讲解.docx
《带电子温度计的万年历讲解.docx》由会员分享,可在线阅读,更多相关《带电子温度计的万年历讲解.docx(16页珍藏版)》请在冰豆网上搜索。
带电子温度计的万年历讲解
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitIO=P1^0;
sbitSCLK=P1^1;
sbitRST=P1^2;
sbitSPEAK=P1^7;
sbitRS=P2^0;
sbitRW=P2^1;
sbitEN=P2^2;
sbitDQ=P2^3;
sbitKey1=P2^4;
sbitKey2=P2^5;
sbitKey3=P2^6;
sbitKey4=P2^7;
ucharWEEK[7][4]={"SUN","MON","TUS","WEN","THU","FRI","SAT"};//用于存储星期
ucharLCD_DSY_BUFFER1[]={"DATE00-00-00"};//用于存储年月日
ucharLCD_DSY_BUFFER2[]={"T00:
00:
00"};//用于存储时分秒
ucharDateTime[7];//存储从ds1302中读出的时间数据
ucharb1,b2,b3;
intt,a1,a2;
ucharmin=0,h=0,day=0,mon=0,year=0,weak=0;
ucharchange1=0,change2=0;
ucharbcd1=0,bcd2=0;
voidDelayXus(uintms);//延时函数
voidWrite_A_Byte_TO_DS1302(ucharx);//该函数用于从ds1302中写入一字节的数据
ucharGet_A_Byte_FROM_DS1302();//该函数用于向ds1302中读出一字节的数据
ucharRead_Data(ucharaddr);//用于向ds1302中读出数据
voidGetTime();
voidds1302_init();//对ds1302进行初始化
voidwrite_1302(ucharadd,uchardat);//用于向ds1302写入数据
voidWrite_LCD_Data(uchardat);//向lcd的控制器写入数据
voidWrite_LCD_Command(ucharcmd);//向lcd控制器写入指令
voidInit_LCD();//lcd的初始化
voidSet_LCD_POS(ucharp);//lcd现实的地址进行选择
voidDisplay_LCD_String1(ucharp,uchar*s);//下面的两个函数是用于把数组中的内容写入lcd控制器
voidDisplay_LCD_String2(ucharp,uchar*s);
voidFormat_DateTime(uchard,uchar*a);//把ds1302中读出的bcd码数据进行转化成字符型
voidDelayus(uchari);//延时函数用于ds18b20的时序的控制
bitreset(void);
voidds_18b20_init();//ds18b20的初始化函数
ucharrdbyte();//从ds18b20中读出一字节的数据
voidwrbyte(uchardat);//向ds18b20中写入一字节的数据
voidconvert();//控制ds18b20进行数据的转换
intreadt();//控制ds18b20对数据进行输出
voidds18_b_20_show();//用于在lcd中显示温度数值
voidTime_change();
voidmain()/////////////
{
chari;
Init_LCD();
ds1302_init();
while
(1)
{
GetTime();
Format_DateTime(DateTime[6],LCD_DSY_BUFFER1+5);
Format_DateTime(DateTime[4],LCD_DSY_BUFFER1+8);
Format_DateTime(DateTime[3],LCD_DSY_BUFFER1+11);
Format_DateTime(DateTime[2],LCD_DSY_BUFFER2+2);
Format_DateTime(DateTime[1],LCD_DSY_BUFFER2+5);
Format_DateTime(DateTime[0],LCD_DSY_BUFFER2+8);
strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]]);
Display_LCD_String1(0x00,LCD_DSY_BUFFER1);
Display_LCD_String2(0x40,LCD_DSY_BUFFER2);
while(Key4==0)Time_change();
change1=0;
ds18_b_20_show();
while(a1>70&&a1!
=85)SPEAK=~SPEAK;
if(DateTime[2]==1)
for(i=0;i<120;i++)
{
SPEAK=~SPEAK;
DelayXus
(2);
}
if(DateTime[2]==1)
for(i=0;i<120;i++)
{
SPEAK=~SPEAK;
DelayXus
(2);
}
}
}
voidDelayXus(uintms)//////
{
uchari;
while(ms--)
{
for(i=0;i<120;i++);
}
}
voidDelayus(uchari)
{
ucharj;
for(j=i;j>0;j--);
}
voidWrite_A_Byte_TO_DS1302(ucharx)/////////////
{
uchari;
SCLK=0;
for(i=0;i<8;i++)
{
IO=x&0x01;SCLK=1;DelayXus
(2);SCLK=0;x>>=1;
}
}
ucharGet_A_Byte_FROM_DS1302()////////////////
{
uchari,b=0x00;
for(i=0;i<8;i++)
{
b>>=1;
if(IO==1)
b|=0x80;
SCLK=1;
DelayXus
(2);
SCLK=0;
DelayXus
(2);
}
returnb;
}
ucharRead_Data(ucharaddr)//////////////////
{
uchardat;
RST=0;RST=1;
Write_A_Byte_TO_DS1302(addr);
dat=Get_A_Byte_FROM_DS1302();
RST=0;
returndat;
}
voidGetTime()/////////////////
{
uchari,addr=0x81;
for(i=0;i<7;i++)
{
DateTime[i]=Read_Data(addr);
addr+=2;
}
}
voidWrite_LCD_Data(uchardat)/////////////
{
DelayXus
(2);
RS=1;RW=0;EN=0;P0=dat;EN=1;DelayXus
(1);EN=0;
}
voidWrite_LCD_Command(ucharcmd)/////////////
{
DelayXus
(2);
RS=0;RW=0;EN=0;P0=cmd;EN=1;DelayXus
(1);EN=0;
}
voidInit_LCD()///////////
{
Write_LCD_Command(0x38);
DelayXus
(1);
Write_LCD_Command(0x01);
DelayXus
(1);
Write_LCD_Command(0x06);
DelayXus
(1);
Write_LCD_Command(0x0c);
DelayXus
(1);
}
voidDisplay_LCD_String1(ucharp,uchar*s)//////////
{
uchari;
Set_LCD_POS(p);
for(i=0;i<18;i++)
{
Write_LCD_Data(s[i]);
DelayXus
(1);
}
}
voidDisplay_LCD_String2(ucharp,uchar*s)//////////
{
uchari;
Set_LCD_POS(p);
for(i=0;i<11;i++)
{
Write_LCD_Data(s[i]);
DelayXus
(1);
}
}
voidSet_LCD_POS(ucharp)
{
Write_LCD_Command(p|0x80);
}
voidFormat_DateTime(uchard,uchar*a)//////////////
{
*a=d/16+'0';
*(a+1)=d%16+'0';
}
voidwrite_1302(ucharadd,uchardat)//ds1302的写入函数
{
RST=0;
RST=1;
Write_A_Byte_TO_DS1302(add);
Write_A_Byte_TO_DS1302(dat);
RST=0;
}
voidds1302_init()//ds1302的初始化函数
{
RST=0;
SCLK=0;
write_1302(0x8e,0x00);
write_1302(0x80,0x00);
write_1302(0x82,0x00);
write_1302(0x84,0x09);
write_1302(0x8a,0x04);
write_1302(0x86,0x22);
write_1302(0x88,0x06);
write_1302(0x8c,0x15);
write_1302(0x90,0x01);
write_1302(0x8e,0x80);
}
voidds18_b_20_show()
{
convert();
t=readt();
a1=(int)(t*0.0625);
a2=(int)(t*0.625)-a1*10;
b1=a1/10+'0';
b2=a1%10+'0';
b3=a2+'0';
Write_LCD_Command(0xcc);
Write_LCD_Data(b1);
Write_LCD_Data(b2);
Write_LCD_Data('.');
Write_LCD_Data(b3);
}
bitreset(void)//ds18b20的复位函数
{
bitflag;
DQ=0;
Delayus(70);
DQ=1;
Delayus(3);
flag=DQ;
Delayus(15);
returnflag;
}
voidds_18b20_init()//ds18b20的初始化函数
{
while
(1)
{
if(!
reset())
{
DQ=1;
break;
}
elsereset();
}
}
ucharrdbyte()//ds18b20的读一字节函数
{
uchari,dat=0;
DQ=1;
for(i=0;i<8;i++)
{
DQ=0;
dat>>=1;
DQ=1;
Delayus
(1);
if(DQ)
dat|=0X80;
Delayus(4);
DQ=1;
}
returndat;
}
voidwrbyte(uchardat)//ds18b20写一字节函数
{
uchari;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
Delayus(5);
DQ=1;
dat>>=1;
}
}
voidconvert()//ds18b20的转换函数
{
ds_18b20_init();
wrbyte(0xcc);
wrbyte(0x44);
}
intreadt()//ds18b20的读数据函数
{
ucharh,l;
ds_18b20_init();
wrbyte(0xcc);
wrbyte(0xbe);
l=rdbyte();
h=rdbyte();
Write_LCD_Command(0xcb);
if(h>=8)
{
h=~h;
l=~l;
Write_LCD_Data('-');
returnh*256+l+1;
}
else
{
Write_LCD_Data('');
returnh*256+l;
}
}
voidTime_change()
{
write_1302(0x8e,0x00);
if(Key1==1&&Key2==1&&Key3==0)//秒
{
if(Key4==0&&change1==change2)
{
write_1302(0x80,0x00);
change1=1;
}
}
if(Key1==1&&Key2==0&&Key3==1)//分
{
if(Key4==0&&change1==change2)
{
bcd2++;
if(bcd2==10)bcd1++,bcd2=0;
if(bcd1==6)bcd1=0;
min=16*bcd1+bcd2;
write_1302(0x82,min);
change1=1;
}
}
if(Key1==1&&Key2==0&&Key3==0)//时
{
if(Key4==0&&change1==change2)
{
bcd2++;
if(bcd2==10)bcd1++,bcd2=0;
if(bcd1==2&&bcd2==4)bcd1=0,bcd2=0;
h=16*bcd1+bcd2;
write_1302(0x84,h);
change1=1;
}
}
if(Key1==0&&Key2==1&&Key3==1)//日
{
if(Key4==0&&change1==change2)
{
bcd2++;
if(bcd2==10)bcd1++,bcd2=0;
if(bcd1==3&&bcd2==2)bcd1=0,bcd2=1;
day=16*bcd1+bcd2;
write_1302(0x86,day);
change1=1;
}
}
if(Key1==0&&Key2==1&&Key3==0)//月
{
if(Key4==0&&change1==change2)
{
bcd2++;
if(bcd2==10)bcd1++,bcd2=0;
if(bcd1==1&&bcd2==3)bcd1=0,bcd2=1;
mon=16*bcd1+bcd2;
write_1302(0x88,mon);
change1=1;
}
}
if(Key1==0&&Key2==0&&Key3==1)//年
{
if(Key4==0&&change1==change2)
{
bcd2++;
if(bcd2==10)bcd1++,bcd2=0;
if(bcd1==10)bcd1=0;
year=16*bcd1+bcd2;
write_1302(0x8c,year);
change1=1;
}
}
if(Key1==0&&Key2==0&&Key3==0)//星期
{
if(Key4==0&&change1==change2)
{
bcd2++;
if(bcd2==7)bcd2=0;
write_1302(0x8a,bcd2);
change1=1;
}
}
write_1302(0x8e,0x80);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 带电 温度计 万年历 讲解