原始代码:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Data=P1^6;
uchar rec_dat[9];
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
void delay(uint n)
{ uint x,y;
for(x=n;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_dat(uchar dat)
{
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_lcd()
{
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void DHT11_delay_us(uchar n)
{
while(--n);
}
void DHT11_delay_ms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void DHT11_start()
{
Data=1;
DHT11_delay_us(2);
Data=0;
DHT11_delay_ms(30);
Data=1;
DHT11_delay_us(30);
}
uchar DHT11_rec_byte()
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
while(!Data);
DHT11_delay_us(8);
dat<<=1;
if(Data==1)
dat+=1;
while(Data);
}
return dat;
}
void DHT11_receive()
{
uchar R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
DHT11_start();
if(Data==0)
{
while(Data==0);
DHT11_delay_us(40);
R_H=DHT11_rec_byte();
R_L=DHT11_rec_byte();
T_H=DHT11_rec_byte();
T_L=DHT11_rec_byte();
revise=DHT11_rec_byte();
DHT11_delay_us(25);
if((R_H+R_L+T_H+T_L)==revise)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
}
rec_dat[0]='0'+(RH/10);
rec_dat[1]='0'+(RH%10);
rec_dat[2]='%';
rec_dat[3]=' ';
rec_dat[4]=' ';
rec_dat[5]='0'+(TH/10);
rec_dat[6]='0'+(TH%10);
}
}
void main()
{
uchar i;
init_lcd();
while(1)
{
DHT11_delay_ms(100);
DHT11_receive();
write_com(0x80);
for(i=0;i<9;i++)
write_dat(rec_dat[i]);
write_dat(rec_dat[7]);
write_dat(0xdf);
write_dat('C');
}
}
添加buttoninit的函数之后实现通过按键增加减小温湿度代码:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Data=P1^6;
uchar rec_dat[9];
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
sbit button1add=P2^3;
sbit button1dec=P2^4;
sbit button2add=P3^0;
sbit button2dec=P3^1;
int num1=0,num2=0;
void delay(uint n)
{ uint x,y;
for(x=n;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_dat(uchar dat)
{
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_lcd()
{
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void DHT11_delay_us(uchar n)
{
while(--n);
}
void DHT11_delay_ms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void DHT11_start()
{
Data=1;
DHT11_delay_us(2);
Data=0;
DHT11_delay_ms(30);
Data=1;
DHT11_delay_us(30);
}
uchar DHT11_rec_byte()
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
while(!Data);
DHT11_delay_us(8);
dat<<=1;
if(Data==1)
dat+=1;
while(Data);
}
return dat;
}
void DHT11_receive()
{
uchar R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
DHT11_start();
if(Data==0)
{
while(Data==0);
DHT11_delay_us(40);
R_H=DHT11_rec_byte();
R_L=DHT11_rec_byte();
T_H=DHT11_rec_byte();
T_L=DHT11_rec_byte();
revise=DHT11_rec_byte();
DHT11_delay_us(25);
if((R_H+R_L+T_H+T_L)==revise)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
}
rec_dat[0]='0'+((RH+num1)/10);
rec_dat[1]='0'+((RH+num1)%10);
rec_dat[2]='%';
rec_dat[3]=' ';
rec_dat[4]=' ';
rec_dat[5]='0'+((TH+num2)/10);
rec_dat[6]='0'+((TH+num2)%10);
rec_dat[7]='.';
rec_dat[8]='0'+(TH*10%10);
}
}
void buttoninit()
{
if(button1add==0)
{
if(button1add==0)
{
delay(50);
num1 += 1;
}
}
if(button1dec==0)
{
if(button1dec==0)
{
delay(50);
num1 -= 1;
}
}
if(button2add==0)
{
if(button2add==0)
{
delay(50);
num2 += 1;
}
}
if(button2dec==0)
{
if(button2dec==0)
{
delay(50);
num2 -= 1;
}
}
}
void main()
{
uchar i;
init_lcd();
while(1)
{
buttoninit();
DHT11_delay_ms(100);
DHT11_receive();
write_com(0x80);
for(i=0;i<8;i++)
write_dat(rec_dat[i]);
write_dat(rec_dat[8]);
write_dat(0xdf);
write_dat('C');
}
}
仿真原理图:
温湿度测量仪的仿真实验