DHT11.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "main.h"
  2. //#include "delay.h"
  3. static void DHT11_GPIO_Init(void);
  4. static void DHT11_Mode_IPU(void);
  5. static void DHT11_Mode_Out_PP(void);
  6. static void DHT11_Reset(void);
  7. static uint8_t DHT11_Check(void);
  8. static uint8_t DHT11_Readbit(void);
  9. static uint8_t DHT11_ReadByte(void);
  10. static uint8_t DHT11_Read_TempAndHumidity(DHT11_Data_TypeDef *DHT11_Data);
  11. DHT11ClassStruct DHT11Class = {
  12. .Init = DHT11_GPIO_Init,
  13. .Read = DHT11_Read_TempAndHumidity
  14. };
  15. // 使用此变量接收DHT11数据
  16. DHT11_Data_TypeDef DHT11Data;
  17. #define DHT11_Dout_GPIO_CLK RCC_APB2Periph_GPIOA
  18. #define DHT11_Dout_GPIO_PORT GPIOA
  19. #define DHT11_Dout_GPIO_PIN GPIO_Pin_15
  20. #define DHT11_Dout_0 GPIO_ResetBits(DHT11_Dout_GPIO_PORT, DHT11_Dout_GPIO_PIN)
  21. #define DHT11_Dout_1 GPIO_SetBits(DHT11_Dout_GPIO_PORT, DHT11_Dout_GPIO_PIN)
  22. #define DHT11_Dout_IN() GPIO_ReadInputDataBit(DHT11_Dout_GPIO_PORT, DHT11_Dout_GPIO_PIN)
  23. /**
  24. * @brief DHT11初始化
  25. * @param None
  26. * @retval None
  27. * @note None
  28. */
  29. static void DHT11_GPIO_Init(void)
  30. {
  31. GPIO_InitTypeDef GPIO_InitStructure;
  32. RCC_APB2PeriphClockCmd(DHT11_Dout_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
  33. GPIO_InitStructure.GPIO_Pin = DHT11_Dout_GPIO_PIN;
  34. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  35. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  36. GPIO_Init(DHT11_Dout_GPIO_PORT, &GPIO_InitStructure );
  37. DHT11_Dout_1;
  38. }
  39. /**
  40. * @brief 修改引脚为浮空输入
  41. * @param None
  42. * @retval None
  43. * @note None
  44. */
  45. static void DHT11_Mode_IPU(void)
  46. {
  47. GPIO_InitTypeDef GPIO_InitStructure;
  48. GPIO_InitStructure.GPIO_Pin = DHT11_Dout_GPIO_PIN;
  49. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  50. GPIO_Init(DHT11_Dout_GPIO_PORT, &GPIO_InitStructure);
  51. }
  52. /**
  53. * @brief 修改引脚为推挽输出
  54. * @param None
  55. * @retval None
  56. * @note None
  57. */
  58. static void DHT11_Mode_Out_PP(void)
  59. {
  60. GPIO_InitTypeDef GPIO_InitStructure;
  61. GPIO_InitStructure.GPIO_Pin = DHT11_Dout_GPIO_PIN;
  62. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  63. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  64. GPIO_Init(DHT11_Dout_GPIO_PORT, &GPIO_InitStructure);
  65. }
  66. /**
  67. * @brief DHT11复位
  68. * @param None
  69. * @retval None
  70. * @note None
  71. */
  72. static void DHT11_Reset(void)
  73. {
  74. DHT11_Mode_Out_PP();
  75. DHT11_Dout_0;
  76. DELAYClass.DelayMs(20);
  77. DHT11_Dout_1;
  78. DELAYClass.DelayUs(30);
  79. }
  80. /**
  81. * @brief 检测DHT11状态
  82. * @param None
  83. * @retval 0: 在线,1: 未检测到设备
  84. * @note None
  85. */
  86. static uint8_t DHT11_Check(void)
  87. {
  88. uint8_t timeout = 0;
  89. DHT11_Mode_IPU();
  90. while (DHT11_Dout_IN() && timeout < 100)
  91. {
  92. timeout++;
  93. DELAYClass.DelayUs(1);
  94. }
  95. if(timeout >= 100)
  96. {
  97. return 1;
  98. }
  99. timeout = 0;
  100. while (!DHT11_Dout_IN() && timeout < 100)
  101. {
  102. timeout++;
  103. DELAYClass.DelayUs(1);
  104. };
  105. if(timeout >= 100)
  106. {
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. /**
  112. * @brief DHT11读取1bit
  113. * @param None
  114. * @retval 返回读取的bit
  115. * @note None
  116. */
  117. static uint8_t DHT11_Readbit(void)
  118. {
  119. uint8_t timeout = 0;
  120. while (DHT11_Dout_IN() && timeout < 100)
  121. {
  122. timeout++;
  123. DELAYClass.DelayUs(1);
  124. }
  125. timeout = 0;
  126. while (!DHT11_Dout_IN() && timeout < 100)
  127. {
  128. timeout++;
  129. DELAYClass.DelayUs(1);
  130. }
  131. DELAYClass.DelayUs(40);
  132. if(DHT11_Dout_IN())
  133. return 1;
  134. return 0;
  135. }
  136. /**
  137. * @brief DHT11读取1Byte
  138. * @param None
  139. * @retval None
  140. * @note 返回读取的Byte
  141. */
  142. static uint8_t DHT11_ReadByte(void)
  143. {
  144. uint8_t dat = 0;
  145. for (uint8_t i = 1; i <= 8; i++)
  146. {
  147. dat <<= 1;
  148. dat |= DHT11_Readbit();
  149. }
  150. return dat;
  151. }
  152. /**
  153. * @brief DHT11读取40bit 全部数据
  154. * @param None
  155. * @retval None
  156. * @note 8bit 湿度整数 + 8bit 湿度小数 + 8bit 温度整数 + 8bit 温度小数 + 8bit 校验和
  157. */
  158. static uint8_t DHT11_Read_TempAndHumidity(DHT11_Data_TypeDef *DHT11_Data)
  159. {
  160. DHT11_Reset();
  161. if(DHT11_Check() == 0)
  162. {
  163. DHT11_Data->humi_int = DHT11_ReadByte();
  164. DHT11_Data->humi_deci = DHT11_ReadByte();
  165. DHT11_Data->temp_int = DHT11_ReadByte();
  166. DHT11_Data->temp_deci = DHT11_ReadByte();
  167. DHT11_Data->check_sum = DHT11_ReadByte();
  168. #ifdef DEBUG_printf
  169. printf("tem:%d hum:%d\n", DHT11_Data->temp_int, DHT11_Data->humi_int);
  170. #endif
  171. if(DHT11_Data->check_sum == DHT11_Data->humi_int + DHT11_Data->humi_deci + DHT11_Data->temp_int + DHT11_Data->temp_deci)
  172. return 0;
  173. }else{
  174. return 1;
  175. }
  176. return 1;
  177. }