/*------------------------------------------------------ TEMPERATURE VS RESISTANCE TABLE Resistance 100k Ohms at 25deg. C Resistance Tolerance +/- 1% B Value 3950K at 25/50deg. C B Value Tolerance +/- 1% ------------------------------------------------------*/ #include "bsp_ntc.h" #include "sysparam.h" #define N 191 //NTC温度对应阻值个数,即ntc[]数组大小 const static float ntc[N]= { //-20℃ //-19 //-18 //-17 //-16 //-15 //-14 //-13 //-12 //-11 975.8038,920.5962,868.8615,820.3603,774.8710,732.1889,692.1238,654.4999,619.1540,585.9346, //-10℃ //-9 //-8 //-7 //-6 //-5 //-4 //-3 //-2 //-1 554.7016,525.3245,497.6821,471.6621,447.1599,424.0781,402.3264,381.8204,362.4818,344.2375, //0℃ //1 //2 //3 //4 //5 //6 //7 //8 //9 327.0195,310.7640,295.4121,280.9084,267.2014,254.2428,241.9877,230.3940,219.4224,209.0361, //10℃ //11 //12 //13 //14 //15 //16 //17 //18 //19 199.2007,189.8841,181.0559,172.6881,164.7540,157.2290,150.0898,143.3144,136.8825,130.7749, //20℃ //21 //22 //23 //24 //25 //26 //27 //28 //29 124.9734,119.4612,114.2223,109.2417,104.5053,100.0000,95.7132, 91.6333, 87.7492, 84.0505, //30℃ //31 //32 //33 //34 //35 //36 //37 //38 //39 80.5274, 77.1707, 73.9717, 70.9222, 68.0144, 65.2411, 62.5954, 60.0707, 57.6610, 55.3604, //40℃ //41 //42 //43 //44 //45 //46 //47 //48 //49 53.1635, 51.0651, 49.0602, 47.1443, 45.3130, 43.5621, 41.8878, 40.2862, 38.7539, 37.2876, //50℃ //51 //52 //53 //54 //55 //56 //57 //58 //59 35.8842, 34.5405, 33.2538, 32.0214, 30.8408, 29.7096, 28.6253, 27.5860, 26.5895, 25.6338, //60℃ //61 //62 //63 //64 //65 //66 //67 //68 //69 24.7171, 23.8376, 22.9937, 22.1836, 21.4061, 20.6594, 19.9424, 19.2537, 18.5920, 17.9562, //70℃ //71 //72 //73 //74 //75 //76 //77 //78 //79 17.3452, 16.7578, 16.1930, 15.6499, 15.1276, 14.6251, 14.1417, 13.6764,13.2286, 12.7976, //80℃ //81 //82 //83 //84 //85 //86 //87 //88 //89 12.3825, 11.9828, 11.5978, 11.2270, 10.8697, 10.5254, 10.1935, 9.8736, 9.5652, 9.2678, //90℃ //91 //92 //93 //94 //95 //96 //97 //98 //99 8.9809, 8.7042, 8.4373, 8.1797, 7.9312, 7.6912, 7.4596, 7.2360, 7.0201, 6.8115, //100℃ //101 //102 //103 //104 //105 //106 //107 //108 //109 6.6101, 6.4155, 6.2274, 6.0457, 5.8701, 5.7003, 5.5362, 5.3775, 5.2240, 5.0755, //110℃ //111 //112 //113 //114 //115 //116 //117 //118 //119 4.9319, 4.7930, 4.6586, 4.5285, 4.4026, 4.2807, 4.1627, 4.0484, 3.9378, 3.8306, //120℃ //121 //122 //123 //124 //125 //126 //127 //128 //129 3.7268, 3.6263, 3.5289, 3.4345, 3.3430, 3.2543, 3.1683, 3.0850, 3.0042, 2.9258, //130℃ //131 //132 //133 //134 //135 //136 //137 //138 //139 2.8498, 2.7761, 2.7045, 2.6352, 2.5678, 2.5025, 2.4391, 2.3775, 2.3178, 2.2598, //140℃ //141 //142 //143 //144 //145 //146 //147 //148 //149 2.2034, 2.1487, 2.0956, 2.0440, 1.9939, 1.9452, 1.8978, 1.8518, 1.8071, 1.7637, //150℃ //151 //152 //153 //154 //155 //156 //157 //158 //159 1.7215, 1.6804, 1.6405, 1.6017, 1.5640, 1.5273, 1.4915, 1.4568, 1.4230, 1.3901, //160℃ //161 //162 //163 //164 //165 //166 //167 //168 //169 1.3582, 1.3270, 1.2967, 1.2672, 1.2385, 1.2106, 1.1833, 1.1568, 1.1310, 1.1059, //170℃ 1.0814 }; State_Enum_TypeDef Samp_Temp_Volt_State = FLG_SAMP_START; //开机上电后即执行温度电压采样 /************************************************************* 函数: void handle_temperature_volt(vu16 *ADCConvertedValue,variables *outpc) 功能: 配合中断,采集气温缸温电池电压 参数: ADCConvertedValue: ADC数组 outpc : 串口发送数据 返回: none 描述: **************************************************************/ void handle_temperature_volt(vu16 *ADCConvertedValue,variables *topc) { //采集气温,缸温,电池电压,分别存入 switch(Samp_Temp_Volt_State) { case FLG_SAMP_START: ADC_SoftwareStartConvCmd(ADC1,ENABLE); //开启ADC转换,气温,缸温,电池电压 Samp_Temp_Volt_State = FLG_SAMP_WAIT; break; case FLG_SAMP_END: //等待ADC转换完成,则处理ADC数据,得到气温,缸温,电池电压 get_temperature_volt(ADCConvertedValue,topc); Samp_Temp_Volt_State = FLG_SAMP_OVER; //气温,缸温,电池电压采样完成 break; default: break; } } /************************************************************* 函数: void get_temperature_volt(vu16 *ADCConvertedValue,variables *outpc) 功能: 得到气温、缸温、电池电压,深度为3的滑动平均算法平滑数据 参数: ADCConvertedValue: ADC数组 outpc : 串口发送数据 返回: none 描述: Third-order moving average algorithm,get ntc temperature and battery voltage **************************************************************/ static void get_temperature_volt(vu16 *ADCConvertedValue,variables *topc) { uint32_t temp = 0; static uint16_t ad_air_temp[3]; //气温,滑动平均深度为3 static uint16_t ad_cyl_temp[3]; //缸温,滑动平均深度为3 static uint16_t ad_vol_temp[3]; //电池电压,滑动平均深度为3 temp = (ADCConvertedValue[0]+ADCConvertedValue[3]+ADCConvertedValue[6])/3; //气温的ADC平均值 ad_air_temp[0] = ad_air_temp[1]; ad_air_temp[1] = ad_air_temp[2]; ad_air_temp[2] = temp; temp = (ad_air_temp[0] + ad_air_temp[1] + ad_air_temp[2])/3; topc->mat = get_ntc_temperature((u16)temp,inpram.Temp_Units); //得到气温X10 temp = (ADCConvertedValue[1]+ADCConvertedValue[4]+ADCConvertedValue[7])/3; //缸温的ADC平均值 ad_cyl_temp[0] = ad_cyl_temp[1]; ad_cyl_temp[1] = ad_cyl_temp[2]; ad_cyl_temp[2] = temp; temp = (ad_cyl_temp[0] + ad_cyl_temp[1] + ad_cyl_temp[2])/3; topc->clt = get_ntc_temperature((u16)temp,inpram.Temp_Units); //得到缸温X10 temp = (ADCConvertedValue[2]+ADCConvertedValue[5]+ADCConvertedValue[8])/3; //电池电压的ADC平均值 ad_vol_temp[0] = ad_vol_temp[1]; ad_vol_temp[1] = ad_vol_temp[2]; ad_vol_temp[2] = temp; temp = (ad_vol_temp[0] + ad_vol_temp[1] + ad_vol_temp[2])/3; topc->batt = temp*132/4095; //电池电压X10 } /************************************************************* 函数: static int16_t get_ntc_temperature(uint16_t ad_data) 功能: 参数: ad_data: STM32的ADC采集到的NTC电路分压AD值 返回: 根据单位换算成摄氏度/华氏度,返回温度值*10 描述: 10K与NTC串联,接到3V3电源上 **************************************************************/ static int16_t get_ntc_temperature(uint16_t ad_data,uint8_t Temp_Units) { float Rntc,t; uint8_t index; Rntc = 10.0*ad_data/(4095 - ad_data);//求出Rntc阻值,单位为Komh for(index = 0;index < (N-1);index++) { if(Rntc > ntc[index]) { break; } } if(index <= 0) //temp lower than -20℃ { t = -20.0; //rail at -20℃ } else if(index >= (N-1)) //temp higher than 170℃ { t = 170.0; //rail at 170℃ } else //temp between -20℃ and 170℃ { t = (-20 + index) - (Rntc-ntc[index])/(ntc[index]-ntc[index-1]);
} if(Temp_Units) // 摄氏度 ℃ { t = t*10; // 温度扩大10倍 } else // 华氏度 F { t = (t*1.8 + 32)*10; // 温度扩大10倍 } return (int16_t)t; // 类型转换为int16_t }