This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10156|Reply: 0

pic16f877a drives the temperature and humidity sensor DHT11

[Copy link]
Posted on 11/25/2014 10:12:09 PM | | |
[mw_shl_code=c,true]#include<pic.h> // Use 12M crystal oscillator
#include"12864.h"
#define uint unsigned int
#define uchar unsigned char
__CONFIG(FOSC_HS&WDTE_OFF&LVP_OFF);
uchar temp_valueH,hum_valueH,hum_valueL,temp_valueL,check;
uint delay_time;
void timer0_init()
{
   TMR0=0;  First value
   T0CS=0;  Select the clock source, which is 1/4 of the crystal oscillator;
   T0SE=1;   Clock source edge selection bits
   PSA=0;    Predivider allocation bits
   PS0=0; PS1=0; PS2=0;  Pre-division selection bits
   TMR0IF=0;    The timer flag is clear 0
   GIE=1;       Open the global interruption
   TMR0IE=0;    Timer 0 enabled
   TMR0=236;    The first value of the poem
}
void interrupt  timer0()
{
     if(TMR0IF)
     {
       TMR0IF=0; TMR0=235;
       delay_time--;
     }   
}
void timer0_delay(uint delay_value)
{
    delay_time=delay_value;
    TMR0IE=1;
    while(delay_time);
    TMR0IE=0;
}
uchar Read_byte()
{
uchar i,temp,hum;
   for(i=0; i<8; i++)   
    {
       while(! RC1);   
       timer0_delay(2); Delay 20us  
       temp=0;
       if(RC1){temp=1; while(RC1); }
       else{temp=0; }   
       hum<<=1;
       hum|=temp;                           
  }
   return(hum);
}
void Read_data()
{
uchar ckeck_value;
RC1=0;
timer0_delay(2500); The bus is pulled down by at least 18 MS
RC1=1;
timer0_delay(2); The bus is pulled up by the pull-up resistor and the host delay is 20us
TRISC1=1;     The host is set to input to judge the slave response signal
RC1=1;
  if(! RC1)
  {
   while((! RC1)) // Determine whether the slave emits an 80us low response signal or not
   {
    NOP();
   }
   while(RC1) // Determine whether the slave emits a high level of 80us, and if so, it enters the data receiving state
   {
    NOP();
   }
   temp_valueH=Read_byte();
   temp_valueL=Read_byte();
   hum_valueH=Read_byte();
   hum_valueL=Read_byte();
   check=Read_byte();
   ckeck_value=hum_valueL+hum_valueH+temp_valueL+temp_valueH;
  if(check==ckeck_value)
   {
      Proce_num(0x84,temp_valueH);
      Proce_num(0x94,hum_valueH);
   }
  }
  TRISC1=0;
  RC1=1;
}
void main()
{
  TRISB=0X00;
  lcdinit();
  timer0_init();
  while(1)
  {
   Read_data();
  }
}[/mw_shl_code]




Previous:Thought and Naked Programming [Serialized]
Next:Welfare benefits, C language naming rules, very practical things
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com