Title: float problem Post by: gtn on December 15, 2007, 04:23:31 16:23 Hello to all :)
For a couple of days I have this strange problem. It is my first meeting with float numbers and PIC. I am using MPLAB and HITECH 9.50 The problem is: I have a float number and I want to add another float to it. Than I want to convert the sum to ascii. Here is the code: Code: #include <htc.h> before sum value decimal fnum =5.885049e-039 4199711 fnum2=0 0 After fnum =5.885049e-039 4199711 fnum2=5.927294e-039 4229858 I think that fnum value should be 2.33 and fnum2 4.04. But it seems not :( Also I cannot convert the float to ascii. All i get is one "f" (without quotation) in the first element of the array. :( If it is important I want to compile the program for PIC16F917 Title: Re: float problem Post by: TomJackson69 on December 22, 2007, 11:11:29 23:11 I never have a luck in using sprintf in MCC18. Here is a short funtion to convert "float" to string that I found in one of the web page (sorry I don't remember where). You can use this in your program to convert float to string then print out the string. Here is the code: /* ********************************************************************************************************* * CONVERT A FLOATING POINT NUMBER TO STRING WITH 1 DECIMAL PLACE * * Description : This function converts a floating point to a null terminated string * with 1 decimal place. * * Examples: * * float f = 9.567; * ftoa(&s[0], f); //s[]={'9','.','5', 0} * float f = -0.189; * ftoa(&s[0], f); //s[]={'-', '0', '.', '1', 0} * Arguments : 'unsigned char* buf' is the pointer to the string holding the conversion result * 'float f' is the input floating point * Returns : Returns the string with unsigned char* buf pointing to. * Notes : This routine modified from itoa10() in ..\sample\misc folder of ht-picc * If more decimal places required, modify the last section of this code * Range of f in (-3,276.7, 3,276.7) * This function does print result like 0.0. ********************************************************************************************************* */ void ftoa(float f, unsigned char *buf, char DecPoint) { unsigned int rem; unsigned char *s,length=0; int i; i = (int)((float)f*10); s = buf; if (i == 0) //print 0.0 with null termination here { *s++ = '0'; *s++ = '.'; *s++ = '0'; *s=0; //null terminate the string } else { if (i < 0) { *buf++ = '-'; s = buf; i = -i; } //while-loop to "decode" the long integer to ASCII by append '0', string in reverse manner //If it is an integer of 124 -> string = {'4', '2', '1'} while (i) { ++length; rem = i % 10; *s++ = rem + '0'; i /= 10; } //reverse the string in this for-loop, string became {'1', '2', '4'} after this for-loop for(rem=0; ((unsigned char)rem)<length/2; rem++) { *(buf+length) = *(buf+((unsigned char)rem)); *(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1)); *(buf+(length-((unsigned char)rem)-1)) = *(buf+length); } /* Take care of the special case of 0.x if length ==1*/ if(length==1) { *(buf+2) = *buf; *buf = '0'; *(buf+1) = '.'; *(s+2)=0; //null terminate } else { *(buf+length) = *(buf+length-1); ///== cai nay MOT dec point ==/// *(buf+length-1)='.'; *(s+1)=0; //null terminate } } } /* Good luck, Tom Title: Re: float problem Post by: arslanweb on December 23, 2007, 01:36:27 01:36 Hello to all :) For a couple of days I have this strange problem. It is my first meeting with float numbers and PIC. I am using MPLAB and HITECH 9.50 The problem is: I have a float number and I want to add another float to it. Than I want to convert the sum to ascii. Here is the code: Code: #include <htc.h> before sum value decimal fnum =5.885049e-039 4199711 fnum2=0 0 After fnum =5.885049e-039 4199711 fnum2=5.927294e-039 4229858 I think that fnum value should be 2.33 and fnum2 4.04. But it seems not :( Also I cannot convert the float to ascii. All i get is one "f" (without quotation) in the first element of the array. :( If it is important I want to compile the program for PIC16F917 Hi, You must first setup the Hi-Tech compiler. You can see in the project opitons. "PICC linker opitons". Select "Printf Support" ; Interger + Long + Float. Or include "Inherit global setting"; "-LF". Than work your code normal. İf is not select float than the prints ans sprintf shows only zero. |