热敏电阻温度传感器(基于传感器管理组件)

核能气质少年 提交于 2019-11-29 04:10:51

热敏电阻传感器,其阻值随着温度的变化而变化,我们通过ADC采集热敏电阻上的电压值,再通过计算得到当前温度信息。

注意:请按照自己热敏电阻的属性,修改代码内对应内容!!!

temperature,c

  1 /**
  2  * @file temperature.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 "ci110x_adc.h"
 14 #include "ci110x_scu.h"
 15 #include "ci110x_uart.h"
 16 #include "ci110x_adc.h"
 17 #include "ci_misc.h"
 18 #include "ci_sensor.h"
 19 
 20 /*-----------------------------------------------------------------------------
 21                             define
 22 -----------------------------------------------------------------------------*/
 23 #define RESISTOR (10 * 1000)/*!< 分压电阻阻值 */
 24 #define TEMPERATURE_ADC (HAL_ADC_BASE)/*!< ADC控制器 */
 25 #define TEMPERATURE_ADC_CHANNEL (ADC_CHANNEL_2)/*!< ADC通道 */
 26 #define TEMPERATURE_ADC_PAD (AIN2_PAD)/*!< ADC引脚 */
 27 
 28 /*-----------------------------------------------------------------------------
 29                             extern
 30 -----------------------------------------------------------------------------*/
 31 extern void ADC_SetIOReuse(uint8_t channel);
 32 
 33 /*-----------------------------------------------------------------------------
 34                         struct / enum / union
 35 -----------------------------------------------------------------------------*/
 36 typedef struct
 37 {
 38     uint8_t temperature;
 39     float min_r;
 40     float max_r;
 41 }R_SENSOR_RANG;
 42 
 43 /*-----------------------------------------------------------------------------
 44                             global
 45 -----------------------------------------------------------------------------*/
 46 /**
 47  * @brief 电压-温度对应表
 48  *
 49  */
 50 R_SENSOR_RANG r_sensor_list[]=
 51 {
 52     {0,31.602,33.006  },
 53     {1,30.097,31.402  },
 54     {2,28.667,29.881  },
 55     {3,27.310,28.439  },
 56     {4,26.022,27.071  },
 57     {5,24.799,25.774  },
 58     {6,23.638,24.543  },
 59     {7,22.535,23.376  },
 60     {8,21.488,22.268  },
 61     {9,20.494,21.218  },
 62     {10,19.550,20.221 },
 63     {11,18.653,19.275 },
 64     {12,17.801,18.378 },
 65     {13,16.992,17.526 },
 66     {14,16.224,16.717 },
 67     {15,15.493,15.950 },
 68     {16,14.799,15.221 },
 69     {17,14.139,14.529 },
 70     {18,13.512,13.872 },
 71     {19,12.915,13.247 },
 72     {20,12.348,12.654 },
 73     {21,11.809,12.090 },
 74     {22,11.295,11.554 },
 75     {23,10.807,11.045 },
 76     {24,10.342,10.561 },
 77     {25,9.900,10.100  },
 78     {26,9.471,9.670   },
 79     {27,9.062,9.261   },
 80     {28,8.673,8.872   },
 81     {29,8.303,8.501   },
 82     {30,7.951,8.147   },
 83     {31,7.615,7.810   },
 84     {32,7.296,7.489   },
 85     {33,6.992,7.182   },
 86     {34,6.702,6.890   },
 87     {35,6.425,6.611   },
 88     {36,6.162,6.346   },
 89     {37,5.911,6.092   },
 90     {38,5.671,5.850   },
 91     {39,5.442,5.618   },
 92     {40,5.224,5.398   },
 93     {41,5.016,5.187   },
 94     {42,4.818,4.985   },
 95     {43,4.628,4.793   },
 96     {44,4.447,4.609   },
 97     {45,4.273,4.433   },
 98     {46,4.108,4.265   },
 99     {47,3.950,4.104   },
100     {48,3.799,3.950   },
101     {49,3.654,3.802   },
102     {50,3.516,3.661   },
103 };
104 
105 /*-----------------------------------------------------------------------------
106                             declare
107 -----------------------------------------------------------------------------*/
108 
109 /*-----------------------------------------------------------------------------
110                             function
111 -----------------------------------------------------------------------------*/
112 
113 /**
114  * @brief 读取温度值
115  *
116  * @param voltage
117  * @return unsigned char
118  */
119 unsigned char Caculate_Temperature(float voltage)
120 {
121     float Resis = 0;
122     unsigned char Temprature = -1;
123     Resis = (RESISTOR * voltage) / (3.3 - voltage);
124     for(int i = 0;i < 51;i++)
125     {
126         float temp1 = (r_sensor_list[i].min_r)*1000;
127         float temp2 = (r_sensor_list[i].max_r)*1000;
128         if((Resis >= temp1) && (Resis <= temp2))
129         {
130             Temprature = r_sensor_list[i].temperature;
131             break;
132         }
133     }
134     return Temprature;
135 }
136 
137 /**
138  * @brief 初始化温度传感器
139  *
140  * @param irq_callback 中断回调
141  * @retval RETURN_OK
142  * @retval RETURN_ERR
143  */
144 int32_t temperature_open(void)
145 {
146     #if 0
147     Scu_Setdiv_Parameter(TEMPERATURE_ADC,63);
148     Scu_SetDeviceGate(TEMPERATURE_ADC,1);
149     Scu_SetIOReuse(TEMPERATURE_ADC_PAD,FIRST_FUNCTION);
150     ADC_SetIOReuse(TEMPERATURE_ADC_CHANNEL);
151     adc_power_ctrl(ENABLE);
152     adc_continuons_convert(DISABLE);
153     adc_period_enable(ENABLE);
154     adc_period_monitor(TEMPERATURE_ADC_CHANNEL,ENABLE);
155     adc_chax_period(TEMPERATURE_ADC_CHANNEL,62);
156     adc_calibrate(DISABLE);
157     adc_int_sel(ADC_INT_MODE_TRANS_END);
158     adc_mask_int(DISABLE);
159     adc_convert_config(TEMPERATURE_ADC_CHANNEL,ADC_CLKCYCLE_1);
160     #else
161     adc_init_cycle_mode(TEMPERATURE_ADC_CHANNEL,4,NULL);
162     #endif
163     return RETURN_OK;
164 }
165 
166 /**
167  * @brief 读取温度
168  *
169  * @param data 读到的数据
170  * @retval RETURN_OK
171  * @retval RETURN_ERR
172  */
173 int32_t temperature_read(sensor_data_t *data)
174 {
175     float temp,adc_value;
176     int real_temperature;
177     temp = adc_get_result(TEMPERATURE_ADC_CHANNEL);
178     adc_value = ((temp/4096)*3.3);
179     real_temperature = Caculate_Temperature(adc_value);
180     if(0xFF != real_temperature)
181     {
182         sensor_data_inform(SENSOR_TYPE_TEMPERATURE);
183         data->temperature = real_temperature * 10;
184     }
185     return RETURN_OK;
186 }
187 
188 /**
189  * @brief 温度传感器ops
190  *
191  */
192 sensor_ops_t temperature_ops =
193 {
194     temperature_open,
195     temperature_read,
196 };
197 
198 /*-----------------------------------------------------------------------------
199                             end of the file
200 -----------------------------------------------------------------------------*/

temperature,h

 1 /**
 2  * @file temperature.h
 3  * @brief 温度传感器的头文件
 4  * @version 0.1
 5  * @date 2019-07-02
 6  *
 7  * @copyright Copyright (c) 2019  Chipintelli Technology Co., Ltd.
 8  *
 9  */
10 
11 #ifndef __TEMPERATURE_H__
12 #define __TEMPERATURE_H__
13 
14 /**
15  * @ingroup third_device_driver
16  * @defgroup 热敏电阻
17  * @brief 热敏电阻传感器驱动
18  * @{
19  */
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /*-----------------------------------------------------------------------------
26                             include
27 -----------------------------------------------------------------------------*/
28 #include "ci_sensor.h"
29 
30 /*-----------------------------------------------------------------------------
31                             define
32 -----------------------------------------------------------------------------*/
33 
34 /*-----------------------------------------------------------------------------
35                             extern
36 -----------------------------------------------------------------------------*/
37 extern sensor_ops_t temperature_ops;
38 
39 /*-----------------------------------------------------------------------------
40                         struct / enum / union
41 -----------------------------------------------------------------------------*/
42 
43 /*-----------------------------------------------------------------------------
44                             global
45 -----------------------------------------------------------------------------*/
46 
47 /*-----------------------------------------------------------------------------
48                         function declare
49 -----------------------------------------------------------------------------*/
50 
51 #ifdef __cplusplus
52 }
53 #endif
54 
55 /**
56  * @}
57  */
58 
59 #endif
60 
61 /*-----------------------------------------------------------------------------
62                             end of the file
63 -----------------------------------------------------------------------------*/
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!