cancel
Showing results for 
Search instead for 
Did you mean: 

have problem on get internal temperature sensor value in STM32F091

DPark.2
Associate II

When getting the value from the internal temperature sensor, the value is not stable.

I am testing it inside the office, and the chip surface temperature is 25 degrees Celsius.

The code outputs the internal temperature sensor value every second, and it is output as follows.

temp 334

temp 333

temp 334

​temp 334

temp 334

temp 68

temp 333

temp 68

temp 68

temp 333

temp 333

temp 334

temp 68

temp 68

temp 68

temp 334

temp 334

temp 68

temp 334

​I'm not sure if the init code or import code is wrong.

```C

// ADc Init Code

static void MX_ADC_Init(void)

{

  /* USER CODE BEGIN ADC_Init 0 */

  ADC1->CHSELR = ADC_CHSELR_CHSEL16;                              /* (1) */

  ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; /* (2) */

  ADC->CCR |= ADC_CCR_TSEN;                                       /* (3) */

  /* USER CODE END ADC_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC_Init 1 */

  /* USER CODE END ADC_Init 1 */

  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 

  */

  hadc.Instance = ADC1;

  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

  hadc.Init.Resolution = ADC_RESOLUTION_12B;

  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

  hadc.Init.LowPowerAutoWait = DISABLE;

  hadc.Init.LowPowerAutoPowerOff = DISABLE;

  hadc.Init.ContinuousConvMode = ENABLE;

  hadc.Init.DiscontinuousConvMode = DISABLE;

  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

  hadc.Init.DMAContinuousRequests = DISABLE;

  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

  if (HAL_ADC_Init(&hadc) != HAL_OK)

  {

    Error_Handler();

  }

  /** Configure for the selected ADC regular channel to be converted. 

  */

  sConfig.Channel = ADC_CHANNEL_2;

  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    Error_Handler();

  }

  /** Configure for the selected ADC regular channel to be converted. 

  */

  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    Error_Handler();

  }

  /* USER CODE BEGIN ADC_Init 2 */

  /* USER CODE END ADC_Init 2 */

}

```

```C

// print ADC Temp Code

void printTemp(void)

{

  // ADC1->CR |= ADC_CR_ADSTART;

  // while ((ADC1->ISR & ADC_ISR_EOC) == 0)

  // {

  // };

  int32_t temperature; /* will contain the temperature in degrees Celsius */

  temperature = (((int32_t)ADC1->DR * VDD_APPLI / VDD_CALIB) - (int32_t)*TEMP30_CAL_ADDR);

  temperature = temperature * (int32_t)(110 - 30);

  temperature = temperature / (int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);

  temperature = temperature + 30;

  DEBUG_PRINT_RN("temp %d\r\n", (int32_t)temperature);

}

```

Reference link

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

I would guess you're converting both channel 2 and the temp sensor. That's what it looks like your initialization code does.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3

Prepare and post a minimal but complete compilable code exhibiting the problem.

JW

I posted only the ADC related initialization code and the code of the function to get the temperature.

Is it said that there are more than necessary? Conversely, is it saying that there is no necessary part?

TDK
Guru

I would guess you're converting both channel 2 and the temp sensor. That's what it looks like your initialization code does.

If you feel a post has answered your question, please click "Accept as Solution".