Hi,

I'm working on a K21 target and want to read the internal temperature sensor. I've set up the PE for the ADC and am able to get ADC readings out. However converting from the ADC readings to the actual Temperature reading is proven to be a little bit tricky. I've consulted AN3031 an various threads on here but haven't got a solution yet.

My problem is that the temperature converted from the ADCR is not correct even though it increase and decrease in the right direction, my guess is that my implementation of the transfer function is not correct. Below is my transfer function implementation:

uint32_t GetTemperature(uint32_t ADCR_Temp) { uint32_t Vtemp_mV = ADCR_Temp * 1800 / ( (1 << AdcLdd1_ADC_RESOLUTION) - 1 ) ; //Convert the ADC reading into voltage uint32_t Temp1; uint32_t Temp2; Temp2 = ( Vtemp_mV - 716 ) * 617; // 1 / 0.00162 = 617 Temp1 = 25000 - Temp2; return (uint32_t) (Temp1); } }

Here is the printf of the program:

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1711,Temperature: 2.788 ▒C

Vtemp ADCR: 1710,Temperature: 3.405 ▒C

And I'm pretty sure I'm sitting in a room at about 20 degrees C, unless there is something else going on (!?).

And I'm using 1.8V for VREFH and Ground for VREFL. ADC resolution 12 bits

Any help is much appreciated.

Many thanks,

If your Vref (1.8V) is 'tighter' than the internal bandgap (+/-3%), then don't bother with all that from AN3031! IF you have a 'better absolute voltage' someplace you could use that for a reference point...

Your first equation looks good, assuming Vtemp comes in as a right-justified 'fraction of full scale', where full-scale comes from #of bits=AdcLdd1_ADC_RESOLUTION=12 (so / 0xFFF -- why not just >> AdcLdd1_ADC_RESOLUTION?).

And I assume here that the K21 has the constants of 716mV@25C, and 1.62mV/C, per your math.

But Temp1&2 (and the returned result) will need to be signed integers for all possible calculations to work (above and below 25C[716mV], above and below 0C).

So that said, an AtoD value of 1711, on 12 bits from 1.8V, nets 752mV, or 36mV higher (colder) than 25C. At 1.62mV/C as indicated, that indeed comes to 22C 'colder' than 25C.

I'm afraid you have to go back to the raw subroutine-input and see that the conversion process is being properly started, from the proper channel. A 25C 'chip temperature' needs to return 'something close to' the expected 716mV, or 1629 (out of 4096 steps from 1.8V).

OR your 1.8V is running 5% 'low'. If 1711 counts WERE to truly represent 716mV (+/-10mV chip-tolerance, at 25C, or +/-6C), THEN full-scale represents 1.71V as your Vref. SO, you might indeed try the concept in AN3031 of calculating 'backwards' to your Vref (but NOT the method!). If you convert the 1volt bandgap, then ((1 << AdcLdd1_ADC_RESOLUTION) * 1000 /* BandGap mV */ ) / BandGAPresult will return Vref in mV -- inject that into your first equation (instead of a fixed 1800) and see if the temperatures come out better!