|
#include<reg51.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar i;
sbit RS=P2^7;
sbit RW=P2^6;
sbit E=P2^5;
sbit D1=P1^0;
sbit D2=P1^1;
sbit k1=P2^1;//加按鍵
sbit k2=P2^2;//減按鍵
sbit k3=P2^0;//設置按鍵
uint th=360;//設定 高溫
uint tl=350;//設定 低溫
uint ta;//實際溫度
uchar code t0[]="WD= . ";
uchar code t1[]="TH= . TL= . ";
uchar code wendu[]="0123456789"; //利用一個溫度表解決溫度顯示亂碼
sbit DQ=P2^3;//定義DS18B20總線IO
uchar position;//設置的位置
//延時子函數
void delay(uint z)
{
uint x,y;
for(x=100;x>1;x--)
for(y=z;y>1;y--);
}
//LCD1602液晶寫命令子函數
void write_com(uchar com)
{
RS=0;
P0=com;
delay(5);
E=1;
delay(5);
E=0;
}
//LCD1602液晶寫數據子函數
void write_date(uchar date)
{
RS=1;
P0=date;
delay(5);
E=1;
delay(5);
E=0;
}
//LCD1602液晶初始化子函數
void LCD1602_init()
{
E=0;
RW=0;
write_com(0x38);
write_com(0x01);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
for(i=0;i<16;i++)
{
write_date(t0[i]);
delay(0);
}
write_com(0x80+0x40);
for(i=0;i<16;i++)
{
write_date(t1[i]);
delay(0);
}
}
//延時子函數
void tmpDelay(int num)
{
while(num--);
}
//DS18B20溫度傳感器初始化子函數
void DS18B20_init()
{
uchar x=0;
DQ=1; //DQ復位
tmpDelay(8); //稍做延時
DQ=0; //單片機將DQ拉低
tmpDelay(80); //精確延時 大于 480us
DQ=1; //拉高總線
tmpDelay(14);
x=DQ; //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
tmpDelay(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;
tmpDelay(4);
}
return(dat);
}
//DS18B20溫度傳感器寫一個字節子函數
void WriteOneChar(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
tmpDelay(5);
DQ=1;
dat>>=1;
}
}
//讀取溫度子函數
uint Readtemp()
{
uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
DS18B20_init();
WriteOneChar(0xCC); // 跳過讀序號列號的操作
WriteOneChar(0x44); // 啟動溫度轉換
DS18B20_init();
WriteOneChar(0xCC); //跳過讀序號列號的操作
WriteOneChar(0xBE); //讀取溫度寄存器
a=ReadOneChar(); //連續讀兩個字節數據 //讀低8位
b=ReadOneChar(); //讀高8位
t=b;
t<<=8;
t=t|a; //兩字節合成一個整型變量。
tt=t*0.0625; //得到真實十進制溫度值,因為DS18B20可以精確到0.0625度,所以讀回數據的最低位代表的是0.0625度
t=tt*10+0.5; //放大十倍,這樣做的目的將小數點后第一位也轉換為可顯示數字,同時進行一個四舍五入操作。
return(t);
}
//LCD1602液晶顯示子函數
void display()
{
uint shi,ge,xiaoshu; //這里的num,shi,ge,xiaoshu 必須用unsigned int無符號整數來表示,用unshigned char 字符型則顯示錯誤
shi=th/100; //顯示 最高溫度 Th
ge=th/10%10;
xiaoshu=th%10;
write_com(0x80+0x40+3);
write_date(wendu[shi]);
write_com(0x80+0x40+4);
write_date(wendu[ge]);
write_com(0x80+0x40+6);
write_date(wendu[xiaoshu]);
shi=tl/100; //顯示 最低文帝 Tl
ge=tl/10%10;
xiaoshu=tl%10;
write_com(0x80+0x40+11);
write_date(wendu[shi]);
write_com(0x80+0x40+12);
write_date(wendu[ge]);
write_com(0x80+0x40+14);
write_date(wendu[xiaoshu]);
}
//報警子函數
void temp_check()
{
uint shi,ge,xiaoshu; //這里的num,shi,ge,xiaoshu 必須用unsigned int無符號整數來表示,用unshigned char 字符型則顯示錯誤
ta=Readtemp();
if(ta>th)
{
D1=0;
D2=1;
}
else if(ta<tl)
{
D1=1;
D2=0;
}
else
{
D1=1;
D2=1;
}
shi=ta/100; //顯示 實際溫度
ge=ta/10%10;
xiaoshu=ta%10;
write_com(0x80+3);
write_date(wendu[shi]);
write_com(0x80+4);
write_date(wendu[ge]);
write_com(0x80+6);
write_date(wendu[xiaoshu]);
}
//按鍵掃描子函數
void key()
{
if(k3==0) //set 按鍵 按下
{
delay(1);
if(k3==0)
{ //設置位置設定
position++;
if(position>2)
position=0;
if(position==0) // 無設置位置
{
write_com(0x80+0x40+7);
write_date(' ');
write_com(0x80+0x40+15);
write_date(' ');
}
if(position==1) //設置TH 在th后顯示< 標識當前位置
{
write_com(0x80+0x40+7);
write_date('<');
write_com(0x80+0x40+15);
write_date(' ');
}
if(position==2) //設置Tl 在tl后顯示< 標識當前位置
{
write_com(0x80+0x40+7);
write_date(' ');
write_com(0x80+0x40+15);
write_date('<');
}
while(k3==0);
}
}
if(k1==0)
{
delay(1);
if(k1==0)
{
if(position==1)
{
th+=5;
if(th>1000)
th=990;
}
if(position==2)
{
tl+=5; //tl>th時 是個錯誤,這里避免此情況發生
if(tl>=th)
tl=th-5;
}
display();
}
while(k1==0);
}
if(k2==0)
{
delay(1);
if(k2==0)
{
if(position==1)
{
th-=5; //th<tl時 是個錯誤,這里避免此情況發生
if(th<=tl)
th=tl+5;
}
if(position==2)
{
if(tl<10)
tl=10;
else
tl-=5;
}
display();
}
while(k2==0);
}
}
//主函數
void main()
{
uint i;
LCD1602_init();
display();
while(1)
{
delay(1);
if((++i)>500) //500ms 檢測一次溫度
{
temp_check();
i=0;
}
key();
}
}
|
-
-
2、仿真圖.zip
2017-2-14 20:15 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
60.03 KB, 下載次數: 223, 下載積分: 黑幣 -5
評分
-
查看全部評分
|