Double string

Status
Not open for further replies.

TucsonDon

Member
I have a project that I am working on that I'm using the ADC on a PIC18F45K22 with a thermistor. I am converting the ADC output to degrees but I need to convert a double to an ascii string.

I am using MPLab IDE with xc8 compiler. It has itoa the library but no file to do what I need.

double Thermistor(int RawADC)
{
double Temp;
Temp = log(10000.0*((1024.0/RawADC-1)));
// =log(10000.0/(1024.0/RawADC-1)) // for pull-up configuration
Temp= 1/(0.001129148 +(0.000234125 +(0.0000000876741 * Temp * Temp ))*Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celsius to Fahrenheit
return Temp;
}
 
Why not use itoa anyway? Get the integer part, convert it and then subtract from double before multiplying the double by 1000 and get 4 decimal places by converting to int again.

Or, if your temp can't go above 650°C then multiply by 100 and use itoa to convert and insert decimal point as appropriate.

Or, better still use 8.8 variables and keep it all in integers.

Mike.
Edit, some code I wrote for integers in the form nn.nnn (Your double * 1000 cast to int)
Code:
void Put8(unsigned int num){
    //buf in the form nn.nnn, as largest is 65.535
    unsigned char buf[7],i,j;
    itoa(buf,num,10);
    i=0;
    while(buf[i]!=0)
        i++;
    j=6;
    while(i!=255)              //move to end of buffer
        buf[j--]=buf[i--];
    while(j!=255)
        buf[j--]='0';         //pad with zeroes
    buf[0]=buf[1];           //shuffle
    buf[1]=buf[2];            //to make room for decimal
    buf[2]='.';              //insert decimal
    for(i=0;i<6;i++)
        Output(buf[i]);
}
 
Last edited:
itoa() wont work anyway... There is an ltoa() made for doubles..

If you do decide to use sprintf... then use the "%ld" specifier rather than the "%d"

C:
char buffer[10];
sprintf(buffer,"%ld",temp);
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…