Jump to content
43oh

newbie question! how do I use UARTprintf with floats?


Recommended Posts

UARTprintf does not yet support floats directly.

 

A couple of options exist.  

 

1) use sprintf or similar from the standard 'C' libraries to do the float to string conversion into a character array and then use %s with UARTprintf to put the float (which is now stored as a string in the array) on the UART.

 

2) Do a little bit of casting and multiplying.  cast the float to a integer.  Call this the integer part.  multiply the float by something like 1000 to get three decimal points of precision.  cast this new number to an integer again and now subtract off the integer part * 1000. Call this the fraction part.  Then with UARTprintf do "%d.%03d", IntegerPart, FractionPart.

 

Dexter

Link to post
Share on other sites

Thanks Dexter and Hash.

 

I am using sprintf now, works well.

 

I turn on --printf support full, and stacks to 4096.

 

Regards,

Terence

 

 

UARTprintf does not yet support floats directly.

 

A couple of options exist.  

 

1) use sprintf or similar from the standard 'C' libraries to do the float to string conversion into a character array and then use %s with UARTprintf to put the float (which is now stored as a string in the array) on the UART.

 

2) Do a little bit of casting and multiplying.  cast the float to a integer.  Call this the integer part.  multiply the float by something like 1000 to get three decimal points of precision.  cast this new number to an integer again and now subtract off the integer part * 1000. Call this the fraction part.  Then with UARTprintf do "%d.%03d", IntegerPart, FractionPart.

 

Dexter

Link to post
Share on other sites

Hi, 

 

or do it like this and stay with UARTprintf only. 

 

aBUGSworstnightmare

 

//*****************************************************************************
//
//! \brief Float to ASCII
//!
//! Converts a floating point number to ASCII. Note that buf must be
//! large enough to hold
//!
//! \param f is the floating point number.
//! \param buf is the buffer in which the resulting string is placed.
//! \return None.
//!
//! \par Example:
//! ftoa(3.14) returns "3.14"
//!
//
//*****************************************************************************
void ftoa(float f,char *buf)
{
    int pos=0,ix,dp,num;
    if (f<0)
    {
        buf[pos++]='-';
        f = -f;
    }
    dp=0;
    while (f>=10.0)
    {
        f=f/10.0;
        dp++;
    }
    for (ix=1;ix<8;ix++)
    {
            num = (int)f;
            f=f-num;
            if (num>9)
                buf[pos++]='#';
            else
                buf[pos++]='0'+num;
            if (dp==0) buf[pos++]='.';
            f=f*10.0;
            dp--;
    }
}
Link to post
Share on other sites

wow!!!

 

 

Hi, 

 

or do it like this and stay with UARTprintf only. 

 

aBUGSworstnightmare

 

//*****************************************************************************
//
//! \brief Float to ASCII
//!
//! Converts a floating point number to ASCII. Note that buf must be
//! large enough to hold
//!
//! \param f is the floating point number.
//! \param buf is the buffer in which the resulting string is placed.
//! \return None.
//!
//! \par Example:
//! ftoa(3.14) returns "3.14"
//!
//
//*****************************************************************************
void ftoa(float f,char *buf)
{
    int pos=0,ix,dp,num;
    if (f<0)
    {
        buf[pos++]='-';
        f = -f;
    }
    dp=0;
    while (f>=10.0)
    {
        f=f/10.0;
        dp++;
    }
    for (ix=1;ix<8;ix++)
    {
            num = (int)f;
            f=f-num;
            if (num>9)
                buf[pos++]='#';
            else
                buf[pos++]='0'+num;
            if (dp==0) buf[pos++]='.';
            f=f*10.0;
            dp--;
    }
}

 

Link to post
Share on other sites
  • 2 weeks later...

Thanks Dexter and Hash.

 

I am using sprintf now, works well.

 

I turn on --printf support full, and stacks to 4096.

 

Regards,

Terence

I am also doing the same thing... could you help me please?

 

I know how to use sprintf... but it doesn't work

 

And I don't know how to turn on printf support full & how to set stack to 4096??

And can anyone explain me how actually we are making controller able to print float by just increasing stack?

 

 

@aBUGSworstnightmare,

Thanks for that code....

 

I've used it as per following, but I am unable to make it works for me:

 

ftoa(g,str);         // I've defined g as float and str as " char *str; "
UARTprintf("valu of g is : %s",str);
 

 

 

Am I wrong ???

Link to post
Share on other sites

I am also doing the same thing... could you help me please?

 

I know how to use sprintf... but it doesn't work

 

And I don't know how to turn on printf support full & how to set stack to 4096??

And can anyone explain me how actually we are making controller able to print float by just increasing stack?

 

 

@aBUGSworstnightmare,

Thanks for that code....

 

I've used it as per following, but I am unable to make it works for me:

 

ftoa(g,str);         // I've defined g as float and str as " char *str; "
UARTprintf("valu of g is : %s",str);
 

 

 

Am I wrong ???

What error do you get?

Link to post
Share on other sites

the value of g is varying from 0.00 to 3.00

If I check it on ccs then it's updationg properly as it should be... in shourt pragram is proper and it's working properly..

 

Only problem Is I am unable to print "g" through UARTPrintf...

It is showing me "valu of g is : 0.00" Everytime... Though it's value is 0.008562 or 2.89562 or anything else (as I've observe it on ccs using watch expression feature) It shows me that same string every time! :(

 

Any suggestions?

Link to post
Share on other sites

the value of g is varying from 0.00 to 3.00

If I check it on ccs then it's updationg properly as it should be... in shourt pragram is proper and it's working properly..

 

Only problem Is I am unable to print "g" through UARTPrintf...

It is showing me "valu of g is : 0.00" Everytime... Though it's value is 0.008562 or 2.89562 or anything else (as I've observe it on ccs using watch expression feature) It shows me that same string every time! :(

 

Any suggestions?

How did you declare str? Any warnings?

Link to post
Share on other sites

ftoa(g,str);         // I've defined g as float and str as " char *str; "

 

Did you remember to let str  point at a something valid ?

Using just str would garble something ...... Whatever str i pointing at ...

 

 

 

{
char buffer[20] , *str;

str = buffer;

ftoa(g,str)

}


or - just plain

{
char buffer[20];
 ftoa(g,buffer);
}

 

 

 

Ohh btw ... I didn't really look at the ftoa() , but shouldn't that generated string be terminated with a '\0' before returning

 

 

/Bingo

Edited by bluehash
Added code tags.
Link to post
Share on other sites

@Bingo600,
Thanks ... It works now.

 

BTW may I know how I can do this using UARTPrintf? I mean I've tried using increased satck size (it was 256 and I've inceased to 4096) & also have edited the printf option to the full, but by doing this I am getting "ERROR" written on the screen .

 

It's not programming error it is "ERROR" string I am getting on UART instead of float no.

If you can help me understanding this also then it would be great for me.

 

Thanks again to all of you.

Link to post
Share on other sites

@Bingo600,

Thanks ... It works now.

 

BTW may I know how I can do this using UARTPrintf? I mean I've tried using increased satck size (it was 256 and I've inceased to 4096) & also have edited the printf option to the full, but by doing this I am getting "ERROR" written on the screen .

 

It's not programming error it is "ERROR" string I am getting on UART instead of float no.

If you can help me understanding this also then it would be great for me.

 

Thanks again to all of you.

Can you post your code.. or zip file. I'll see what I can do.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...