Skip to content

STM32 HAL issue - reading internal temp after VBAT measurement. #1685

@campag

Description

@campag

@bcostm
Measuring VBAT sets the ADC_CCR_VBATE bit in ADC->CCR, and there doesn't appear to be a way with the HAL to clear it.

If it isn't cleared, VBAT remains connected to the ADC channel in preference to temperature, so VBAT readings are returned in place of temperature. See the note at the bottom of page 226 of the reference manual RM0368, DM00096844.pdf

I resorted to poking the register directly:

float read_temp(void)
{
    ADC_ChannelConfTypeDef sConfig = {0};

    // Configure ADC channel
    sConfig.Rank         = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
    sConfig.Offset       = 0;
    sConfig.Channel      = ADC_CHANNEL_TEMPSENSOR;

    HAL_ADC_ConfigChannel(&hAdc, &sConfig);

    //disable Vbat from  ADC1_IN18 input channel, shared with temperature sensor
    ADC->CCR &= ~ADC_CCR_VBATE;
    //better added instead in stm32f4xx_hal_adc.c at line 1123, also line 753 in adc_ex.c?

    HAL_ADC_Start(&hAdc); // Start conversion

    // Wait end of conversion and get value
    if (HAL_ADC_PollForConversion(&hAdc, 10) == HAL_OK) {
        uint16_t value=HAL_ADC_GetValue(&hAdc);
        HAL_ADC_Stop(&hAdc);
        return  25.0f+(3.3f*(float)value/(float)ADCMAXVALUE-0.76f)/0.0025f;
    } else {
        return -273.15f; //hell has frozen over
    }
}

As per the comments in the above code, perhaps that bit should be cleared when configuring the ADC to read the temperature in stm32f4xx_hal_adc.c & stm32f4xx_hal_adc_ex.c ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions