標(biāo)題:
單片機智能水溫控制系統(tǒng)程序
[打印本頁]
作者:
clkzs
時間:
2019-12-15 18:57
標(biāo)題:
單片機智能水溫控制系統(tǒng)程序
智能水溫控制系統(tǒng)的相關(guān)文件
單片機源程序如下:
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
/*LCD1602顯示模塊*/
#define LCD1602_DATAPINS P0
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_E=P2^7;
/*按鍵調(diào)節(jié)模塊*/
sbit k1=P1^1;//功能鍵
sbit k2=P1^2;//加
sbit k3=P1^3;//減
void keyscan();
void key1_switch();
void key2_add();
void key3_minus();
uchar k_num;
void LCD1602_SetTemp(uchar add,uchar dat);
uchar high=90,low=10,set_t=30,set_d=2;//這里的數(shù)據(jù)類型可能有問題。
/*其他模塊接口*/
sbit DSPORT=P3^7; //溫度傳感器單總線端
sbit RELAY=P1^4;//繼電器
sbit COOLER=P1^7;//降溫裝置
/*LCD1602相關(guān)函數(shù)*/
void Delay1us(uint a);
void LCD1602_WriteCom(uchar com);//LCD1602寫入8位命令子函數(shù)
void LCD1602_WriteData(uchar dat);
void LCD1602_Init();//LCD1602初始化子程序
void LCD1602_ShowTemp(int temp);
/*DS18B20溫度傳感器相關(guān)函數(shù)*/
void Delay15us(uchar aa);
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar dat);
uchar Ds18b20ReadByte();
void Ds18b20SwitchTemp();
int Ds18b20ReadTemp();
float temp_max=125.00,temp_min=-55.00;
uchar ii=0,jj=0,kk=0;
uchar words_h[2]={"H="};
uchar words_l[2]={"L="};
uchar words_s[4]={"Set="};
void Sound();
void Temp_control(int temp);uchar count=0;//用來解決主程序執(zhí)行太快,按鍵光標(biāo)來不及顯示的問題
void Temp_control(int temp)
{
unsigned int kk=400;
uchar set_h,set_l;
set_h=set_t+set_d;
set_l=set_t-set_d;
if (temp<=set_h&&temp>=set_l) //設(shè)定溫度范圍之內(nèi)
{
RELAY = 0; //繼電器斷開
COOLER=0;//風(fēng)扇關(guān)閉
}
else
{
TR0=0;
RELAY = 1; //繼電器打開,外電路(常開觸電)閉合加熱
if (temp>=set_h)//溫度過高
{
RELAY = 0; //繼電器斷開
COOLER=1;//打開
}
TR0=1; }}
/*其他模塊相關(guān)函數(shù)*/
void Delay1us(uint a)
{
uint b,c;
for(c=a;c>0;c--)
for(b=110;b>0;b--);
}
void LCD1602_WriteCom(uchar com) //寫入命令
{
LCD1602_E=0;
LCD1602_RS=0;//0就寫指令
LCD1602_RW=0; //0就是寫
LCD1602_DATAPINS=com;
Delay1us(10);
LCD1602_E=1;
Delay1us(10);
LCD1602_E=0;}
void LCD1602_WriteData(uchar dat) //寫入數(shù)據(jù)
{
LCD1602_E=0;
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DATAPINS=dat;
Delay1us(10);
LCD1602_E=1;
Delay1us(10);
LCD1602_E=0;
}
void LCD1602_Init() //LCD初始化子程序
{
// uint num;
LCD1602_WriteCom(0x38); //開顯示
LCD1602_WriteCom(0x0f); //開顯示不顯示光標(biāo)
LCD1602_WriteCom(0x06); //寫一個指針加1
LCD1602_WriteCom(0x01); //清屏
LCD1602_WriteCom(0x80); //設(shè)置數(shù)據(jù)指針起點
LCD1602_WriteCom(0x80+0x0B);
for (ii=0;ii<2;ii++)
{
LCD1602_WriteData(words_h[ii]);
}
LCD1602_SetTemp(13,high);
LCD1602_WriteCom(0x80+0x40);
for (kk=0;kk<4;kk++)
{
LCD1602_WriteData(words_s[kk]);
}
LCD1602_SetTemp(4+0x40,set_t);
LCD1602_WriteCom(0x80+0x47);
LCD1602_WriteData(0x23); //顯示正負(fù)號
LCD1602_WriteCom(0x80+0x48);
LCD1602_WriteData(0x30+set_d);
LCD1602_WriteCom(0x80+0x4B);
for (jj=0;jj<2;jj++)
{
LCD1602_WriteData(words_l[jj]);
}
LCD1602_SetTemp(13+0x40,low);
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;//總中斷
ET0=1; TR0=1;
}
void Delay15us(uchar aa)
{
do{ _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); aa--;
}
while(aa);
}
/*DS18B20溫度傳感器相關(guān)函數(shù)*/
uchar Ds18b20Init()
{
uchar flag;
DSPORT=0; //總線拉低
Delay15us(40);//延時480~960us
DSPORT=1;
Delay15us(2);
flag=DSPORT;
Delay15us(6);//Ds18b20發(fā)出應(yīng)答信號,延時60~240us
Delay15us(25);
return flag;
}
uchar Ds18b20ReadByte()
{
uchar byte,bi;
uint i;
for (i=0;i<8;i++)
{
DSPORT=0;
_nop_();_nop_();_nop_();_nop_(); //保持低電平至少1us,但不能低于15us
DSPORT=1;
bi= DSPORT;//讀取一位數(shù)據(jù),循環(huán)8次為一個字節(jié)
byte = (byte >> 1) | (bi << 7);//將byte左移一位,然后與上右移7位后的bi,8次合成一個字節(jié) Delay15us(4);
}
return byte;
}
void Ds18b20WriteByte(uchar dat)
{
uint i;
for (i=0;i<8;i++)
{
DSPORT=0;
_nop_();
DSPORT=dat&0x01;//一位一位得讀,從最低位開始
Delay15us(5);
DSPORT=1;_nop_();
dat>>=1;
}}
void Ds18b20SwitchTemp()
{
Ds18b20Init();
Delay15us(7);
Ds18b20WriteByte(0xcc);//跳過ROM指令
Ds18b20WriteByte(0x44);//啟動溫度轉(zhuǎn)換指令
}
int Ds18b20ReadTemp()
{
int temp=0;
uchar temp_h,temp_l;
Ds18b20SwitchTemp();
Ds18b20Init(); Delay1us(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0xbe); //發(fā)送讀取溫度命令
temp_l = Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節(jié)
temp_h = Ds18b20ReadByte();
temp= temp_h;
temp<<=8;
temp|= temp_l;
return temp;}
void LCD1602_ShowTemp(int temp)
{
float tt;
uchar show[5]={0,0,0,0,0};
if (temp<0)//溫度為負(fù)
{
LCD1602_WriteCom(0x80); //寫地址 80表示初始地址
LCD1602_WriteData('-'); //顯示負(fù)
temp=temp-1;
temp=~temp;tt=temp;
temp=tt*0.0625*100;
}
else//溫度為正
{
LCD1602_WriteCom(0x80); //寫地址 80表示初始地址
LCD1602_WriteData('+'); //顯示正
tt=temp;
temp=tt*0.0625*100;
}
show[0]=temp/10000;
show[1]=temp%10000/1000;
show[2]=temp%1000/100;
how[3]=temp%100/10; show[4]=temp%10;
LCD1602_WriteCom(0x82);
LCD1602_WriteData('0'+show[0]); //百位
LCD1602_WriteCom(0x83);
LCD1602_WriteData('0'+show[1]); //十位
LCD1602_WriteCom(0x84);
LCD1602_WriteData('0'+show[2]); //個位
LCD1602_WriteCom(0x85);
LCD1602_WriteData('.');
LCD1602_WriteCom(0x86);
LCD1602_WriteData('0'+show[3]); //十分位
LCD1602_WriteCom(0x87);
LCD1602_WriteData('0'+show[4]); //百分位
LCD1602_WriteCom(0x88);
LCD1602_WriteData(0xdf);
LCD1602_WriteCom(0x89);
LCD1602_WriteData('C');
LCD1602_WriteCom(0x0C);
temp=temp/100;
Temp_control(temp);
}
void key1_switch()//功能切換
{
uchar set_h,set_l;
set_h=set_t+set_d;
set_l=set_t-set_d;
//k1鍵指令
if(k1==0)
{
Delay1us(10);//延時消抖
if(k1==0)
{
k_num++;
while(!k1); //按鍵沒有松開
if(k_num==1)
{
TR0=0; //關(guān)掉計時器,使得光標(biāo)得以顯示
LCD1602_WriteCom(0x80+0x46);
LCD1602_WriteCom(0x0f);
Delay15us(6);
}
if(k_num==2)
{
LCD1602_WriteCom(0x80+0x48);
LCD1602_WriteCom(0x0f);
Delay15us(6);
}
if(k_num==3)
{
LCD1602_WriteCom(0x80+0x0F);
LCD1602_WriteCom(0x0f);
Delay15us(6);
}
if(k_num==4)
{
LCD1602_WriteCom(0x80+0x4F);
LCD1602_WriteCom(0x0f);
Delay15us(6);
}
if(k_num==5)
{
k_num=0;
LCD1602_WriteCom(0x0C);//光標(biāo)不顯示
if (high<low) //用來default
{
high=90;
low=40;//恢復(fù)默認(rèn)值
}
if(set_h>high||set_l<low)
{
high=90;
low=10;//恢復(fù)默認(rèn)值
set_t=30;
set_d=2;//恢復(fù)默認(rèn)值
}
TR0=1;//計時器重新打開,使得屏幕得以刷新
}
}
}}
void key2_add()//功能加
{
if(k_num!=0)
{
if(k2==0)
{
Delay1us(5);
if(k2==0)
{
while(!k2);
if(k_num==1)
{
set_t++;
if (set_t>=high)
set_t=high;
LCD1602_SetTemp(4+0x40,set_t); LCD1602_WriteCom(0x80+0x46); }
if(k_num==2)
{
set_d++;
if (set_d>9)
set_d=0;
LCD1602_WriteCom(0x80+0x48);
LCD1602_WriteData(0x30+set_d);
LCD1602_WriteCom(0x80+0x48); }
if(k_num==3)
{
high++;
if (high>=temp_max)
high=0;
LCD1602_SetTemp(13,high);
LCD1602_WriteCom(0x80+0x0F);// LCD1602_WriteCom(0x80+0x40+8); }
if(k_num==4)
{
low++;
if (low>=temp_max)
low=0;
LCD1602_SetTemp(13+0x40,low); LCD1602_WriteCom(0x80+0x4F);// LCD1602_WriteCom(0x80+0x40+13);
}
}
}
}}
void key3_minus()//功能減
{
if(k_num!=0)
{
if(k3==0)
{
Delay1us(5);
if(k3==0)
{
while(!k3);
if(k_num==1)
{
if (set_t<=0)
set_t=high+1;
set_t--;
LCD1602_SetTemp(4+0x40,set_t); LCD1602_WriteCom(0x80+0x46); }
if(k_num==2)
{
if (set_d<=0)
set_d=10;
set_d--;
LCD1602_WriteCom(0x80+0x48);
LCD1602_WriteData(0x30+set_d);
LCD1602_WriteCom(0x80+0x48); }
if(k_num==3)
{
if (high<0)
high=temp_max;
high--;
LCD1602_SetTemp(13,high);
LCD1602_WriteCom(0x80+0x0F);// LCD1602_WriteCom(0x80+0x40+8); }
if(k_num==4)
{
if (low<=0)
low=high;
low--;
LCD1602_SetTemp(13+0x40,low); LCD1602_WriteCom(0x80+0x4F);// LCD1602_WriteCom(0x80+0x40+16); } }
}
}
}
void keyscan()//按鍵檢測
{ key1_switch();
key2_add();
key3_minus();}
void LCD1602_SetTemp(uchar add,uchar dat)//數(shù)碼管制定位置顯示
{
uchar head,body,end;
head=dat/100;
body=dat%100/10;
end=dat%10;
LCD1602_WriteCom(0x80+add);
LCD1602_WriteData(0x30+head);
LCD1602_WriteData(0x30+body);
LCD1602_WriteData(0x30+end);}
void main(
){
LCD1602_Init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if (count==10)//讓溫度每0.5s刷新一次
{ count=0;
LCD1602_ShowTemp(Ds18b20ReadTemp());
}}
復(fù)制代碼
新建文件夾.zip
2019-12-15 18:56 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
157.53 KB, 下載次數(shù): 8, 下載積分: 黑幣 -5
作者:
淪落人
時間:
2021-5-27 23:31
這個有原理圖嗎?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1