Sunteți pe pagina 1din 10

DHT11 Sensor Interfacing with 8051.

Introduction
DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and
temperature values serially.
It can measure relative humidity in percentage (20 to 90% RH) and temperature in degree
Celsius in the range of 0 to 50°C.
It has 4 pins of which 2 pins are used for supply, 1 is not used and the last one is used for data.

d
CKT DIAGRAM
PROGRAM

#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"

sbit DHT11=P2^1; /* Connect DHT11 Sensor Pin to P2.1 Pin */


int I_RH,D_RH,I_Temp,D_Temp,CheckSum;

void timer_delay20ms() /* Timer0 delay function */


{
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0x0C;
TR0 = 1;
while(TF0 == 0);
TR0 = 0;
TF0 = 0;
}

void timer_delay30us()
{
TMOD = 0x01; *
TH0 = 0xFF;
TL0 = 0xF1;
TR0 = 1;
while(TF0 == 0);
TR0 = 0;
TF0 = 0;
}

void Request() /* Microcontroller send start pulse or request */


{
DHT11 = 0; /* set to low pin */
timer_delay20ms();/* wait for 20ms */
DHT11 = 1; /* set to high pin */
}

void Response() /* receive response from DHT11 */


{
while(DHT11==1);
while(DHT11==0);
while(DHT11==1);
}

int Receive_data() /* receive data */


{
int q,c=0;
for (q=0; q<8; q++)
{
while(DHT11==0); /* check received bit 0 or 1 */
timer_delay30us();
if(DHT11 == 1) /* if high pulse is greater than 30ms */
c = (c<<1)|(0x01); /* then its logic HIGH */

else /* otherwise its logic LOW */


c = (c<<1);

while(DHT11==1);
}
return c;
}

void main()
{
unsigned char dat[20];
LCD_Init(); /* initialize LCD */

while(1)
{
Request(); /* send start pulse*/
Response(); /* receive response */

I_RH=Receive_data(); /* store first eight bit in I_RH */


D_RH=Receive_data(); /* store next eight bit in D_RH */
I_Temp=Receive_data(); /* store next eight bit in I_Temp */
D_Temp=Receive_data(); /* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */

if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)


{
LCD_String_xy(0,0,"Error");
}
else
{
sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
memset(dat,0,20);
sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
delay(100);
}
}
Lm35 temp sensor with 8051
Ckt daigram

Connection Diagram

LCD:
 RS : P0.5
 RW : P0.6
 EN : P0.7
 Data Lines : P2
ADC:
 RD : P0.0
 WR : P0.1
 INTR : P0.2
 Output : P1
PROGRAM
#include<reg51.h>

#define delay for(i=0;i<1000;i++);


#define lcd P2

sbit rd=P0^0;
sbit wr=P0^1;
sbit intr=P0^2;

sbit rs=P0^5;
sbit rw=P0^6;
sbit en=P0^7;

void lcd_int();
void cmd(unsigned int b);
void dat(unsigned int c);
void show(unsigned char *s);

unsigned char adc(),get_value, conv;


int i;
void main()
{
lcd_int();
show("Temp : ");
while(1)
{
get_value = adc();
cmd(0x87);
dat((get_value/100)+48);
dat(((get_value/10)%10)+48);
dat((get_value%10)+48);
dat(0x60);
dat('C');
}
}

void lcd_int()
{
cmd(0x38);
cmd(0x0e);
cmd(0x06);
cmd(0x01);
cmd(0x80);
}

void cmd(unsigned int b)


{
lcd=b;
rs=0;
rw=0;
en=1;
delay;
en=0;
}

void dat(unsigned int c)


{
lcd=c;
rs=1;
rw=0;
en=1;
delay;
en=0;
}

void show(unsigned char *s)


{
while(*s)
dat(*s++);
}

unsigned char adc()


{
wr=0;
rd=1;
wr=1;
while(intr==1);
rd=0;
conv=P1;

return conv;
}

S-ar putea să vă placă și