dht11.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "dht11.h"
  2. //32MHZ us延时函数;
  3. #pragma optimize=none
  4. void dht11_delay_us(unsigned int n)
  5. {
  6. n>>=1;
  7. while(n--)
  8. {
  9. asm("NOP");
  10. asm("NOP");
  11. asm("NOP");
  12. asm("NOP");
  13. asm("NOP");
  14. asm("NOP");
  15. asm("NOP");
  16. asm("NOP");
  17. asm("NOP");
  18. asm("NOP");
  19. asm("NOP");
  20. asm("NOP");
  21. asm("NOP");
  22. asm("NOP");
  23. asm("NOP");
  24. }
  25. }
  26. unsigned char dht11_read_byte(void)
  27. {
  28. unsigned char r_val = 0;
  29. unsigned char t_count = 0; //计时器,防止超时;
  30. unsigned char i;
  31. for(i = 0 ; i < 8 ; i++)
  32. {
  33. t_count = 0;
  34. //低电平50us后开始一个数据位读取;
  35. while( !DHT11_PIN )
  36. {
  37. asm("NOP");
  38. t_count++;
  39. if(t_count > 250) //超时;
  40. return 100;
  41. }
  42. t_count = 0;
  43. dht11_delay_us(32); //32us
  44. //高电平26~28us表示'0',70us表示'1'
  45. if( DHT11_PIN == 1 )
  46. {
  47. r_val <<= 1;
  48. r_val |= 1;
  49. }
  50. else
  51. {
  52. r_val <<= 1;
  53. continue;
  54. }
  55. //等待DHT11数据输出结束;
  56. while( DHT11_PIN == 1)
  57. {
  58. asm("NOP");
  59. t_count++;
  60. if(t_count>250)
  61. {
  62. return 100;
  63. }
  64. }
  65. }
  66. return r_val;
  67. }
  68. char dht11_value(unsigned char *temp , unsigned char *humi , unsigned char flag)
  69. {
  70. unsigned char t_count = 0; //计时器;
  71. unsigned char h_i = 0 , h_f = 0;
  72. unsigned char t_i = 0 , t_f = 0;
  73. unsigned char check_sum = 0;
  74. DHT11_PIN_OUT();
  75. DHT11_PIN_L(); //输出低电平;
  76. //低电平持续时间必须大于18ms;
  77. dht11_delay_us(20000); //20ms;
  78. DHT11_PIN_H(); //主机结束信号,高电平;
  79. //主机等待20us~40us,读取DHT11响应输出;
  80. dht11_delay_us(30);
  81. DHT11_PIN_IN();
  82. if(DHT11_PIN == 0) //正确的响应输出;
  83. {
  84. while( !DHT11_PIN )
  85. {
  86. asm("NOP");
  87. t_count++;
  88. if(t_count > 250) //超时;
  89. return -1;
  90. }
  91. t_count = 0;
  92. dht11_delay_us(50); //DHT11给出响应输出后会拉高总线80us;
  93. while( DHT11_PIN ); //等待接收;
  94. {
  95. asm("NOP");
  96. t_count++;
  97. if(t_count > 250) //超时;
  98. return -1;
  99. }
  100. h_i = dht11_read_byte(); //湿度整数部分;
  101. h_f = dht11_read_byte(); //湿度小数部分;
  102. t_i = dht11_read_byte(); //温度整数部分;
  103. t_f = dht11_read_byte(); //温度小数部分;
  104. check_sum = dht11_read_byte(); //校验和;
  105. //校验和正确或者温湿度整数部分获取正确即表示获取成功!
  106. if(check_sum == ( h_i + h_f + t_i + t_f ) || (h_i != 100 && t_i != 100) )
  107. {
  108. if(flag == DHT11_STRING)
  109. {
  110. temp[0] = t_i/10+0x30;
  111. temp[1] = t_i%10+0x30;
  112. humi[0] = h_i/10+0x30;
  113. humi[1] = h_i%10+0x30;
  114. }
  115. else
  116. {
  117. *temp = t_i;
  118. *humi = h_i;
  119. }
  120. }
  121. else
  122. {
  123. // if(flag == DHT11_STRING)
  124. // {
  125. // temp[0] = '0';
  126. // temp[1] = '0';
  127. // humi[0] = '0';
  128. // humi[1] = '0';
  129. // }
  130. // else
  131. // {
  132. // *temp = 0;
  133. // *humi = 0;
  134. // }
  135. return -1;
  136. }
  137. }
  138. else
  139. {
  140. // if(flag == DHT11_STRING)
  141. // {
  142. // temp[0] = '0';
  143. // temp[1] = '0';
  144. // humi[0] = '0';
  145. // humi[1] = '0';
  146. // }
  147. // else
  148. // {
  149. // *temp = 0;
  150. // *humi = 0;
  151. // }
  152. return -1;
  153. }
  154. return 0;
  155. }