1901 views|1 replies

1

Posts

0

Resources
The OP
 

1602 display dht11 temperature and humidity [Copy link]

#include<reg52.h>
#include "intrins.h"
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit rs=P2^6; //Data command selection
sbit rw=P2^5; //Read and write selection
sbit e=P2^7; //Enable

sbit k1=P3^3; //mode
sbit k2=P2^1; //add
sbit k3=P2^2; //subtract

sbit DHT11_DQ_OUT=P3^2;
sbit led1=P3^6;
sbit led2=P3^7;

sbit dq=P2^0;

uint8 mode=0,xian;
char temph=50,templ=20;
char humih=80,humil=20;
uint8 temp,humi;
uint8 flag; //Set alarm flag
uint8 a,c,tempvalue;
uint8 code num[10]="0123456789";

uint8 code str1[]="Temp:"; //Temperature
uint8 code str2[]="Humi:"; //Humidity
uint8 code str3[]="Error";
uint8 code str4[]="Success ";
uint8 code str5[]="%RH";
uint8 code str6[]="TempH:"; //Set the upper limit of temperature display
uint8 code str7[]="TempL:"; //Set the lower limit of temperature display
uint8 code str8[]="HumiH:"; //Set the upper limit of humidity display
uint8 code str9[]="HumiL:"; //Set the lower limit of humidity display


void delay(uint16 i)
{
while(i--);
}

void delay_ms(uint16 i)
{
while(i--)
delay(90);
}

void wrc(uint8 c) //write command
{
delay(1000);
rs=0;
rw=0;
e=0;
P0=c;
e=1;
delay(10);
e=0;
}
void wrd(uint8 dat) //write data
{
delay(1000);
rs=1;
rw=0;
e=0;
P0=dat;
e=1;
delay(10);
e=0;
rs=0;
}
void lcd_init() //LCD1602 initialization
{
delay(1000);
wrc(0x38);
wrc(0x38); //Function setting command, select 8-bit bus, double-line display 5*7 dot matrix characterswrc
(0x38);
wrc(0x06); //Cursor and display mode settings Cursor right move The whole screen does not movewrc
(0x0c); //Display switch control Turn on the display No cursor Cursor does not blink
wrc(0x01); //Clear instruction Fixed
}
//Reset DHT11
void DHT11_Rst()
{
DHT11_DQ_OUT=0; //Pull DQ low
delay_ms(20); //Pull low for at least 18ms
DHT11_DQ_OUT=1; //DQ=1
delay(3); //Host pulls high for 20~40us
}

//Wait for the response from DHT11
//Return 1: DHT11 is not detected
//Return 0: It exists
uint8 DHT11_Check()
{
uint8 retry=0;
while (DHT11_DQ_OUT&&retry<100)//DHT11 will pull down for 40~50us
{
retry++ ; _nop_()
;
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_OUT&&retry<100)//DHT11 will pull up again after being pulled down for 40~50us
{
retry++;
_nop_();
};
if(retry>=100)return 1;
return 0;
}


//DHT11 initialization
//Return 0: initialization successful, 1: failure
uint8 DHT11_Init()
{
DHT11_Rst();
return DHT11_Check();
}


//Read a bit from DHT11
//Return value: 1/0
uint8 DHT11_Read_Bit(void)
{
uint8 retry=0;
while(DHT11_DQ_OUT&&retry<100)//Wait for it to become low level for 12-14us start
{
retry++;
_nop_();
}
retry=0;
while((!DHT11_DQ_OUT)&&retry<100)//Wait for it to become high level 26-28us means 0, 116-118us means 1
{
retry++;
_nop_();
}
delay(1);//Wait for 40us
if(DHT11_DQ_OUT)return 1;
else return 0;
}

//Read a byte from DHT11
//Return value: the read data
uint8 DHT11_Read_Byte(void)
{
uint8 i,dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}

//Read data from DHT11 once
//temp: temperature value (range: 0~50°)
//humi: humidity value (range: 20%~90%)
//Return value: 0, normal; 1, reading failed
uint8 DHT11_Read_Data(uint8 *temp,uint8 *humi)
{
uint8 buf[5];
uint8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)//Read 40 bits of data
{
buf=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}

}else return 1;
return 0;
}


void ds18b20init() //18b20 initialization
{
dq=1;
delay(1);
dq=0;
delay(80);
dq=1;
delay(5);
dq=0;
delay(20);
dq=1;
delay(35);
}
void ds18b20wr(uint8 dat) //18b20 write data
{
uint8 i;
for(i=0;i<8;i++)
{
dq=0;
dq=dat&0x01;
dat>>=1;
delay(8);//Only this block has the most accurate timing requirement, its time must be greater than 15us
dq=1;
delay(1);
}
}
uint8 ds18b20rd() //18b20 read data
{
uint8 value,i;
for(i=0;i<8;i++)
{
dq=0;
value>>=1;
dq=1;
if(dq==1)value|=0x80;
delay(8);//In this section, the time requirement is also very accurate, the whole program must be greater than 60us
}
return value;
}
uint8 readtemp() //Read the temperature that needs to be reset
{
uint8 b;
ds18b20init(); //Initialize
ds18b20wr(0xcc); //Send the ignore ROM command
ds18b20wr(0x44); //Send the temperature conversion command
delay(100);
ds18b20init(); //Initialize
ds18b20wr(0xcc); //Send the ignore ROM command
ds18b20wr(0xbe); //Send the read register command
a=ds18b20rd(); //The low eight bits of temperature
b=ds18b20rd(); //The high eight bits of temperature
b<<=4; //ssss s***; s is the flag s=0 means the temperature is positive, s=1 means the temperature is negative
c=b&0x80; //Confirm the positive and negative flag of temperature
b+=(a&0xf0)>>4;
a=a&0x0f; //Decimal part of temperature
return b;
}


void key_pros() //Key processing function
{
if(k1==0)
{
delay(1000);
if(k1==0)
{
mode++;
if(mode==5)mode=0;
wrc(0x01);
}
while(!k1);
}
if(mode==1) //Set the upper limit of temperature
{
if(k2==0) //Add
{
delay(1000);
if(k2==0)
{
temph++;
if(temph>=80)temph=80;
}
while(!k2);
}
if(k3==0) //Subtract
{
delay(1000);
if(k3==0)
{
temph--;
if(temph<=0)temph=0;
}
while(!k3);
}
}
if(mode==2) //Set the lower limit of temperature
{
if(k2==0) //Add
{
delay(1000);
if(k2==0)
{
templ++;
if(templ>=80)templ=80;
}
while(!k2);
}
if(k3==0) //Subtract
{
delay(1000);
if(k3==0)
{
templ--;
if(templ<=0)templ=0;
}
while(!k3);
}
}
if(mode==3) //Set the upper limit of humidity
{
if(k2==0) //Add
{
delay(1000);
if(k2==0)
{
humih++;
if(humih>=80)humih=80;
}
while(!k2);
}
if(k3==0) //Subtract
{
delay(1000);
if(k3==0)
{
humih--;
if(humih<=0)humih=0;
}
while(!k3);
}
}
if(mode==4) //Set the lower limit of humidity
{
if(k2==0) //Add
{
delay(1000);
if(k2==0)
{
humil++;
if(humil>=80)humil=80;
}
while(!k2);
}
if(k3==0) //减
{
delay(1000);
if(k3==0)
{
humil--;
if(humil<=0)humil=0;
}
while(!k3);
}
}
}

void lcd_init_display() //LCD initialization display
{
uint8 i;
for(i=0;i<5;i++)
{
wrc(0x80+i);
wrd(str1);
}

for(i=0;i<5;i++)
{
wrc(0xc0+i);
wrd(str2);
}
}

void data_pros() //data processing function
{
uint8 i;
uint8 temp_buf[2],humi_buf[2];
uint8 temphbuf[2],templbuf[2],humihbuf[2],humilbuf[2];
float dio;
uint16 k;

tempvalue=readtemp();
DHT11_Read_Data(&temp,&humi);
temp_buf[0]=temp/10+0x30;
temp_buf[1]=temp%10+0x30;

humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;

dio=a*0.0625;
k=dio*10000; // take the two significant digits after the decimal point

temphbuf[0]=temph/10+0x30;
tempbuf[1]=temph%10+0x30;
templbuf[0]=templ/10+0x30;
templbuf[1]=templ%10+0x30;

humihbuf[0]=humih/10+0x30;
humihbuf[1]=humih%10+0x30;
humblebuf[0]=humble/10+0x30;
humblebuf[1]=humble%10+0x30;

if(mode==0)
{
lcd_init_display();
wrc(0x85);
wrd(num[tempvalue%100/10]);
wrd(num[tempvalue%100%10]);
wrd('.');
wrd( num[k/1000]);
wrd(0xdf);
wrd('C');

for(i=0;i<2;i++)
{
wrc(0Xc5+i);
wrd(humi_buf);
}
for (i=0;i<3;i++)
{
wrc(0Xc7+i);
wrd(str5);
}
}
if(mode==1) //The upper limit of temperature display
{
wrc(0x80);
for(i =0;i<6;i++)
{
wrd(str6);
}
wrd(temphbuf[0]);
wrd(temphbuf[1]);
}
if(mode==2) //Temperature lower limit display
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str7);
}
wrd(templbuf[0]);
wrd(templbuf[1]);
}
if(mode==3) //Humidity upper limit display
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str8);
}
wrd(humihbuf[0]);
wrd( humihbuf[1]);
}
if(mode==4) //humidity lower limit display
{
wrc(0x80);
for(i=0;i<6;i++)
{
wrd(str9);
}
wrd(humilbuf [0]);
wrd(humilbuf[1]);
}
}

void baojinpros() //Alarm processing
{
if(tempvalue>=temph||humi>=humih) //The detected temperature or humidity is higher than the set upper limit, cooling and humidity
{
led1=1; //The cooling and humidity indicator
led2=0;
}
if(tempvalue<=templ||humi<=humil) //The detected temperature or humidity is lower than the set lower limit, heating and humidity
{
led1=0;
led2=1; //Increasing the temperature and humidity indicator
}
if((tempvalue>templ&&tempvalue<temph)&&(humi>humil&&humi<humih))
{
led1=0;
led2=0;
}
}

void main()
{
uint8 i=0;
led1=0;
led2=0;
lcd_init();
while(DHT11_Init()) //Check if DHT11 is pure in
{
for(i=0;i<5;i++)
{
wrc(0x80+i);
wrd(str3);
}
}
wrc(0x01);
lcd_init_display(); //LCD initialization displayi
=0;
while(1)
{
i++;
key_pros();
baojinpros(); //Alarm processingif
(i==15)
{
i=0;
data_pros(); //Reading DHT11 data once must be at least greater than 100ms
}
delay(1000);

}
}

This post is from Programming Basics

Latest reply

Good, keep it up. It would be best if you could show it in person.  Details Published on 2021-10-11 17:50
 

1412

Posts

3

Resources
2
 
Good, keep it up. It would be best if you could show it in person.
This post is from Programming Basics
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Featured Posts
Analog Circuit Basics Tutorial (eBook)

Come and download http://www.netyi.net/Resource/13373d8c-cc93-4c91-b5d2-199c75f46cf8.aspx

Application skills/Software and hardware implementation of serial communication between PIC16 series microcontroller and PC

Abstract: This paper introduces a method of using PIC16F84 microcontroller to realize serial communication with PC, ...

Wireless LAN Terminology Analysis

PCI slot wireless network card: allows your computer to communicate with other computers on the network without cables. ...

GPIO block diagram of C6000 series DSP

First, take a look at the GPIO block diagram: 509171 By looking at the GPIO block diagram, we can get a lot of informati ...

Why Python is the programming language of the future

Python is a powerful language that serves a variety of purposes for developers around the world. According to the TIOBE ...

Internal referral for multiple positions (campus recruitment and social recruitment)

Company Name: Fashion Networks Hangzhou Branch Find a job you love Do what you like There is a group of young people w ...

31 "Millions of Miles" Raspberry Pi Car——Ubuntu MATE System Installation

Next, I was going to start learning ROS, but it was particularly difficult to install it on the Raspberry Pi operating ...

37 "Ten Thousand Miles" Raspberry Pi Car——ROS Learning (VSCode Debugging ROS)

This post was last edited by lb8820265 on 2022-5-10 00:27 I have introduced VSCode debugging before , which is GDB deb ...

Award Ceremony: ADIWorld Health Industry Carnival - e-book download

ADIWorld Health Industry Carnival—— E-book download event awards ceremony Please see the list below for details. Winn ...

Unboxing review based on MSPM0L1306 LaunchPad

I received the MSPM0L1306 LaunchPad development kit a few years ago. The MSPM0L1306 is a 32-bit Arm Cortex -M0+ CPU ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list