STM32L4的温度计算公式显示不正确的结果

2019-07-14 16:27发布

在使用STM32L4的ADC测量其内部温度传感器数据时,我遇到了一个问题。STM32L4参考手册中提供的等式似乎显示不正确的结果。等式是:*温度=((110-30)*(TS_DATA - TS_CAL_1)/(TS_CAL_2 - TS_CAL_1))+ 30 *其中,TS_DATA = 945(温度的原始ADC数据),TS_CAL_1 = 1035(从预定义的存储器地址读取校准点),TS_CAL_2 = 1373(从预定义的存储器地址读取校准点)这导致室温(约26摄氏度)的“温度= 8.69”,这显然是不正确的。我的代码是:
  1. #define TS30    ((uint16_t*)((uint32_t)0x1FFF75A8))
  2. #define TS110   ((uint16_t*)((uint32_t)0x1FFF75CA))
  3. uint32_t ADC_Value;
  4. double temperature;
  5. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
  6.    ADC_Value = HAL_ADC_GetValue(&hadc1);
  7.    temperature = (double)ADC_Value - (uint32_t)*TS30;
  8.    temperature *= (double)((uint32_t)110 - (uint32_t)30);
  9.    temperature /= (double)(int32_t)((uint32_t)*TS110 - (uint32_t)*TS30);
  10.    temperature += 30;
  11. }
  12. int main(){           /**Main Loop**/
  13. MX_ADC1_Init();
  14.    HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
  15.    HAL_ADC_Start_IT(&hadc1);

  16.    while(1) {
  17.      HAL_Delay(400);
  18.      HAL_ADC_Start_IT(&hadc1);
  19.    }
  20. }
  21. static void MX_ADC1_Init(void)
  22. {
  23.   ADC_ChannelConfTypeDef sConfig;
  24.   hadc1.Instance = ADC1;
  25.   hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  26.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  27.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  28.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  29.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  30.   hadc1.Init.LowPowerAutoWait = DISABLE;
  31.   hadc1.Init.ContinuousConvMode = DISABLE;
  32.   hadc1.Init.NbrOfConversion = 1;
  33.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  34.   hadc1.Init.NbrOfDiscConversion = 1;
  35.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  36.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  37.   hadc1.Init.DMAContinuousRequests = DISABLE;
  38.   hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  39.   hadc1.Init.OversamplingMode = DISABLE;
  40.   if (HAL_ADC_Init(&hadc1) != HAL_OK){
  41.     _Error_Handler(__FILE__, __LINE__);
  42.   }

  43.   /**Configure Regular Channel**/
  44.   sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  45.   sConfig.Rank = ADC_REGULAR_RANK_1;
  46.   sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
  47.   sConfig.SingleDiff = ADC_SINGLE_ENDED;
  48.   sConfig.OffsetNumber = ADC_OFFSET_NONE;
  49.   sConfig.Offset = 0;
  50.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){
  51.    _Error_Handler(__FILE__, __LINE__);
  52.   }
  53. }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。