Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 9th August 2006, 05:07 PM   #1
Default float to string conversion for PIC

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[i] = number%10 + '0'; <---------------------
number = number/10;
}

func2( &string[i + 1] );

}
fzn10 is offline  
Old 9th August 2006, 06:23 PM   #2
Default

i actually undertand the code now but still cant figure out how i can implement the same thing with a float
fzn10 is offline  
Old 9th August 2006, 07:19 PM   #3
Default

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 9th August 2006, 09:03 PM   #4
Default

x%y gives the remainder of x/y. This integer value is converted to char type and the ascii code of '0' is added.
eng1 is offline  
Old 9th August 2006, 10:30 PM   #5
Default

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.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline  
Old 10th August 2006, 04:19 AM   #6
Default

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.
Papabravo is offline  
Old 10th August 2006, 01:50 PM   #7
Default

thanks guys, the thing was i needed to use to display decimal numbers, but figured it out without using float or doulbe.
fzn10 is offline  
Old 27th October 2009, 11:09 PM   #8
Default

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?
jme147 is offline  
Old 28th October 2009, 08:41 AM   #9
Default

Quote:
Originally Posted by jme147 View Post
I am going to have to store this frequency value as a float right?
No, you could store it as an integer, and manually add the decimal point.

In other words, instead of storing it as 6.4KHz, store it as 6400Hz.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply

Tags
conversion, float, pic, string

Thread Tools
Display Modes




All times are GMT. The time now is 05:17 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker