2021-04-29 07:29 AM
Hello all,
I am working with the STM32L152RE and want to read out the internal temperature sensor of the mikrocontroller. My code is the following:
#define TS_CAL1 *((uint16_t*) 0x1FF800FA) // 30 °C
#define TS_CAL2 *((uint16_t*) 0x1FF800FE) // 10 °C
double Get_Temp(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
HAL_StatusTypeDef Res;
// GLOBAL CONFIGURATION
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_DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
Res = HAL_ADC_Init(&hadc); // HAL_OK
// CHANNEL CONFIGURATION
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
Res = HAL_ADC_ConfigChannel(&hadc, &sConfig); // HAL_OK
// WAKE UP TEMPERATURE SENSOR
ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;
// WAIT FOR STABILIZATION (datasheet: >10 microseconds)
HAL_Delay(100);
// START ADC CONVERSION
Res = HAL_ADC_Start(&hadc); // HAL_OK
Res = HAL_ADC_PollForConversion(&hadc, 100); // HAL_OK
Res = HAL_ADC_Stop(&hadc); // HAL_OK
// READ RESULTING DATA
int TempAdc = HAL_ADC_GetValue(&hadc); // 589
// CALCULATE TEMPERATURE
int CAL1 = TS_CAL1; // 663
int CAL2 = TS_CAL2; // 845
double Slope = (110-30)/(double)(CAL2-CAL1); // 0.4395
double Degree = (Slope * (TempAdc-CAL1)) + 30; // -2.5274 °C
return Degree;
}
So the problem is, that I read and calc the temperature -2,5 °C. It should be about 22 °C. I did the instructions in the reference manual (RM0038 page 287) and consider the spezifications in the datasheet (page 112). I know the accuracy of TS_CAL1 and TS_CAL2 is +-5°C but I think this value ist too small.
I am grateful for any help.
Best regards,
Tobias
Solved! Go to Solution.
2021-04-29 08:19 AM
Hi,
This is funny, I had the exact same problem this afternoon, however on a L452 device, but I believe it is the same issue.
The TS_CAL values are measured with 3.0 Vcc supply voltage. I suppose that like me your system has 3.3 Vcc supply voltage.
Apply a x1.1 correction factor on the TempAdc, and you shoud get the right value of around 23°C.
Best regards,
Kraal
2021-04-29 08:19 AM
Hi,
This is funny, I had the exact same problem this afternoon, however on a L452 device, but I believe it is the same issue.
The TS_CAL values are measured with 3.0 Vcc supply voltage. I suppose that like me your system has 3.3 Vcc supply voltage.
Apply a x1.1 correction factor on the TempAdc, and you shoud get the right value of around 23°C.
Best regards,
Kraal
2021-05-03 07:45 AM
Thank you! That solved the problem.
For others who have the same problem, one more addition: The factor 1.1 is calculated by 3.3/3.0
Best regards,
Tobias