Author Topic: C - log value - thermistor resistance to temperature conversion  (Read 1516 times)

0 Members and 1 Guest are viewing this topic.

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
C - log value - thermistor resistance to temperature conversion
« on: February 05, 2019, 05:49:20 am »
Hi,

I wrote a thermistor resistance value to temperature conversion program by the combination of PIC18F57K42 & MPLAB x IDE. Inside the conversion, I am using log calculation. I got a value using c compiler, but that value is not same as the calculator value. Here i attached one simple c code. 
Code: [Select]
#include <stdio.h>
#include <math.h>

void main()
{
    float Rth=29498.553;
    float Rr=50000;
    float a;
   
    a = log (Rth/Rr);
   
    printf("a = %f", a);

}
  ////////////////////
a =  - 0.527682     ////after compiling i got that value. But the calculator value is -0.22916929139 

I am also changing data type float to double, but i got only the same value. Please let me know, what is making wrong...

I am using the following formula for conversion: Tkelvin = (beta * room_temp) / (beta + (room_temp * log (Rthermistor / Rroom_temp_res))) . Is it correct or else please suggest other than that one.

Advance in thanks...!
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11891
  • Country: us
Re: C - log value - thermistor resistance to temperature conversion
« Reply #1 on: February 05, 2019, 05:54:49 am »
When you do log(x) on a calculator you are getting log10(x). However, when you do log(x) in your C program you are getting loge(x), which would be ln(x) on a calculator. You need to decide which kind of logarithm you want.
 
The following users thanked this post: arul

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
Re: C - log value - thermistor resistance to temperature conversion
« Reply #2 on: February 05, 2019, 06:36:55 am »
Thankyou so much #IanB
 

Offline mark03

  • Frequent Contributor
  • **
  • Posts: 711
  • Country: us
Re: C - log value - thermistor resistance to temperature conversion
« Reply #3 on: February 05, 2019, 03:56:50 pm »
Also, if you only need single-precision floating point as shown (the "float" type, not "double"), use the library calls with an "f" suffix, like logf.  That will save a conversion from float to double and back again, and the library call is presumably faster too.
 

Offline georges80

  • Frequent Contributor
  • **
  • Posts: 912
  • Country: us
Re: C - log value - thermistor resistance to temperature conversion
« Reply #4 on: February 05, 2019, 04:14:03 pm »
If you only need say 1C resolution and your thermistor is likely only 1% tolerance (or worse), a simple lookup table to convert from ADC reading to degree C is fast and compact (compared to dragging in a floating point library and log functions) especially with an 8 bit uC.

cheers,
george.
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2550
  • Country: us
Re: C - log value - thermistor resistance to temperature conversion
« Reply #5 on: February 05, 2019, 04:41:34 pm »
float logf (float f); and float log10f (float f); single precision log functions are NOT defined in the XC8 compilier.

You must use the double precision versions:

   double log (double f);       // natural log
   double log10 (double f);     // base 10 log
 
The following users thanked this post: arul

Offline mark03

  • Frequent Contributor
  • **
  • Posts: 711
  • Country: us
Re: C - log value - thermistor resistance to temperature conversion
« Reply #6 on: February 05, 2019, 04:44:43 pm »
float logf (float f); and float log10f (float f); single precision log functions are NOT defined in the XC8 compilier.

You must use the double precision versions:

   double log (double f);       // natural log
   double log10 (double f);     // base 10 log


What??  :palm:
Yet another reason to switch to ARM, or any other architecture really, that doesn't tie you to a single vendor's tools.
 
The following users thanked this post: Yansi, arul

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2416
  • Country: us
Re: C - log value - thermistor resistance to temperature conversion
« Reply #7 on: February 05, 2019, 05:55:47 pm »
I second the recommendation to use a lookup table, with whatever precision (number of data points) you require for your application. Interpolation between data points gets you a good enough result.
 
The following users thanked this post: arul

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4427
  • Country: dk
Re: C - log value - thermistor resistance to temperature conversion
« Reply #8 on: February 05, 2019, 06:24:39 pm »
Also, if you only need single-precision floating point as shown (the "float" type, not "double"), use the library calls with an "f" suffix, like logf.  That will save a conversion from float to double and back again, and the library call is presumably faster too.

on AVR it doesn't matter they are the same, 32bit single precision
 
The following users thanked this post: arul


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf