在使用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”,这显然是不正确的。
我的代码是:
#define TS30 ((uint16_t*)((uint32_t)0x1FFF75A8))
#define TS110 ((uint16_t*)((uint32_t)0x1FFF75CA))
uint32_t ADC_Value;
double temperature;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
ADC_Value = HAL_ADC_GetValue(&hadc1);
temperature = (double)ADC_Value - (uint32_t)*TS30;
temperature *= (double)((uint32_t)110 - (uint32_t)30);
temperature /= (double)(int32_t)((uint32_t)*TS110 - (uint32_t)*TS30);
temperature += 30;
}
int main(){ /**Main Loop**/
MX_ADC1_Init();
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
HAL_ADC_Start_IT(&hadc1);
while(1) {
HAL_Delay(400);
HAL_ADC_Start_IT(&hadc1);
}
}
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.NbrOfDiscConversion = 1;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK){
_Error_Handler(__FILE__, __LINE__);
}
/**Configure Regular Channel**/
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){
_Error_Handler(__FILE__, __LINE__);
}
}
复制代码 |