Terenceang 0 Posted March 3, 2013 Share Posted March 3, 2013 As above.. spend a couple of hours googling on this topic and not getting anywhere? Any help is appreciated. Thanks, Terence Quote Link to post Share on other sites
dellwoodbu 1 Posted March 3, 2013 Share Posted March 3, 2013 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 Jigar4Electronics, Terenceang and bluehash 3 Quote Link to post Share on other sites
bluehash 1,581 Posted March 4, 2013 Share Posted March 4, 2013 As above.. spend a couple of hours googling on this topic and not getting anywhere? Any help is appreciated. Thanks, Terence Like dw suggested. I use sprintf. Works well. Make sure you increase stack size. Quote Link to post Share on other sites
Terenceang 0 Posted March 4, 2013 Author Share Posted March 4, 2013 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 Quote Link to post Share on other sites
aBUGSworstnightmare 1 Posted March 4, 2013 Share Posted March 4, 2013 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--; } } bluehash, Jigar4Electronics and Terenceang 3 Quote Link to post Share on other sites
bluehash 1,581 Posted March 4, 2013 Share Posted March 4, 2013 Perfect snip! Thanks. Quote Link to post Share on other sites
Terenceang 0 Posted March 5, 2013 Author Share Posted March 5, 2013 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--; } } Jigar4Electronics 1 Quote Link to post Share on other sites
Jigar4Electronics 0 Posted March 17, 2013 Share Posted March 17, 2013 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 ??? Quote Link to post Share on other sites
bluehash 1,581 Posted March 17, 2013 Share Posted March 17, 2013 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? Jigar4Electronics 1 Quote Link to post Share on other sites
Jigar4Electronics 0 Posted March 18, 2013 Share Posted March 18, 2013 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? Quote Link to post Share on other sites
bluehash 1,581 Posted March 18, 2013 Share Posted March 18, 2013 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? Quote Link to post Share on other sites
Bingo600 0 Posted March 18, 2013 Share Posted March 18, 2013 (edited) 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 March 18, 2013 by bluehash Added code tags. Jigar4Electronics and bluehash 2 Quote Link to post Share on other sites
bluehash 1,581 Posted March 18, 2013 Share Posted March 18, 2013 Ohh btw ... I didn't really look at the ftoa() , but shouldn't that generated string be terminated with a '\0' before returning Point. Quote Link to post Share on other sites
Jigar4Electronics 0 Posted March 20, 2013 Share Posted March 20, 2013 @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. Quote Link to post Share on other sites
bluehash 1,581 Posted March 20, 2013 Share Posted March 20, 2013 @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. Jigar4Electronics 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.