Skip to main content
Graduate II
June 19, 2019
Solved

Reading the temperature sensor of an STM32H753

  • June 19, 2019
  • 1 reply
  • 1982 views

When reading the temperature sensor of an STM32H753, I read above 65 °C, right at start-up. The board lays on the desk, and the MCU doesn't feel that warm, maybe up to 36 °C.

The environmental temperature now, is about 24 °C.

After a some time, the MCU temperature increases to above 85 °C.

The reading seems somewhat high, to me. Below is the code I used.

Update: I added the call to the ADC calibration function.

HAL_ADCEx_Calibration_Start(&hadc3, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);

Below updated code, which now reads reasonable values:

#define TS_CAL1_REG 0x1FF1E820 // Calibration ADC value at 30 °C
#define TS_CAL2_REG 0x1FF1E840 // Calibration ADC value at 110 °C
#define TEMPERATURE_ADC_SAMPLES 8
 
uint16_t MCU_Temperature_AVG = 50;
 
uint16_t TemperatureCalculate(uint32_t ts_data)
{
 // 0x1FF1E820 Calibration ADC value at 30 °C = 0x2fc0, 12224
 uint16_t ts_cal1 = *(uint16_t*) (TS_CAL1_REG);
 // 0x1FF1E840 Calibration ADC value at 110 °C = 0x3cb4, 15540
 uint16_t ts_cal2 = *(uint16_t*) (TS_CAL2_REG);
 
 return (80 * (ts_data - ts_cal1)) / (ts_cal2 - ts_cal1) + 30;
}
 
void TemperatureStart(void)
{
 // To get reasonable results of 50-60 °C when the system is heated up,
 // we need to call the calibration function before starting the ADC:
 HAL_ADCEx_Calibration_Start(&hadc3, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
 
 // Perform first reading to adjust average value
 HAL_ADC_Start(&hadc3);
 if (HAL_ADC_PollForConversion(&hadc3, 1000000) == HAL_OK)
 {
 MCU_Temperature_AVG = TemperatureCalculate(HAL_ADC_GetValue(&hadc3));
 }
 HAL_ADC_Stop(&hadc3);
 
 HAL_ADC_Start_IT(&hadc3);
}
 
int16_t TemperatureRead(void)
{
// Temperature (°C) = ((110 - 30) * (TS_DATA - TS_CAL1)) / (TS_CAL_2 - TS_CAL_1) + 30
// Where:
// • ts_cal1: temperature sensor calibration value at 30 °C, VDDA= 3.3 V
// • ts_cal2: temperature sensor calibration value at 110 °C, VDDA= 3.3 V
// • ts_data: temperature sensor output from ADC
// Temperature (°C) = 80 * (ts_data - ts_cal1) / (ts_cal2 - ts_cal1) + 30
// ts_cal1 = T1_30 = at 30 Degrees
// ts_cal2 = T2_110 = at 110 Degrees
 
 uint16_t MCU_Temperature_New = TemperatureCalculate(HAL_ADC_GetValue(&hadc3));
 
 // Average temperature reading
 // One part new value + 7 parts old value is 8 parts in total
 uint16_t MCU_Temperature = MCU_Temperature_New + MCU_Temperature_AVG * (TEMPERATURE_ADC_SAMPLES - 1);
 // Divide by 8
 MCU_Temperature_AVG = MCU_Temperature / TEMPERATURE_ADC_SAMPLES;
 
 if (HAL_ADC_Start_IT(&hadc3) != HAL_OK)
 {
 /* Start Conversation Error */
// dmc_puts("HAL_ADC_Start\n");
 }
 
 return MCU_Temperature_AVG;
}
 
int16_t TemperatureGet(void)
{
 return MCU_Temperature_AVG;
}
 
/**
 * @brief This function handles ADC3 global interrupt.
 */
void ADC3_IRQHandler(void)
{
 /* USER CODE BEGIN ADC3_IRQn 0 */
 (void) TemperatureRead();
 
 /* USER CODE END ADC3_IRQn 0 */
 HAL_ADC_IRQHandler(&hadc3);
 /* USER CODE BEGIN ADC3_IRQn 1 */
 
 /* USER CODE END ADC3_IRQn 1 */
}
 
 print("Temperature: %d C\n", TemperatureGet());

Am I doing something wrong?

    This topic has been closed for replies.
    Best answer by Uwe Bonnes

    This has been discussed recently. Look at the old thread. Possible errors: Different VREF, no calibration cycle ...

    1 reply

    Graduate II
    June 19, 2019

    This has been discussed recently. Look at the old thread. Possible errors: Different VREF, no calibration cycle ...

    Jack3Author
    Graduate II
    June 21, 2019

    Many thanks, I didn't find that, it pointed out the problem.

    I needed to call the calibration before starting the ADC:

    void TemperatureStart(void)
    {
     // To get reasonable results of 50-60 °C when the system is heated up,
     // we need to call the calibration function before starting the ADC:
     // Otherwise we may read around 83 °C
     HAL_ADCEx_Calibration_Start(&hadc3, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
     
     // Perform first reading to adjust average value
     HAL_ADC_Start(&hadc3);
     if (HAL_ADC_PollForConversion(&hadc3, 1000000) == HAL_OK)
     {
     MCU_Temperature_AVG = TemperatureCalculate(HAL_ADC_GetValue(&hadc3));
     }
     HAL_ADC_Stop(&hadc3);
     
     HAL_ADC_Start_IT(&hadc3);
    }

    Visitor II
    July 6, 2021

    Hi Jack,

    I just want to read The Temp Sensor.

    I use a STM32H723.

    I see that you have a lot of experience in this TS.

    Can you please direct me to configure my system?

    Thanks in advance

    Eran