Working_Codes_For_DHT11

Following codes are not mine and visit to https://libstock.mikroe.com/ for detail.
These codes are simple and read all 40+1 bits of DHT11. . . .
My test runs on 8Mhz internal OSC of 18f4550.

Library Codes:

#include "dht11.h"
unsigned int DHT11_TMP;
unsigned int DHT11_HUM;
char DHT11_CHKSM;
///////////////////////////////////////////////////
char waitforhigh()
{
 char sayac=0;
 while(dht11_pin==0)
                    {
                     sayac++;
                     delay_us(1);
                     if(sayac>100)return 0;
                    }
}
///////////////////////////////////////////////////
char waitforlow()
{
 char sayac=0;
 while(dht11_pin==1)
                    {
                     sayac++;
                     delay_us(1);
                     if(sayac>100)return 0;
                    }
}
///////////////////////////////////////////////////

long DHT11_Read()
{

 char i=0,datar=0;

 DHT11_TMP=0;DHT11_HUM=0;DHT11_CHKSM=0;
 for(i=0;i<40;i++)
 {
 waitforhigh();
 delay_us(40);
 if(dht11_pin==0)datar=0;
 else{datar=1;waitforlow();}
 if(i<16){ DHT11_HUM|=datar; if(i<15){DHT11_HUM<<=1;}}
 if(i>=16 && i<32){DHT11_TMP|=datar; if(i<31)DHT11_TMP<<=1;}
 if(i>=32&& i<40){DHT11_CHKSM|=datar; if(i<39)DHT11_CHKSM<<=1;}
 }
 delay_ms(1);
 DHT11_DIR=0;
 DHT11_Pin=1;
}
///////////////////////////////////////////////////
void DHT11_Start()
{
   dht11_dir=0;//baslangic
   dht11_pin=1;
   delay_ms(50);
   dht11_pin=0;
   delay_ms(18);
   dht11_pin=1;
   dht11_dir=1;
   delay_us(40);
   waitforhigh();
   waitforlow();
}
///////////////////////////////////////////////////
void DHT11_init()
{
 DHT11_DIR=0;
 DHT11_Pin=1;

}



Example project codes:

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

//DHT11 DEFINITONS
sbit DHT11_Pin at PORTB.B0;
sbit DHT11_DIR at TRISB.B0;
extern unsigned int DHT11_TMP;
extern unsigned int DHT11_HUM;
extern char DHT11_CHKSM;
//END OF DHT11 DEFINITONS

long veri;
int isi,nem;
char bekleme=0;
char txt[7];

//////////////////////////////////////////////

void main() {
short int k;
OSCCON=0xff;
  ADCON1=0xff;
  lcd_init();
  DHT11_init();
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_CLEAR);
  lcd_out(1,1,"Temp.=");
  Lcd_Out(2,1,"Hum.=");
  delay_ms(500);

  while(1)
  {
   Dht11_Start();
   DHT11_Read();

   if(DHT11_CHKSM==((DHT11_TMP>>8)+(DHT11_HUM>>8)+(DHT11_TMP&0xff)+(DHT11_HUM&0xff)))
   {
    inttostr(DHT11_TMP>>8,txt);
    lcd_out(1,8,txt);
    inttostr(DHT11_HUM>>8,txt);
    lcd_out(2,8,txt);
  }
 }
}

Comments

Popular posts from this blog

Inductance caculator

Pointer to object array