DS18B20测温程序.docx
- 文档编号:2875217
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:8
- 大小:15.42KB
DS18B20测温程序.docx
《DS18B20测温程序.docx》由会员分享,可在线阅读,更多相关《DS18B20测温程序.docx(8页珍藏版)》请在冰豆网上搜索。
DS18B20测温程序
//温度高于26摄氏度则蜂鸣器响
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitdula=P2^6;
sbitwela=P2^7;
sbitbeer=P2^3;//控制蜂鸣器
sbitdsb=P2^2;//ds18b20的数据总线
sbitrs=P3^5;//液晶显示数据命令选择端口
sbitlcme=P3^4;//液晶显示使能信号
sbitkey=P3^7;
uinttemp;//温度传感器测得的温度
ucharcodelistone[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
ucharcodelisttwo[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
ucharcodelistthree[]={"Thetempisunder:
"};
voiddelay(uchari)
{
while(--i);
}
Voiddelayone(uintz)
{
uintx,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
/*液晶显示写命令初始化*/
Voidwritecom(ucharcom)
{
rs=0;
delayone
(2);
lcme=0;
P0=com;
lcme=1;
delayone
(2);
lcme=0;
}
/*液晶显示写数据初始化*/
voidwritebyte(ucharbyte)
{
rs=1;
delayone
(2);
lcme=0;
P0=byte;
lcme=1;
delayone
(2);
lcme=0;
}
/*液晶显示初始化*/
voidds18b20init()
{
uinttempone;
uinti;
dula=0;
wela=0;
lcme=0;
writecom(0x38);
writecom(0x0f);
writecom(0x06);
writecom(0x80);
for(i=0;i<16;i++)
{
writebyte(listthree[i]);
delayone
(2);
}
writecom(0x80+0x40);
for(i=0;i<16;i++)
{
tempone=temp&0x80;
writebyte(tempone);
delay
(2);
temp<<=1;
}
writecom(0x0c);
}
/*温度传感器初始化*/
voidtempinit()
{
dsb=1;
delay
(1);
dsb=0;
delay(250);
dsb=1;
delay(100);
}
voidwritedata(uchardat)
{
uinti;
for(i=0;i<8;i++)
{
dsb=1;
_nop_();
dsb=0;
_nop_();
_nop_();
dsb=dat&0x01;
delay(10);
dat>>=1;
delay
(1);
}
dsb=1;
}
ucharreaddata()
{
uinti;
uchardate;
for(i=0;i<8;i++)
{
dsb=1;
_nop_();
dsb=0;
_nop_();
_nop_();
//date>>=1;
dsb=1;
delay
(1);
date>>=1;//右移放于此处也可以
if(dsb==1)
date|=0x80;
delay(10);
}
returndate;
}
uintcontrol()
{
floattt;
//uinttemp;
uchara,b;
tempinit();
writedata(0xcc);
writedata(0x44);
tempinit();
writedata(0xcc);
writedata(0xbe);
a=readdata();
b=readdata();
temp=b;
temp<<=8;
temp=temp|a;
tt=temp*0.0625;
temp=tt*100+0.05;
returntemp;
}
voidshuma(uinttemp)
{
ucharbai,shi,ge,xiaoshu;
bai=(temp/1000);
shi=temp%1000/100;
ge=temp%100/10;
xiaoshu=temp%10;
dula=1;
P0=listone[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayone
(1);
dula=1;
P0=listtwo[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayone
(1);
dula=1;
P0=listone[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delayone
(1);
dula=1;
P0=listone[xiaoshu];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(10);
}
voidwarning(uinttemp)
{
if((temp>=2600)&&(temp<2800)&&(key==1))
{
beer=0;
P1=0x55;
delayone(4);
}
if((key==0)||(temp<2600))
{
delayone(5);
if((key==0)||(temp<2600))
{
beer=1;
P1=0xff;
}
}
}
voidmain()
{
while
(1)
{
if(temp<2600)
{
shuma(control());
}
if((temp>=2600)&&(temp<2800))
{
shuma(control());
warning(temp);
}
if(temp>=2800)
{
dula=0;
wela=0;
P0=0xff;
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DS18B20 测温 程序