2017-02-22 01:26 AM
Posted on February 22, 2017 at 10:26
Hi everyone,
I'm trying to get the internal temperature on my STM32L0.
I have write the major part of the script but unfortunally the reading value is 0.
Does anyone can have a look on my script and tel me if you see something ?
Thank you very much for your help
Regards
Simon
&sharpdefine TEMP130_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FF8007E))
&sharpdefine TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FF8007A))
&sharpdefine VDD_CALIB ((uint16_t) (300))
&sharpdefine VDD_APPLI ((uint16_t) (330))
int32_t ComputeTemperature(uint32_t measure)
{
int32_t temperature;
temperature = ((measure * VDD_APPLI / VDD_CALIB) - (int32_t)*TEMP30_CAL_ADDR );
temperature = temperature *(int32_t)(130-30);
temperature = temperature /(int32_t)(*TEMP130_CAL_ADDR -*TEMP30_CAL_ADDR);
temperature = temperature + 30;
return(temperature);
}
void ConfigTemperature(void){
//ADC Ready
ADC1->ISR |= ADC_ISR_ADRDY;
//Activation of the clock
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
//Continuous mode
ADC1->CFGR1 |= ADC_CFGR1_CONT;
//Channel temp sensor
ADC1->CHSELR |= ADC_CHANNEL_TEMPSENSOR;
//Select the sample time
ADC1->SMPR |= ADC_SMPR_SMP_1;
//Turn on the temp sensor
ADC->CCR |= ADC_CCR_TSEN;
wait(1);
//Start the conversion
ADC1->CR |= ADC_CR_ADSTART;
wait(1);
pcMain.printf('%Measure %i\n\r', measure);
pcMain.printf('%The temperature value is %i\n\r',ComputeTemperature(measure));
}
int main(void)
{
ConfigTemperature();
}
Solved! Go to Solution.
2017-02-23 09:28 AM
I just ran into this weeks ago using STM32FL052. You need to enable the internal temp-sensor in CFGR3. These are the HAL calls from my code:
// We need to call these to enable VREFINT and TempSensor readings
HAL_ADCEx_EnableVREFINT(); HAL_ADCEx_EnableVREFINTTempSensor();This is the specific register bit setting from the HAL code:
/* Enable the Buffer for the ADC by setting ENBUF_SENSOR_ADC bit in the CFGR3 register */
SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_SENSOR_ADC);The Reference manual also states that you need to set up the ADC sampling time really long to read the internal 2x sensors. Here's from my code:
hadc.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; // S.Krattiger - Must keep at 239.5 (33.5uS) for accurate Temp & Vref reads
To read the internal temperature, there's a MACRO defined in the HAL code, here's my implementation that returns an int value in (C). Since I read multiple ADC's, my results are DMA'd and show up in 'adcResult[2]' below. And it's all based on an accurate reference voltage, so you'll need to read that too.
GlobalStats.CurrentTemp = __LL_ADC_CALC_TEMPERATURE(GlobalStats.ReferenceVoltage, adcResult[2], LL_ADC_RESOLUTION_12B));
Hope this helps!
SteveK
2017-02-22 01:44 AM
Hi
salydan
,Did you have a look to Cube Example
STM32Cube_FW_L0_V1.8.0\Projects\STM32L073RZ-Nucleo\Examples_LL\ADC\ADC_TemperatureSensor
Take care also to apply the correct sampling time. ( refer to data sheet )
Hope it help,
Olivier
2017-02-22 02:42 AM
What's measure?
JW
PS. Do you enable ADC clock in RCC? Post a minimal but complete compilable code exhibiting the problem.
2017-02-22 03:00 AM
Thank you for your quick reply
GALLIEN.Olivier
,I've already have a look on this project. But unfortunally I can't use all of this functions in my project... The sampling time seems to be correct, indeed after verifications in the data sheet the set sampling time is up to the one cited. But I'll have a new look to be sure, you are right.Simon
2017-02-22 06:59 AM
Hi
Waclawek.Jan
,I try to get the internal temperature but the returned value is 0. I add some modification now (Write between 'NEW PART BEGIN' and 'NEW PART END' comment). This is a link to obtain my project on the mbed platform
https://developer.mbed.org/users/SimonNOWAK/code/STM32L0_TempSensor/
The correct value should be around 600mV (+/- 50mV). This is approximatively for 21°C. And yes the clock is enabled. Thank you Simon2017-02-22 07:09 PM
Posted on February 23, 2017 at 03:09
Simon,
Played with it in
https://developer.mbed.org/users/wek/code/STM32L0_TempSensor/
. Remarks:
JW
2017-02-23 06:19 AM
Thank you very much !
This script work very well. The code is dirty but it came from STM32 doc...My next challenge is to use it after a deepSleep. Indeed the configuration of the ADC is not complete after that.Thank you for your time, I was looking for this problem since Monday...Have a good day
Simon
2017-02-23 09:28 AM
I just ran into this weeks ago using STM32FL052. You need to enable the internal temp-sensor in CFGR3. These are the HAL calls from my code:
// We need to call these to enable VREFINT and TempSensor readings
HAL_ADCEx_EnableVREFINT(); HAL_ADCEx_EnableVREFINTTempSensor();This is the specific register bit setting from the HAL code:
/* Enable the Buffer for the ADC by setting ENBUF_SENSOR_ADC bit in the CFGR3 register */
SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_SENSOR_ADC);The Reference manual also states that you need to set up the ADC sampling time really long to read the internal 2x sensors. Here's from my code:
hadc.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; // S.Krattiger - Must keep at 239.5 (33.5uS) for accurate Temp & Vref reads
To read the internal temperature, there's a MACRO defined in the HAL code, here's my implementation that returns an int value in (C). Since I read multiple ADC's, my results are DMA'd and show up in 'adcResult[2]' below. And it's all based on an accurate reference voltage, so you'll need to read that too.
GlobalStats.CurrentTemp = __LL_ADC_CALC_TEMPERATURE(GlobalStats.ReferenceVoltage, adcResult[2], LL_ADC_RESOLUTION_12B));
Hope this helps!
SteveK
2017-02-24 02:32 AM
Hi Steve,
Thanks a lot for your help. Your code is clear and helpful for me. Your approach with HAL is very interresting and look easier than directly use registers.
Have a good daySimon
2020-06-01 02:55 PM
Does ST anywhere provide a complete example, written using HAL calls, for reading temperature from the STM32L0? Very frustrating trying to piece together bits of information from the posts here + other resources on the internet. This should not be a science project.