我现在使用的是STM32H743芯片,使用LL库进行读取内部温度,ADC时钟为40M,我在while循环中进行读取,但是我读取的数据一直不太正确(温度为负数),下边是配置代码以及输出结果
void MX_ADC3_Init(void)
{
/* USER CODE BEGIN ADC3_Init 0 */
/* USER CODE END ADC3_Init 0 */
LL_ADC_InitTypeDef ADC_InitStruct = {0};
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSOURCE_PLL2P);
/* Peripheral clock enable */
LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_ADC3);
/* USER CODE BEGIN ADC3_Init 1 */
/* USER CODE END ADC3_Init 1 */
/** Common config
*/
ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_16B;
ADC_InitStruct.LowPowerMode = LL_ADC_LP_AUTOWAIT;
LL_ADC_Init(ADC3, &ADC_InitStruct);
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_1RANK;
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
LL_ADC_REG_Init(ADC3, &ADC_REG_InitStruct);
LL_ADC_SetOverSamplingScope(ADC3, LL_ADC_OVS_DISABLE);
ADC_CommonInitStruct.CommonClock = LL_ADC_CLOCK_ASYNC_DIV2;
ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC3), &ADC_CommonInitStruct);
/* Disable ADC deep power down (enabled by default after reset state) */
LL_ADC_DisableDeepPowerDown(ADC3);
/* Enable ADC internal voltage regulator */
LL_ADC_EnableInternalRegulator(ADC3);
/* Delay for ADC internal voltage regulator stabilization. */
/* Compute number of CPU cycles to wait for, from delay in us. */
/* Note: Variable divided by 2 to compensate partially */
/* CPU processing cycles (depends on compilation optimization). */
/* Note: If system core clock frequency is below 200kHz, wait time */
/* is only a few CPU processing cycles. */
uint32_t wait_loop_index;
wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
while(wait_loop_index != 0)
{
wait_loop_index--;
}
/** Configure Regular Channel
*/
LL_ADC_REG_SetSequencerRanks(ADC3, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_TEMPSENSOR);
LL_ADC_SetChannelSamplingTime(ADC3, LL_ADC_CHANNEL_TEMPSENSOR, LL_ADC_SAMPLINGTIME_810CYCLES_5);
LL_ADC_SetChannelSingleDiff(ADC3, LL_ADC_CHANNEL_TEMPSENSOR, LL_ADC_SINGLE_ENDED);
/* USER CODE BEGIN ADC3_Init 2 */
// LL_ADC_SetChannelPreSelection(ADC3, LL_ADC_CHANNEL_TEMPSENSOR);
ADC3->PCSEL |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_CHANNEL_TEMPSENSOR) & 0x1FUL));
LL_ADC_StartCalibration(ADC3, LL_ADC_CALIB_OFFSET_LINEARITY, LL_ADC_SINGLE_ENDED);
// LL_ADC_StartCalibration(ADC3, LL_ADC_CALIB_OFFSET, LL_ADC_SINGLE_ENDED);
while (LL_ADC_IsCalibrationOnGoing(ADC3)); // 等待校准完成
LL_ADC_Enable(ADC3);
while (LL_ADC_IsActiveFlag_ADRDY(ADC3) != SET) {};
LL_ADC_REG_StartConversion(ADC3);
/* USER CODE END ADC3_Init 2 */
}
/* USER CODE BEGIN 1 */
uint16_t adc_val;
uint16_t TS_CAL1;
uint16_t TS_CAL2;
float mpu_temp;
void read_adc_value(void)
{
uint16_t timeout = 1000;
LL_ADC_REG_StartConversion(ADC3);
while (!LL_ADC_IsActiveFlag_EOC(ADC3))
{
timeout--;
if (timeout == 0)
{
LL_ADC_REG_StopConversion(ADC3);
break;
}
}
LL_ADC_ClearFlag_EOC(ADC3);
adc_val= LL_ADC_REG_ReadConversionData16(ADC3);
TS_CAL1 = *(__IO uint16_t *)(0x1FF1E820);
TS_CAL2 = *(__IO uint16_t *)(0x1FF1E840);
mpu_temp = ((110.0f - 30.0f) / (TS_CAL2 - TS_CAL1)) * (adc_val - TS_CAL1) + 30.0f;
}

|