Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 27, 2024, 08:43:41 08:43


Login with username, password and session length


Pages: [1]
Print
Author Topic: float problem  (Read 8555 times)
0 Members and 1 Guest are viewing this topic.
gtn
Newbie
*
Offline Offline

Posts: 14

Thank You
-Given: 71
-Receive: 0


« on: December 15, 2007, 04:23:31 16:23 »

Hello to all Smiley
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>
#include <stdio.h>

float fnum=2.33,fnum2;
char outString[8];

void main (void)
{
fnum2=fnum+2.01;

sprintf(outString,"%5.2f",fnum2);

}//end main


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 Sad
Also I cannot convert the float to ascii. All i get is one "f" (without quotation) in the first element of the array. Sad

If it is important I want to compile the program for PIC16F917
Logged
TomJackson69
Active Member
***
Offline Offline

Posts: 218

Thank You
-Given: 26
-Receive: 63


« Reply #1 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
Logged

Con Rong Chau Tien
arslanweb
Newbie
*
Offline Offline

Posts: 13

Thank You
-Given: 1
-Receive: 4


« Reply #2 on: December 23, 2007, 01:36:27 01:36 »

Hello to all Smiley
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>
#include <stdio.h>

float fnum=2.33,fnum2;
char outString[8];

void main (void)
{
fnum2=fnum+2.01;

sprintf(outString,"%5.2f",fnum2);

}//end main


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 Sad
Also I cannot convert the float to ascii. All i get is one "f" (without quotation) in the first element of the array. Sad

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.

Logged

Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC