123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #include "dht11.h"
- void dht11_Delay32us(void)
- {
- unsigned char i;
- i = 12;
- while (--i);
- }
- void dht11_Delay20ms(void)
- {
- unsigned char i, j;
- i = 36;
- j = 217;
- do
- {
- while (--j);
- } while (--i);
- }
- void dht11_Delay50us(void)
- {
- unsigned char i;
- _nop_();
- i = 20;
- while (--i);
- }
- unsigned char dht11_read_byte(void)
- {
- unsigned char r_val = 0;
- unsigned char t_count = 0;
- unsigned char i;
-
- for(i = 0 ; i < 8 ; i++)
- {
- t_count = 0;
-
-
- while( !DHT11_PIN )
- {
- _nop_();
- t_count++;
- if(t_count > 250)
- return 100;
- }
- t_count = 0;
-
- dht11_Delay32us();
-
-
- if( DHT11_PIN == 1 )
- {
- r_val <<= 1;
- r_val |= 1;
- }
- else
- {
- r_val <<= 1;
- continue;
- }
-
-
- while( DHT11_PIN == 1)
- {
- _nop_();
- t_count++;
- if(t_count>250)
- {
- return 100;
- }
- }
- }
- return r_val;
- }
- char dht11_value(unsigned char *temp , unsigned char *humi , unsigned char flag)
- {
- unsigned char t_count = 0;
- unsigned char h_i = 0 , h_f = 0;
- unsigned char t_i = 0 , t_f = 0;
- unsigned char check_sum = 0;
-
- DHT11_PIN_OUT();
- DHT11_PIN_L();
-
-
- dht11_Delay20ms();
-
- DHT11_PIN_H();
-
-
- dht11_Delay32us();
-
- DHT11_PIN_IN();
- if(DHT11_PIN == 0)
- {
- while( !DHT11_PIN )
- {
- _nop_();
- t_count++;
-
- if(t_count > 250)
- return -1;
- }
-
- t_count = 0;
-
- dht11_Delay50us();
- while( DHT11_PIN );
- {
- _nop_();
- t_count++;
-
- if(t_count > 250)
- return -1;
- }
- EA=0;
- h_i = dht11_read_byte();
- h_f = dht11_read_byte();
- t_i = dht11_read_byte();
- t_f = dht11_read_byte();
- check_sum = dht11_read_byte();
- EA=1;
-
- if(check_sum == ( h_i + h_f + t_i + t_f ) || (h_i != 100 && t_i != 100) )
- {
- if(flag == DHT11_STRING)
- {
- temp[0] = t_i/10+0x30;
- temp[1] = t_i%10+0x30;
- humi[0] = h_i/10+0x30;
- humi[1] = h_i%10+0x30;
- }
- else
- {
- if(t_i<=50&&h_i<95)
- {
- *temp = t_i;
- *humi = h_i;
- }
- }
- }
- else
- {
-
- return -1;
- }
- }
- else
- {
- return -1;
- }
- return 0;
- }
|