基于51单片机的温湿度测量控制程序,使用DHT11数字传感器,用1602液晶屏显示,按键定义如下:
sbit key_1=P0^1; //确定按键
sbit key_2=P0^2; //按键加
sbit key_3=P0^3; // 按键减
本程序中用到的头文件 12c5a.h下载: http://www.51hei.com/f/12c5a.rar
#include "12c5a.H"
#include "intrins.h"
unsigned char code num[]={"0123456789"};//显示字符存储
unsigned char code start_char[]={"Loading...."};
unsigned char code t_char[]={"NOW TEMP IS"};
unsigned char *pnum=num;
unsigned int bbb,ccc,temp,temp_high,temp_low;
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //540 clocks
#define ADC_SPEEDL 0x20 //360 clocks
#define ADC_SPEEDH 0x40 //180 clocks
#define ADC_SPEEDHH 0x60 //90 clocks
sbit RS=P3^4; //数据命令选择端
sbit RW=P3^5; //读写控制端
sbit E=P3^6; //使能信号控制端
//sbit key_0=P0^0; //设置按键
sbit key_1=P0^1; //确定按键
sbit key_2=P0^2; //按键加
sbit key_3=P0^3; // 按键减
sbit P04=P0^4;
sbit P05=P0^5;
sbit P06=P0^6;
void key_temphigh();
void key_templow();
void key_scan();
void delay_1602(unsigned int a);
void init_1602(void);
void WR_data_1602(unsigned char adata,unsigned char i);
void process(unsigned int shuju);
void Delay(unsigned char n)
{
int x;
while (n--)
{
x = 500;
while (x--);
}
}
void init_ADC(void)
{
P1ASF=0xFF;
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = 0x88;
Delay(2);
}
unsigned int GetResult(void)
{
unsigned char m,n;
unsigned int aaa;
init_ADC();
while (!(ADC_CONTR & ADC_FLAG)); //Wait complete flag
ADC_CONTR =0x80; //Close ADC
m=ADC_RES;
n=ADC_RESL;
aaa=m*4+n;
return aaa;
}
void start_init(void)
{
unsigned char i;
WR_data_1602(0x80,0); //显示在第一行
for(i=0;i<11;i++)
WR_data_1602(start_char[i],1);
delay_1602(20);
init_ADC();
temp=GetResult()*5.0;
WR_data_1602(0x80,0); //显示在第一行
for(i=0;i<11;i++)
WR_data_1602(t_char[i],1);
}
void delay_1602(unsigned int a) //延时
{
unsigned int i,j,k;
for(i=0;i<a;i++)
for(j=0;j<106;j++)
for(k=0;k<100;k++);
}
void key_temphigh() // 温度上限判断
{ if(key_2==0) //进行加判断
{ Delay(3);
if(key_2==0)
{ while(!key_2);
temp_high+=5;
if(temp_high==995)
{temp_high=5;}
}
}
if(key_3==0) //进行减判断
{ Delay(3);
if(key_3==0)
{ while(!key_3); //等待松手
temp_high-=5; //上限报警温度每次减0。5
if(temp_high==0)
{temp_high=990;}
}
}
}
void key_templow() // 温度下限判断
{ if(key_2==0) //进行加判断
{ Delay(3);
if(key_2==0)
{ while(!key_2);
temp_low+=5;
if(temp_low==995)
{temp_low=5;}
}
}
if(key_3==0) //进行减判断
{ Delay(3);
if(key_3==0)
{ while(!key_3); //等待松手
temp_low-=5; // 上限温度每次减0。5
if(temp_low== 0)
{temp_low=990;}
}
}
}
void key_scan()
{ unsigned char nm=0,k;
if(key_1==0) //检测设置按键,判断是否进行设置
{ if(key_1==0)
{ Delay(100) ;
while(!key_1);
k=1;nm++;
}
}
while(k) //进入调整界面,
{ if(key_1==0)
{ Delay(3);
if(key_1==0)
{ while(!key_1);
nm++; //调整哪个的参数
if(nm==4){nm=1;}
}
}
switch(nm) //选择调整的对象
{ case 1:WR_data_1602(0xc0,0);WR_data_1602('H',1);process(temp_high);key_temphigh();break;
case 2:WR_data_1602(0xc0,0);WR_data_1602('L',1);process(temp_low);key_templow();break;
case 3:WR_data_1602(0xc0,0);WR_data_1602(' ',1);process(temp);nm=0;k=0;break;
default :break;
}
/* if(key_1==0) //确定并退出
{Delay(3);
if(key_1==0)
{while(!key_1);
nm=0;k=0;WR_data_1602(0xc1,0);process(temp);
}
} */
if(k==0)
{continue;}//结束循环
}
}
void baojing()
{
if(temp>temp_high-5) {P04=1;P05=0;P06=1;} // 上限25
else if(temp<temp_low+5) {P04=0;P05=1;P06=1;} // 下限20
else {P04=1;P05=1;P06=0;}
}
void init_1602(void) //初始化
{
WR_data_1602(0x38,0);
WR_data_1602(0x01,0);
WR_data_1602(0x06,0);
WR_data_1602(0x0c,0);
}
void WR_data_1602(unsigned char adata,unsigned char i) //写数据i=1,写指令i=0
{
P2=adata;
RS=i;
RW=0;
E=1;
delay_1602(1);
E=0;
RW=1;
RS=0;
}
void process(unsigned int shuju) //显示数据处理
{
unsigned int a,b,c,d;
a=shuju/1000;
b=(shuju%1000)/100;
c=(shuju%100)/10;
d=shuju%10;
if(a==0&&b!=0){WR_data_1602(0xc2,0);
WR_data_1602(*(pnum+b),1);
WR_data_1602(*(pnum+c),1);
WR_data_1602('.',1);
WR_data_1602(*(pnum+d),1);
WR_data_1602('C',1);}
if(a==0&&b==0){WR_data_1602(0xc2,0);
WR_data_1602(*(pnum+c),1);
WR_data_1602('.',1);
WR_data_1602(*(pnum+d),1);
WR_data_1602('C',1);
WR_data_1602(' ',1);}
//WR_data_1602(0xc1,0); //显示在第二行
//WR_data_1602(' ',1);
//WR_data_1602(*(pnum+a),1);
// WR_data_1602(*(pnum+b),1);
//WR_data_1602(*(pnum+c),1);
// WR_data_1602('.',1);
// WR_data_1602(*(pnum+d),1);
// WR_data_1602('C',1);
delay_1602(1);
}
void main() //主函数
{
unsigned int mm;
init_1602(); //初始化
start_init();
mm=GetResult();
temp_high=250;
temp_low=90;
while(1)
{
key_scan();
baojing();
ccc=GetResult();
temp=ccc*5.0;
process(temp);
delay_1602(10);
}
}