2023-01-06 12:43 PM
I've made several attempts to get the temperature from the ADC and all end up resulting in negative numbers.
I'm using an ADC reference IC that provides a 2.048 reference.
I've tried a few different approaches.
The simplest approach I've done is trying to use the built in macros:
__HAL_ADC_CALC_TEMPERATURE, and __LL_ADC_CALC_TEMPERATURE.
serprintf("%i\n", __HAL_ADC_CALC_TEMPERATURE(2048, aADC3ConvertedData[0], ADC_RESOLUTION_12B));
serprintf("%i\n", __LL_ADC_CALC_TEMPERATURE(2048, aADC3ConvertedData[0], LL_ADC_RESOLUTION_12B));
Where aADC3ConvertedData is an array that consists of temperature, vref, and vbat.
These macros give me ~-135C. (one calls the other anyway, I think)
I've attempted to get the vref using this macro:
vref = __LL_ADC_CALC_VREFANALOG_VOLTAGE(aADC3ConvertedData[1], ADC_RESOLUTION_12B);
That gave me the same result.
Lastly, I've used the datasheet function:
There is at least one caveat with this function
1) TS_CAL1 and TS_CAL2 were measured with a vref of 3300mV
(They are stored as 12251, and 16254 respectively)
I've converted the values to my vref of 2048mV using the following process:
3300/65535
Out[128]: 0.05035477225909819
2048/65535
Out[129]: 0.03125047684443427
12251*0.05035477225909819
Out[132]: 616.896314946212
616/0.03125047684443427
Out[135]: 19711.69921875
16254*0.05035477225909819
Out[136]: 818.4664682993821
818.4664682993821/0.03125047684443427
Out[141]: 26190.527343750004
TS_CAL1 = 19740.380859375
TS_CAL2 = 26190.527343750004
Raw ADC value for temperature (TS_DATA) is 6050.
Plugging into the above equation gives me -139C.
I've left everything at 16b since this is what is recommended in stm32h7_hal_adc.h
Is there something else I have not considered?
2023-01-06 08:04 PM
Check and adapt a Nucleo ADC example which is closet to your chip, which is supposed to measure temperature.
2023-01-08 05:35 PM
Not the main issues, but still...
Check my post in this topic:
https://community.st.com/s/question/0D53W00002167nhSAA/wrong-voltage-reading
2023-01-09 06:00 AM
I see I made a few minor issues, but still something isn't right.
I took a slightly different approach to scale the TS_CAL1 and TS_CAL2 values from 3300mV to 2000mV:
TS_CAL1 = 12251
(12251/65536)*3300
Out[28]: 616.8869018554688
So, this is a "voltage" representation based on this discussion.
I should be able to convert that voltage reading into a scaled version of the ADC signal at my VREF:
616.8869018554688*65536/2000
Out[29]: 20214.15
Validating that:
(20214.15/65536)*2000
Out[31]: 616.8869018554688
I do the same with TS_CAL2 and I get the results of
26819.1
My reading, which is around ~6500 is already scaled to 2000mV. Gives me ~-135C.
However, upon reflection why is my ADC giving me 6050 for temperature?
If TS_CAL1 is taken at 30C, and it's scaled value is 20214.15 then this should be significantly higher since I'm at room temperature. I understand this is not a linear value, but it should be much higher.
2023-01-09 09:24 AM
Additionally, I also have an STM32G491. However, it's using a 3.3V ref. I noticed TS_CAL1, and TS_CAL2 are 1034 and 1384 respectively.
The raw ADC temperature reading is 245.
I applied a heat gun to both MCPUs it increase the temperature and neither one of them changed significantly at all.
My Cube settings for G491
and for H745
2023-01-09 10:49 AM
1.5/2.5 cycles sampling time is certainly incorrect. Read datasheet.
JW
2023-01-09 11:47 AM
> I applied a heat gun to both MCPUs it increase the temperature and neither one of them changed significantly at all.
Then you have a problem with the A/D conversion?
2023-01-09 12:30 PM
This definitely seems to be the cause. Definite disconnect on my end about how it works. Still something isn't clear though. The datasheet excerpt is this:
My ADC is clocked at 120Mhz, however I am sing a prescaler of 2, so it is effectively 60Mhz.
This seems to imply I can select 12.5 cycles?
I noticed with 12.5 cycles this is still not accurate. But as I increase cycles the accuracy increases. For instance, at 12.5 cycles my ADC reading is still ~500 which ends up being -110C.
If I increase the cycles to 92.5 then my temperature is 44C. Closer, and possible.
If I increase the cycles to 247.5 then my temperature calculates to 29C. This is probably correct.
So, I guess I am trying to get a little further understanding of this.
Based on what I've read, Tconv would be (247.5+12.5)/60M
About 4us. So, with this in mind, I am converting 230769 times per second.
Is that the right thinking? The sampling is adequate.
Thanks for pointing me in the right direction.
2023-01-09 12:54 PM
Refer to Temperature sensor characteristics in datasheet, should have something like:
ADC sampling time when reading the temperature: 9 μs