cancel
Showing results for 
Search instead for 
Did you mean: 

Problem to get internal temperature STM32L0

Simon NOWAK
Associate II

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();

}

1 ACCEPTED SOLUTION

Accepted Solutions
Steve Krattiger
Associate III
Posted on February 23, 2017 at 18:28

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

View solution in original post

10 REPLIES 10
Olivier GALLIEN
ST Employee
Posted on February 22, 2017 at 10:44

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

Olivier GALLIEN
In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
Posted on February 22, 2017 at 11:42

What's measure?

JW

PS. Do you enable ADC clock in RCC? Post a minimal but complete compilable code exhibiting the problem.

Posted on February 22, 2017 at 11:00

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

Posted on February 22, 2017 at 14:59

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

Simon

Posted on February 23, 2017 at 03:09

Simon,

Played with it in

https://developer.mbed.org/users/wek/code/STM32L0_TempSensor/

. Remarks:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • that code is still very dirty, I didn't care to clean it up
  • note I have a 'L053 Disco rather than a Nucleo, you'd need to switch it back in mbed or simply just have a look what did I change in the .c source
  • the key problem was that even if ADC mode set to continuous it needs to be started somehow, so the key change is adding the ADC1->CR |= ADC_CR_ADSTART; line
  • I looked and yes, this start is missing in the 'snippets' at the end of RM0367 in the software-triggered continuous example chapter A.8.6 - anybody from ST listens?
  • the first result read from DR returns an incorrect value, maybe because of the errata mentioned in the correct answer
  • https://community.st.com/s/question/0D50X00009XkYNlSAN/strange-behavior-of-stm32l053-adc
  • ? that's why I looped (maybe the delay should be shorter or the whole tweaked in some other way - I'm not going to investigate further)
  • this is probably not how the continuous mode is intended to be used (running ADC and reading out result at random); should work though - but maybe you want to use a single-conversion mode, or use the WAIT facility? Just listing the options.
  • the snippet for calibration is incorrect, too, but works as this problem does not cause problem in the first calibration after reset - see
  • <LINK NO LONGER ACTIVE>
  • initial post's last section

JW

Posted on February 23, 2017 at 14:19

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

Steve Krattiger
Associate III
Posted on February 23, 2017 at 18:28

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

Posted on February 24, 2017 at 10:32

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 day

Simon

DNick.1
Associate II

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.