18b20h温度传感器相关的程序.docx
- 文档编号:3599848
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:12
- 大小:16.75KB
18b20h温度传感器相关的程序.docx
《18b20h温度传感器相关的程序.docx》由会员分享,可在线阅读,更多相关《18b20h温度传感器相关的程序.docx(12页珍藏版)》请在冰豆网上搜索。
18b20h温度传感器相关的程序
18b20.h主程序
#include
#include
#include
#include
#include
#include
sbitbeep=P2^1;
sbitkey1=P1^0;
sbitkey2=P1^1;
sbitkey3=P1^2;
sbitkey4=P1^3;
ucharcount,xx=13,alarmx,alarmx1=1,ff,fA;
ucharmax=31,min=55;
uchartt[5];
voidkey();
voidinit()
{
init_lcd();
clrram_lcd();
TMOD=0x01;
TH0=0x4c;
TL0=0x00;
EA=0;
ET0=1;
TR0=1;
}
voidshow_logo()
{
uchari,j;
show_p(logo);
mdelay(1000);
for(i=0;i<128;i++)
{
for(j=49;j<63;j++)
disp_bit(i,j,2);
}
mdelay(1500);
}
voidshow_zb()
{
show_p(zb);
gotoxy(1,7);
wr_d_lcd(0xa1);
wr_d_lcd(0xe6);
}
voidabout()
{
clrram_lcd();
show_p(apple);
mdelay(2000);
show_p(logo);
while(ff==9)
{
key();
}
}
voidalarm(ucharnn)
{
if((nn!
=0)&alarmx1)
{
fA=1;
beep=~beep;
gotoxy(1,2);
if(beep)
{
print("");
}
else
{
print("△");
gotoxy(3,2);
switch(nn)
{
case1:
print("高温告警");break;
case2:
print("失温告警");break;
case3:
print("传感器告警");break;
}
}
}
elseif(fA)
{
beep=1;
gotoxy(1,2);
print("");
gotoxy(3,2);
print("");
fA=0;
}
}
voidtemp_to_zb(void)
{
uchary,temp,i;
temp=(temp_h<<5)|(temp_l>>3);
if(temp<0x29)
temp=0x29;
elseif(temp>0x55)
temp=0x55;
else
{
y=101-temp;
if(y alarmx=1; elseif(y>min) alarmx=2; elseif(alarmx! =3) alarmx=0; for(i=16;i<60;i++) { disp_bit(xx,i,0); } disp_bit(xx,y,1); xx++; if(xx==124) xx=13; for(i=max;i { disp_bit(xx,i,1); } } } voidzb_to_sz(ucharzz) { uchartt0; if(tt0=(101-zz)&0x01) tt[4]=5+'0'; else tt[4]='0'; tt[3]='.'; tt0=(101-zz)>>1; tt[2]=tt0%10+'0'; tt[1]=tt0/10%10+'0'; tt[0]=tt0/100; if(tt[0]==0) tt[0]=''; tt[5]='\0'; } voidkey() { if(key2==0) { mdelay(10); if(key2==0) { while(key2==0); switch(ff) { case0: gotoxy(2,1);print("Alarm: "); gotoxy(2,5);if(alarmx1==1)print("On");elseprint("Off"); ff++;break; case1: gotoxy(2,1);print("Setmax"); gotoxy(2,5);zb_to_sz(max);print(tt);ff++;break; case2: gotoxy(2,1);print("Setmin"); gotoxy(2,5);zb_to_sz(min);print(tt);ff++;break; case3: gotoxy(2,1);print("About");ff++;break; case4: gotoxy(2,1);print("");ff=0;break; case9: ff=0;show_zb();xx=13;break; } } } if(key3==0) { mdelay(10); if(key3==0) { while(key3==0); switch(ff) { case0: break; case1: gotoxy(2,5);if(alarmx1==1){alarmx1=2;print("Off");} else{alarmx1=1;print("On");}break; case2: if(max>16){max--;gotoxy(2,5);zb_to_sz(max);print(tt);}break; case3: if(min>16){min--;gotoxy(2,5);zb_to_sz(min);print(tt);}break; case4: ff=9;about();break; } } } if(key4==0) { mdelay(10); if(key4==0) { while(key4==0); switch(ff) { case0: break; case1: gotoxy(2,5);if(alarmx1==1){alarmx1=2;print("Off");} else{alarmx1=1;print("On");}break; case2: if(max<60){max++;gotoxy(2,5);zb_to_sz(max);print(tt);}break; case3: if(min<60){min++;gotoxy(2,5);zb_to_sz(min);print(tt);}break; case4: break; } } } } voiddisp_temp() { if(count==10) { count=0; ReadTemp(); temp_to_str(); gotoxy(1,3); print(TempBuffer); temp_to_zb(); alarm(alarmx); } } voidcheck_ds18b20() { if(presence==1) alarmx=3; else alarmx=0; } main() { init(); show_logo(); show_zb(); EA=1; while (1) { check_ds18b20(); key(); disp_temp(); } } voidtimer0()interrupt1 { TH0=0x4c; TL0=0x00; count++; if(count>10) count=0; } ds18b20.h温度传感器相关的程序 //**************************************** //ds18b20子程序模块 //**************************************** #defineuintunsignedint #defineucharunsignedchar sbitDQ=P2^7;//温度传送数据IO口 uchartemp_value,temp1_value,temp_h,temp_l,presence;//temp_value温度值正数部分temp1_value温度值小数部分 ucharTempBuffer[8]; /***********ds18b20延迟子函数(晶振12MHz)*******/ voiddelay_18B20(unsignedinti) { while(i--); } /**********ds18b20初始化函数**********************/ Init_DS18B20(void) { DQ=1;//DQ复位 delay_18B20(8);//稍做延时 DQ=0;//单片机将DQ拉低 delay_18B20(40);//精确延时大于480us DQ=1;//拉高总线 delay_18B20(7); presence=DQ;//稍做延时后如果x=0则初始化成功x=1则初始化失败 delay_18B20(10); return(presence);//返回信号,0=presence,1=nopresence } /***********ds18b20读一个字节**************/ ucharReadOneChar(void) { uchari=0; uchardat=0; for(i=8;i>0;i--) { DQ=0;//给脉冲信号 dat>>=1; DQ=1;//给脉冲信号 if(DQ) dat|=0x80; delay_18B20(4); } return(dat); } /*************ds18b20写一个字节****************/ voidWriteOneChar(uchardat) { uchari=0; for(i=8;i>0;i--) { DQ=0; DQ=dat&0x01; delay_18B20(5); DQ=1; dat>>=1; } } /**************读取ds18b20当前温度************/ voidReadTemp(void) { uchart; Init_DS18B20(); WriteOneChar(0xCC);//跳过读序号列号的操作 WriteOneChar(0x44);//启动温度转换 delay_18B20(100);//thismessageisweryimportant Init_DS18B20(); WriteOneChar(0xCC);//跳过读序号列号的操作 WriteOneChar(0xBE);//读取温度寄存器等(共可读9个寄存器)前两个就是温度 delay_18B20(50); temp_l=ReadOneChar();//读取温度值低位 temp_h=ReadOneChar();//读取温度值高位 t=temp_h&0xf8; if(t) { TempBuffer[0]=''; TempBuffer[1]='-'; temp_value=temp_h<<4; temp_value+=(temp_l&0xf0)>>4; temp_value=~temp_value+1; temp1_value=~temp_l&0x0f; } else { temp_value=temp_h<<4; temp_value+=(temp_l&0xf0)>>4; temp1_value=temp_l&0x0f; TempBuffer[0]=''; TempBuffer[1]=temp_value/100+'0';//百位 if(TempBuffer[1]=='1') { TempBuffer[1]='1'; } else { TempBuffer[1]=''; } } } /**************温度数据转换成液晶字符显示************/ voidtemp_to_str() { TempBuffer[2]=temp_value%100/10+'0';//十位 TempBuffer[3]=temp_value%10+'0';//个位 TempBuffer[4]='.'; TempBuffer[5]=temp1_value*625/1000%10+'0'; TempBuffer[6]=temp1_value*625/100%10+'0'; TempBuffer[7]=temp1_value*625/10%10+'0'; //TempBuffer[8]=temp1_value*625%10+'0'; TempBuffer[8]='\0'; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 18 b20h 温度传感器 相关 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)