DHT11数字温度湿度传感器驱动(基于传感器管理组件)

传感器简介

DHT11数字温度湿度传感器是一款有已校准数字信号输出的温湿度复合传感器,其测量精度:湿度+-5%RH、温度:+-2℃;测量范围:湿度20-90%RH、温度0-50℃。

DHT11.c

  1 /**
  2  * @file DHT11.c
  3  * @brief
  4  * @version 0.1
  5  * @date 2019-06-28
  6  *
  7  * @copyright Copyright (c) 2019  Chipintelli Technology Co., Ltd.
  8  *
  9  */
 10 /*-----------------------------------------------------------------------------
 11                             include
 12 -----------------------------------------------------------------------------*/
 13 #include "ci_sensor.h"
 14 #include "ci110x_scu.h"
 15 #include "ci110x_timer.h"
 16 #include "ci110x_gpio.h"
 17 #include "platform_config.h"
 18 
 19 /*-----------------------------------------------------------------------------
 20                             define
 21 -----------------------------------------------------------------------------*/
 22 #define DHT11_GPIO_GROUP    (GPIO0)/*!< GPIO组 */
 23 #define DHT11_GPIO_PIN      (gpio_pin_2)/*!< GPIO pin脚 */
 24 #define DHT11_GPIO_PAD      (UART1_TX_PAD)/*!< GPIO对应PAD */
 25 #define DHT11_GPIO_IRQ      (GPIO0_IRQn)/*!< GPIO中断 */
 26 
 27 #define DHT11_TIMER         (TIMER0)/*!< TIMER控制器 */
 28 #define DHT11_TIMER_IRQ     (TIMER0_IRQn)/*!< TIMER中断 */
 29 #define DHT11_TIMER_ONEUS_COUNT (get_apb_clk())
 30 
 31 /*-----------------------------------------------------------------------------
 32                             extern
 33 -----------------------------------------------------------------------------*/
 34 
 35 /*-----------------------------------------------------------------------------
 36                         struct / enum / union
 37 -----------------------------------------------------------------------------*/
 38 
 39 /*-----------------------------------------------------------------------------
 40                             global
 41 -----------------------------------------------------------------------------*/
 42 uint8_t dht11_flag = 0xFF;/*!< 中断标志 */
 43 uint8_t dht11_data[5] = {0};/*!< 二进制数据接收数组 */
 44 uint8_t dht11_count = 0;/*!< GPIO中断计数控制 */
 45 
 46 /*-----------------------------------------------------------------------------
 47                             declare
 48 -----------------------------------------------------------------------------*/
 49 
 50 /*-----------------------------------------------------------------------------
 51                             function
 52 -----------------------------------------------------------------------------*/
 53 
 54 /**
 55  * @brief TIMER配置
 56  */
 57 void Dht11_Timer_Init(void)
 58 {
 59     timer_init_t init;
 60     init.mode = timer_count_mode_single;
 61     init.div = timer_clk_div_0;
 62     init.width = timer_iqr_width_2;
 63     init.count = 0xFFFFFFFF;
 64     timer_init(DHT11_TIMER,init);
 65     timer_start(DHT11_TIMER);
 66 }
 67 
 68 /**
 69  * @brief TIMER配置毫秒级延时
 70  *
 71  * @param ms 毫秒
 72  */
 73 void Dht11_Timer_Ms(int ms)
 74 {
 75     timer_stop(DHT11_TIMER);
 76     timer_set_count(DHT11_TIMER,ms * DHT11_TIMER_ONEUS_COUNT / 1000);
 77     timer_start(DHT11_TIMER);
 78 }
 79 
 80 /**
 81  * @brief TIMER配置微秒级延时
 82  *
 83  * @param us 微秒
 84  */
 85 void Dht11_Timer_Us(int us)
 86 {
 87     timer_stop(DHT11_TIMER);
 88     timer_set_count(DHT11_TIMER,us * DHT11_TIMER_ONEUS_COUNT / 1000000);
 89     timer_start(DHT11_TIMER);
 90 }
 91 
 92 /**
 93  * @brief DHT11模块初始化
 94  *
 95  */
 96 void Dht11_Init()
 97 {
 98     Scu_SetDeviceGate((unsigned int)DHT11_TIMER,ENABLE);
 99     Scu_Setdevice_Reset((unsigned int)DHT11_TIMER);
100     Scu_Setdevice_ResetRelease((unsigned int)DHT11_TIMER);
101     NVIC_EnableIRQ(DHT11_TIMER_IRQ);
102 
103     Scu_SetDeviceGate((unsigned int)DHT11_GPIO_GROUP,ENABLE);
104     Scu_Setdevice_Reset((unsigned int)DHT11_GPIO_GROUP);
105     Scu_Setdevice_ResetRelease((unsigned int)DHT11_GPIO_GROUP);
106     NVIC_EnableIRQ(DHT11_GPIO_IRQ);
107 
108     Scu_SetIOReuse(DHT11_GPIO_PAD,FIRST_FUNCTION);
109     gpio_set_output_mode(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
110     gpio_set_output_high_level(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
111 
112     Dht11_Timer_Init();
113 }
114 
115 /**
116  * @brief DHT11模块TIMER中断程序
117  *
118  */
119 void dht11_timer_handler(void)
120 {
121     dht11_flag = 0;
122 }
123 
124 /**
125  * @brief DHT11模块GPIO中断程序
126  *
127  */
128 void dht11_gpio_handler(void)
129 {
130     if(dht11_count != 0)
131     {
132         Dht11_Timer_Us(50);
133     }
134     dht11_count++;
135 }
136 
137 void dht11_wait_timer(void)
138 {
139     uint32_t timeout = 0x5FFFF;
140     while(dht11_flag && timeout--);
141     dht11_flag = 0xFF;
142 }
143 
144 /**
145  * @brief 初始化DHT11模块
146  *
147  * @param irq_callback 中断回调
148  * @retval RETURN_OK
149  * @retval RETURN_ERR
150  */
151 int32_t dht11_open(void)
152 {
153     Dht11_Init();
154     return RETURN_OK;
155 }
156 
157 /**
158  * @brief DHT11传感器读数据
159  *
160  * @param data 读到的数据
161  * @retval RETURN_OK
162  * @retval RETURN_ERR
163  */
164 int32_t dht11_read(sensor_data_t *data)
165 {
166     uint8_t temp = 0;
167     uint8_t dht11_buf[40] = {0};
168 START:
169     gpio_set_output_mode(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
170     gpio_set_output_low_level(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
171     Dht11_Timer_Ms(20);
172     dht11_wait_timer();
173     gpio_set_output_mode(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
174     gpio_set_output_high_level(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
175     Dht11_Timer_Us(20);
176     dht11_wait_timer();
177     gpio_set_input_mode(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
178     gpio_irq_trigger_config(DHT11_GPIO_GROUP,DHT11_GPIO_PIN,up_edges_trigger);
179     for(int i = 0;i < 40;i++)
180     {
181         dht11_wait_timer();
182         dht11_buf[i] = gpio_get_input_level(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
183     }
184     gpio_set_output_mode(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
185     gpio_set_output_high_level(DHT11_GPIO_GROUP,DHT11_GPIO_PIN);
186     timer_stop(DHT11_TIMER);
187     dht11_count = 0;
188     for(int i = 0;i < 5;i++)
189     {
190         temp = 0;
191         for(int j = 0;j < 8;j++)
192         {
193             temp |= dht11_buf[j + i * 8] << (7 - j);
194         }
195         dht11_data[i] = temp;
196     }
197     temp = dht11_data[0] + dht11_data[1] + dht11_data[2] + dht11_data[3];
198     if(dht11_data[4] != temp)
199     {
200         Dht11_Timer_Us(50);
201         dht11_wait_timer();
202         goto START;
203     }
204     else
205     {
206         data->humidity = dht11_data[0] * 10;
207     }
208     return RETURN_OK;
209 }
210 
211 /**
212  * @brief DHT11传感器ops
213  *
214  */
215 sensor_ops_t dht11_ops =
216 {
217     dht11_open,
218     dht11_read,
219 };
220 
221 /*-----------------------------------------------------------------------------
222                             end of the file
223 -----------------------------------------------------------------------------*/

DHT11.h

 1 /**
 2  * @file DHT11.h
 3  * @brief DHT11传感器的头文件
 4  * @version 0.1
 5  * @date 2019-07-02
 6  *
 7  * @copyright Copyright (c) 2019  Chipintelli Technology Co., Ltd.
 8  *
 9  */
10 
11 #ifndef __DHT11_H__
12 #define __DHT11_H__
13 
14 /**
15  * @ingroup third_device_driver
16  * @defgroup DHT11
17  * @brief DHT11传感器驱动
18  * @{
19  */
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /*-----------------------------------------------------------------------------
26                             include
27 -----------------------------------------------------------------------------*/
28 
29 /*-----------------------------------------------------------------------------
30                             define
31 -----------------------------------------------------------------------------*/
32 
33 /*-----------------------------------------------------------------------------
34                             extern
35 -----------------------------------------------------------------------------*/
36 extern sensor_ops_t dht11_ops;
37 
38 /*-----------------------------------------------------------------------------
39                         struct / enum / union
40 -----------------------------------------------------------------------------*/
41 
42 /*-----------------------------------------------------------------------------
43                             global
44 -----------------------------------------------------------------------------*/
45 
46 /*-----------------------------------------------------------------------------
47                         function declare
48 -----------------------------------------------------------------------------*/
49 
50 #ifdef __cplusplus
51 }
52 #endif
53 
54 /**
55  * @}
56  */
57 
58 #endif
59 
60 /*-----------------------------------------------------------------------------
61                             end of the file
62 -----------------------------------------------------------------------------*/

 

你可能感兴趣的:(DHT11数字温度湿度传感器驱动(基于传感器管理组件))