Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

float to string conversion for PIC

Status
Not open for further replies.

fzn10

New Member
I cant get my head round this one. Iv been looking around on the net and it seems that the way that people usually do it in c, is to use the sprintf function, my compiler for the PIC does not support such libraries. i was wondering if any one know of how to do it.I managed to get code that converts int to a character string i tested it and it works fine. Id like to have something simialar to it. the code is bellow. Also could any tell me what the line marked with the arrow is doing, especially what it means by the "number%10". Thanks

void func1( int number ) {
int i;
char string[] = "0000000";


for( i = 6 ; number != 0 ; i-- )
string = number%10 + '0'; <---------------------
number = number/10;
}

func2( &string[i + 1] );

}
 
i actually undertand the code now but still cant figure out how i can implement the same thing with a float
 
Can't help you with C, but the simple answer is NOT to use floats, they are slow, take up loads of space, and are inaccurate - with sensible program design they are hardly ever required, use integers instead.
 
x%y gives the remainder of x/y. This integer value is converted to char type and the ascii code of '0' is added.
 
Tell us which compiler you are using!

Nigel is right. Everybody assumes they need floats and 99% of the time they're wrong and it's a terribly inefficient choice.
 
The approach is basically the same as with the integers. You use division and remainder operations to compute the mantissa. Then you use the integer routine to work on the exponent. On a 32 bit float on a 20MHz PIC you would be lucky to do the conversion in 25 to 50 milliseconds.

The book called the "Standard C Library" or "Numerical Recipes in C" might be good references.
 
thanks guys, the thing was i needed to use to display decimal numbers, but figured it out without using float or doulbe.
 
Did you need to display decimal #'s on an LCD by chance.
I am trying to sense the period of a signal with the input capture on the PIC18 and then compute the frequency. The frequency is then going to be displayed on the LCD. I am going to have to store this frequency value as a float right? then convert it to a string to print it to my LCD module. Any ideas?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top