2025-04-15 2:46 AM
I'm using the internal temp sensor on a C011 chip *actually the STM32C0116 dev board) and trying to get an approximate reading in C, using __LL_ADC_CALC_TEMPERATURE_TYP_PARAMS macro.
I understand that this won't be very accurate, but it seems that when I use the T_CAL1 value stored in ROM the result actually gets significantly worse. Here is my code:
#define VDDA_APPLI ((uint32_t) 3300)
#define TEMPSENSOR_TYP_CAL1_V (( int32_t) 760)
#define TEMPSENSOR_TYP_AVGSLOPE (( int32_t) 2530)
#define TEMPSENSOR_CAL_VREF ((uint32_t) 3000)
int32_t get_last_temp_reading(void)
{
uint16_t t_cal1 = *TEMPSENSOR_CAL1_ADDR;
return (int32_t)__LL_ADC_CALC_TEMPERATURE_TYP_PARAMS(TEMPSENSOR_TYP_AVGSLOPE,
//TEMPSENSOR_TYP_CAL1_V,
t_cal1,
TEMPSENSOR_CAL1_TEMP,
VDDA_APPLI,
raw_value,
LL_ADC_RESOLUTION_12B);
}
My raw_value is 787. When I use the typical datasheet value of 760, I get a result of -20. If I use t_cal1 (which has a value of 1048) I get -134
What is going on? OK, I don't expect much accuracy, but using the internal cal value should at least make things better not worse?
Solved! Go to Solution.
2025-04-15 7:05 AM
indeed, I think sampling time was the issue. I went back into CubdeMX, slowed down the ARC clock and the sampling setting to about 10us, and now I get results of about 70 degrees, which are believable.
I still cannot make sense of the supplied macro in the library, but if I do it myself, I get these numbers now.
Thanks very much for your help.
2025-04-15 4:12 AM
What temperature are you expecting to see?
The t_cal1 value is an ADC reading, not the value in mV. Multiply by 3000, the divide by 4096 to get the voltage.
2025-04-15 4:57 AM
I expect to see somewhere near ambient - 25 to 30.
I am just plugging numbers into the macro. In all honesty I spent a good hour trying to relate the macro to the datasheet without success. it is not even clear to me what reference is used internally for the temp sensor.
2025-04-15 5:14 AM
using the datasheet and trying to do it manually (screenshot below):
Avg Slope Code = 2.53 x 4096/3000 = 3.45
sense data = 787 x 3.3 / 3 = 866
TS_CAL_1 = 1048
Temp = ((866 - 1048) / 3.45) + 30 = -23
I am just putting the numbers into the equation, exactly as described, so something is incomplete or unclear here.
2025-04-15 5:20 AM
Well, the 787 you quote as raw value does indeed correspond to -20°C, according to the calculations given in the manual. Using the t_cal1 value it's -22.8
I would expect to see an ADC count of around 1030 at 25°C
2025-04-15 6:19 AM
In addition to these:
2025-04-15 7:05 AM
indeed, I think sampling time was the issue. I went back into CubdeMX, slowed down the ARC clock and the sampling setting to about 10us, and now I get results of about 70 degrees, which are believable.
I still cannot make sense of the supplied macro in the library, but if I do it myself, I get these numbers now.
Thanks very much for your help.