cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert temperature sensor to C?

Keith Henrickson
Associate II

I'm trying to read the built-in temperature sensor on the MCU we're using (STM32L011F4P6)

We only have the one calibration datapoint, so I can't use the temperature conversion function I'm familiar with.

The code is as follows:

int32_t analog_get_temperature(void)

{

uint32_t temperature_reading;

int32_t temp_calc;

uint32_t u16vcc = analog_sample_vcc();

temperature_reading = analog_sample_channel(ADC_CHANNEL_TEMPSENSOR);

 temp_calc = __LL_ADC_CALC_TEMPERATURE_TYP_PARAMS(TEMPERATURE_MV_PER_DEGREEC * 1000, *TEMPSENSOR_CAL2_ADDR, TEMPSENSOR_CAL2_TEMP, u16vcc, temperature_reading, LL_ADC_RESOLUTION_12B);

return temp_calc;

}

u16vcc is 3299, which is the same as what I measure with a voltmeter.

The calibration value at 0x1FF8007E is: 0x03A7

TEMPSENSOR_CAL2_TEMP is 130

The slope from the datasheet is:

#define TEMPERATURE_MV_PER_DEGREEC 1.61

The measured temperature_reading is: 614.

When we init the ADC:  hadc.Init.Resolution = ADC_RESOLUTION_12B;

But the calculation tells me that it's -143C. I don't care how cold it feels outside in Wisconsin.... one, I'm not outside. Two, even then, it's not THAT cold!

Anyone spot what I'm missing?

2 REPLIES 2
S.Ma
Principal

On STM32C0 there is one point reference and a slope from the datasheet.

So the conversion function is in analog.c from this project

Second parameter to the _LL_ADC_CALC_TEMPERATURE_TYP_PARAMS() macro is not the calibration value you've read out from TEMPSENSOR_CAL2_ADDR, but the *voltage* corresponding to that value:

* @param __TEMPSENSOR_TYP_CALX_V__ Device datasheet data: Temperature sensor voltage typical value (at temperature and Vref+ defined in parameters below) (unit: mV).

* On STM32L0, refer to device datasheet parameter "V130" (corresponding to TS_CAL2).

So you should divide it by the ADC range (0xFFF) and multiply by the datasheet-given voltage at which this reading was taken (I think it's 3000mV for the 'L0, but I did not check).

Even then, you'll get around 12deg.C if I calculated correctly.

Did you perform calibration before measurement? Did you observe the temperature sensor settling time after switchon, and the sampling time, given in datasheet?

However, this may also be correct. Using the upper-end value for slope, 1.75mV/C, I get 22deg.C. The lack of second calibration point is the price you pay for using a "low-end" model.

JW