This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Convert NTC value in °C

Other Parts Discussed in Thread: TMS320F28062F

Hello, 

I'm working with a TMS320F28062F and I would like to convert the value of an NTC thermistor in °C (I'm a new user of C2000 MCU).

My schematic is 3V3 -> NTC thermistor (10K @ 25°C) -> ADC measure -> 2K2 resistor -> GND.

First, I calculate the resistor value of the NTC :

Vr = ADCmeas * ADC scale factor = ADCmeas * (Valim / 4095) = ADCmeas * (3.3/4095) = ADCmeas *  0.0008058608

Rntc = R * ((Valim - Vr)/ Vr)

After that I would like to calculate the temperature in °C with the Steinhart-Hart equation

T = (1 / A + (B * ln(Rntc)) + (C * (ln(Rntc))^3)) - 273.15

here is my code :

_iq value;

_iq Adc_SF = _IQ(0.0008058608);
_iq Fixed_Resistor = _IQ12(2200.0);
_iq Voltage_3V3 = _IQ18(3.3);
_iq Steinhart_coeff_A = _IQ(0.000865282);
_iq Steinhart_coeff_B = _IQ(0.000254940);
_iq Steinhart_coeff_C = _IQ(0.000000183);

// convert temperature bridge A
value = (_iq)ADC_readResult(obj->adcHandle,ADC_ResultNumber_7); // ADC measure
value = _IQmpy((value<<18),Adc_SF);                                                              // ADC 12 bits exponent multiply by ADC scale factor
value = _IQ18div((Voltage_3V3 - value),value);                                               // (Valim - Vr) / Vr
value = _IQ12mpy((value>>6),Fixed_Resistor);                                              // Resistor's value of NTC (18 bits exponent because 188500 ohms @ -40°C)
value = _IQ12div(_IQ(1.0),(Steinhart_coeff_A + _IQmpy(Steinhart_coeff_B,log(value)) + _IQmpy(Steinhart_coeff_C,pow(log(value),_IQ(3.0))))); // Temperature in °C
pAdcData->T.value[0] = value;

I can read the resistor's value of NTC but not the result of the Steinhart-Hart equation.

Please, help me.

Thank you in adavance,

Best regards,

  • Hi Sebastien,

    What do you mean with the following statement?

    Sebastien Dassy said:
    I can read the resistor's value of NTC but not the result of the Steinhart-Hart equation.

    Can't you read the Q value in the Watch window? Please clarify

    BR Andreas

  • Hi Andreas,

    If I put the Steinhart-Hart equation in comments in my code,I can read the resistor's value of NTC in a watch window (with Q12 format). This value is consistent.

    When I add the Steinhart-Hart equation in my code, the result is not consistent.

    I tried : result = log(Rntc) but the result is not consistent neither.

    Thanks for your help Andreas

    BR

  • Ok let's go ;-)

    What is your global Q-format? I am asking this because I suspect that you are *messing* a little with your q operations. Without going into details I saw that you are using different Q formats. Perhaps you are right - just asking. Did you try to single step through the code just to follow the intermediate results?

    Andreas

  • If I understand well, Q-format is :

    1 bit for the sign (MSB)

    x bits for the exponent part

    x bits for the fraction part

    total 32 bits.

    I degan step by step like this :

    1) the analog sensor has a 12 bits resolution and I want to have them in the exponent part so Q18-format is chosen => result was consistent

     I applied scale factor (*(3.3/4095) on this value => the result was consistent  (in the code : value = _IQmpy((value<<18),Adc_SF); )

    In this step I assume that Q28 would have been OK.

    2) The first part of the equation to find the resistor of the NTC is (3V3 - Vadc) / Vadc so Q18-format was still OK (in the code : value =_IQ18div((Voltage_3V3 - value),value);)

    minimum value of the NTC is 534 ohms => min value of this equation is 0.243 so Q18 is OK

    maximum value of the NTC is 188500 ohms => max value of this equation is 85.84 so Q18 is OK

    => the result was consistent 

    3) The second part of the equation to find the resistor of the NTC is "previous result * 2200" (in the code : value = _IQ12mpy((value>>6),Fixed_Resistor);)

    minimum value of the NTC is 534 ohms => min value of this equation is 534 so Q18 is OK

    maximum value of the NTC is 188500 ohms => max value of this equation is 188500 so Q18 is not enough, Q12 is OK

    => the result was consistent  with Q12

    4) I wrote the Steinhart-Hart equation to have the result in °C

    ( in the code : value =_IQ12div(_IQ(1.0),(Steinhart_coeff_A + _IQmpy(Steinhart_coeff_B,log(value)) + _IQmpy(Steinhart_coeff_C,pow(log(value),_IQ(3.0)))));)

    => the result was not consistent

    I tried to calculate LN(Rntc) in order to do it step by step

    (in the code : value = log(value))

    => the result was not consistent so a decided to post a message

    Sebastien

     

     

  • OK,

    Again what is your global Q format?? If I do remember well it should be IQ24 as long as you don't manipulate it.

    -> are you sure, that your IQ12div is doing the right thing i.e. is _IQ(1.0) converting into the correct Q format?

    And then I think you should use IQexp instead pow(). For the log command it is a bit more painful, but you should find some support in this thread.

    http://e2e.ti.com/support/microcontrollers/c2000/f/171/t/58620.aspx

    Andreas

  • Yes you have right, the default Q-format is Q24.

    On monday, I will check step by step to have all the right Q-format.

    I will try IQexp and IQlog.

     

    Thanks for your help, I'll keep you informed on monday.

    BR