本帖最后由 快乐的小娃 于 2014-11-24 17:49 编辑
好久没有冒泡了,冒下泡表示我还活着。
。给大家分享个自己原创的NTC的例子,希望对大家有那么一点点帮助,例子很简单就是使用查表的方式在读取过来的电阻值进行比较找出当前温度,如果没有接热敏电阻会返回错误码;里面初始化了两个ADC使用来演示双通道同时采集的,但是只用了一个ADC用来举例。
- #include "Adc_device.h"
- #include "stm32f10x.h"
- #include "ntc_r2c.h"
- Adc_fun_Struct_date Adc_fun_date;//ADC保存数据
- void ADC_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
-
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
- /* Enable peripheral clocks ------------------------------------------------*/
- /* Enable DMA1 clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- /*------------------使能时钟---------------*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB |
- RCC_APB2Periph_AFIO | RCC_APB2Periph_ADC1,ENABLE);
- /*----------端口配置 模拟输入模式---------*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /*----------端口DMA1_Channel1---------*/
-
- /* DMA1 channel1 configuration ----------------------------------------------*/
- //使用DMA的原因是比如类似温度信息这类似的辅助处理就不用麻烦CPU费心了 ,要做到分工明确,灵活应用上面的资源,不能浪费不是;俺是农民出身;
- DMA_DeInit(DMA1_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Adc_fun_date.DMA_ADC_PeripheralBaseAddr;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = 200;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
- /* Enable DMA1 channel1 */
- DMA_Cmd(DMA1_Channel1, ENABLE);
-
- /* ADC1 configuration ------------------------------------------------------*/
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 2;
- ADC_Init(ADC1, &ADC_InitStructure);
- /* ADC1 regular channel14 configuration */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_239Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_239Cycles5);
- /* Enable ADC1 DMA */
- ADC_DMACmd(ADC1, ENABLE);
-
- /* Enable ADC1 */
- ADC_Cmd(ADC1, ENABLE);
- /* Enable ADC1 reset calibration register */
- ADC_ResetCalibration(ADC1);
- /* Check the end of ADC1 reset calibration register */
- while(ADC_GetResetCalibrationStatus(ADC1));
- /* Start ADC1 calibration */
- ADC_StartCalibration(ADC1);
- /* Check the end of ADC1 calibration */
- while(ADC_GetCalibrationStatus(ADC1));
-
- /* Start ADC1 Software Conversion */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
-
- }
- /*************************************
- **NTC温度转换
- **电压范围与电阻关系
- **100K 3.1518V
- **10K 2.2448V
- **1K 0.578947V
- **100欧姆 0.06875V
- ***************************************/
- short NTC_V2R(){
- uint32_t Temp_Value;
- unsigned tmp_i;
- float ADC_Value=0.00;
- float R_num=0.00;
- short cc_num;
- //算出平均值电压
- for(tmp_i = 0,Temp_Value = 0;tmp_i < 100;tmp_i++){
- Temp_Value += Adc_fun_date.DMA_ADC_PeripheralBaseAddr[tmp_i][1];
- }
- Temp_Value /= 100;
- ADC_Value = (3.3/4096)*Temp_Value - (float)Calibration_TypeDef.SOC_JZ;
- Adc_fun_date.ntc_adc_date = ADC_Value;
- //算出取样点电压并推算出阻值
- R_num = ADC_Value / ((3.3 - ADC_Value) / 4.7);
- if(R_num > 34){
- for(tmp_i = 39;tmp_i > 0;tmp_i--){
- if(((R_num >= NTC_R_num_down[tmp_i])&&
- (R_num <= NTC_R_num_down[tmp_i-1])&&(tmp_i != 0))){
- if((R_num - NTC_R_num_down[tmp_i]) > (NTC_R_num_down[tmp_i-1] - R_num)){
- cc_num = -(40 - (tmp_i-1));
- return cc_num;
- }else{
- cc_num = -(40 - tmp_i);
- return cc_num;
- }
- }else if(tmp_i == 0){
- cc_num = -50;
- return cc_num;
- }
- }
- }else if(R_num < 34){
- for(tmp_i = 1;tmp_i < 151;tmp_i++){
- if(((R_num >= NTC_R_num_top[tmp_i])&&(R_num <= NTC_R_num_top[tmp_i-1]))){
-
- if((R_num - NTC_R_num_top[tmp_i]) > (NTC_R_num_top[tmp_i-1] - R_num)){
- cc_num = tmp_i-1;
- return cc_num;
- }else{
- cc_num = tmp_i;
- return cc_num;
- }
- }else if(tmp_i == 151){
- cc_num = 160;
- return cc_num;
- }
- }
- }
- //如果到了这一步都没有出去那就说明采样失败了返回错误码203
- cc_num = 203;
- return cc_num;
- }
复制代码
下面是头文件
#ifndef __ADC_DEVICE_H
#define __ADC_DEVICE_H
#include "stm32f10x.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
void ADC_Configuration(void);
short NTC_V2R(void);
float CVT_adc(void);
#pragma pack (1) /*指定按1字节对齐*/
typedef struct{
uint16_t DMA_ADC_PeripheralBaseAddr[100][2];
float ntc_adc_date;
float CVT_adc_date;
}Adc_fun_Struct_date;
#pragma pack() /*取消指定对齐,恢复缺省对齐*/
#endif
另一个头文件
#ifndef __NTC_R2C_H
#define __NTC_R2C_H
const float NTC_R_num_down[40] = {
352.600,// -40 3.526E+05
329.100,// -39 3.291E+05
307.300,// -38 3.073E+05
287.100,// -37 2.871E+05
268.400,// -36 2.684E+05
251.100,// -35 2.511E+05
235.000,// -34 2.350E+05
220.000,// -33 2.200E+05
206.100,// -32 2.061E+05
193.200,// -31 1.932E+05
181.100,// -30 1.811E+05
169.900,// -29 1.699E+05
159.500,// -28 1.595E+05
149.700,// -27 1.497E+05
140.700,// -26 1.407E+05
132.200,// -25 1.322E+05
124.400,// -24 1.244E+05
117.000,// -23 1.170E+05
110.100,// -22 1.101E+05
103.700,// -21 1.037E+05
97.700,// -20 9.770E+04
92.080,// -19 9.208E+04
86.830,// -18 8.683E+04
81.910,// -17 8.191E+04
77.310,// -16 7.731E+04
72.990,// -15 7.299E+04
68.950,// -14 6.895E+04
65.160,// -13 6.516E+04
61.610,// -12 6.161E+04
58.270,// -11 5.827E+04
55.140,// -10 5.514E+04
52.200,// -9 5.220E+04
49.430,// -8 4.943E+04
46.830,// -7 4.683E+04
44.390,// -6 4.439E+04
42.090,// -5 4.209E+04
39.920,// -4 3.992E+04
37.880,// -3 3.788E+04
35.950,// -2 3.595E+04
34.140,// -1 3.414E+04
};
const float NTC_R_num_top[151] = {
32.420,// 0 3.242E+04
30.810,// 1 3.081E+04
29.290,// 2 2.929E+04
27.850,// 3 2.785E+04
26.490,// 4 2.649E+04
25.210,// 5 2.521E+04
23.990,// 6 2.399E+04
22.850,// 7 2.285E+04
21.760,// 8 2.176E+04
20.730,// 9 2.073E+04
19.760,// 10 1.976E+04
18.840,// 11 1.884E+04
17.970,// 12 1.797E+04
17.150,// 13 1.715E+04
16.370,// 14 1.637E+04
15.620,// 15 1.562E+04
14.920,// 16 1.492E+04
14.250,// 17 1.425E+04
13.620,// 18 1.362E+04
13.020,// 19 1.302E+04
12.450,// 20 1.245E+04
11.910,// 21 1.191E+04
11.400,// 22 1.140E+04
10.910,// 23 1.091E+04
10.440,// 24 1.044E+04
10.000,// 25 1.000E+04
9.579,// 26 9.579E+03
9.179,// 27 9.179E+03
8.798,// 28 8.798E+03
8.436,// 29 8.436E+03
8.090,// 30 8.090E+03
7.761,// 31 7.761E+03
7.447,// 32 7.447E+03
7.148,// 33 7.148E+03
6.863,// 34 6.863E+03
6.591,// 35 6.591E+03
6.331,// 36 6.331E+03
6.084,// 37 6.084E+03
5.848,// 38 5.848E+03
5.622,// 39 5.622E+03
5.406,// 40 5.406E+03
5.201,// 41 5.201E+03
5.004,// 42 5.004E+03
4.816,// 43 4.816E+03
4.637,// 44 4.637E+03
4.465,// 45 4.465E+03
4.301,// 46 4.301E+03
4.144,// 47 4.144E+03
3.993,// 48 3.993E+03
3.849,// 49 3.849E+03
3.711,// 50 3.711E+03
3.579,// 51 3.579E+03
3.453,// 52 3.453E+03
3.332,// 53 3.332E+03
3.216,// 54 3.216E+03
3.105,// 55 3.105E+03
2.998,// 56 2.998E+03
2.895,// 57 2.895E+03
2.797,// 58 2.797E+03
2.703,// 59 2.703E+03
2.612,// 60 2.612E+03
2.525,// 61 2.525E+03
2.441,// 62 2.441E+03
2.360,// 63 2.360E+03
2.283,// 64 2.283E+03
2.209,// 65 2.209E+03
2.137,// 66 2.137E+03
2.068,// 67 2.068E+03
2.002,// 68 2.002E+03
1.939,// 69 1.939E+03
1.877,// 70 1.877E+03
1.818,// 71 1.818E+03
1.762,// 72 1.762E+03
1.707,// 73 1.707E+03
1.654,// 74 1.654E+03
1.604,// 75 1.604E+03
1.555,// 76 1.555E+03
1.508,// 77 1.508E+03
1.463,// 78 1.463E+03
1.419,// 79 1.419E+03
1.377,// 80 1.377E+03
1.336,// 81 1.336E+03
1.297,// 82 1.297E+03
1.259,// 83 1.259E+03
1.223,// 84 1.223E+03
1.188,// 85 1.188E+03
1.154,// 86 1.154E+03
1.121,// 87 1.121E+03
1.089,// 88 1.089E+03
1.059,// 89 1.059E+03
1.029,// 90 1.029E+03
1.000,// 91 1.000E+03
0.9724,// 92 9.724E+02
0.9455,// 93 9.455E+02
0.9196,// 94 9.196E+02
0.8945,// 95 8.945E+02
0.8703,// 96 8.703E+02
0.8469,// 97 8.469E+02
0.8242,// 98 8.242E+02
0.8023,// 99 8.023E+02
0.7812,// 100 7.812E+02
0.7608,// 101 7.608E+02
0.7410,// 102 7.410E+02
0.7219,// 103 7.219E+02
0.7033,// 104 7.033E+02
0.6854,// 105 6.854E+02
0.6680,// 106 6.680E+02
0.6511,// 107 6.511E+02
0.6348,// 108 6.348E+02
0.6190,// 109 6.190E+02
0.6036,// 110 6.036E+02
0.5888,// 111 5.888E+02
0.5744,// 112 5.744E+02
0.5604,// 113 5.604E+02
0.5468,// 114 5.468E+02
0.5337,// 115 5.337E+02
0.5209,// 116 5.209E+02
0.5085,// 117 5.085E+02
0.4964,// 118 4.964E+02
0.4847,// 119 4.847E+02
0.4733,// 120 4.733E+02
0.4622,// 121 4.622E+02
0.4515,// 122 4.515E+02
0.4410,// 123 4.410E+02
0.4309,// 124 4.309E+02
0.4210,// 125 4.210E+02
0.4114,// 126 4.114E+02
0.4020,// 127 4.020E+02
0.3930,// 128 3.930E+02
0.3841,// 129 3.841E+02
0.3756,// 130 3.756E+02
0.3672,// 131 3.672E+02
0.3591,// 132 3.591E+02
0.3512,// 133 3.512E+02
0.3435,// 134 3.435E+02
0.3360,// 135 3.360E+02
0.3287,// 136 3.287E+02
0.3216,// 137 3.216E+02
0.3147,// 138 3.147E+02
0.3080,// 139 3.080E+02
0.3014,// 140 3.014E+02
0.2950,// 141 2.950E+02
0.2888,// 142 2.888E+02
0.2827,// 143 2.827E+02
0.2768,// 144 2.768E+02
0.2711,// 145 2.711E+02
0.2655,// 146 2.655E+02
0.2600,// 147 2.600E+02
0.2547,// 148 2.547E+02
0.2495,// 149 2.495E+02
0.2444// 150 2.444E+02
};
#endif
完毕
能办到一些小忙也好,哈哈
一周热门 更多>