Dear Experts
We normally use sprintf to output floating numbers but I didn't see this function on TivaWare.
Could you let me know how to output floating number on UART to PC?
Thanks for your comment.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Dear Petrei
Thanks for your feedback.
Actually I found an example on Deyisupport:
http://www.deyisupport.com/question_answer/dsp_arm/sitara_arm/f/25/p/23334/79500.aspx
float Xangle,temp;
temp = Xangle;
printf("\r\n");
if(Xangle>=0){
printf("radX:%d.%d",(int32_t)temp ,(int32_t)((temp -(int32_t)temp )*1000));
}
else{
temp = 0-temp;
printf("radX:-%d.%d",(int32_t)temp ,(int32_t)((temp -(int32_t)temp )*1000));
}
But when I try to show 1.0045, I see 1.4 on PC side.
Do you have any comment about this result?
This is a test.
Thank you.
Hi,
I apologize - I do not understand Chinese/japan to realize if your example is good or not for the purpose. You do not mention if you run the example or not. The example definitely does not uses sprintf and worse, after defining temp variable as float, it is converted to int32_t, so the rest of operations are suspicious since float - integer is converted back to float and then to integer, so a lot are lost ...
So i cannot comment your result - maybe you use a clean operation and see the result. Take into account the number of decimals to be shown must be specified ...
Petrei
Hi Jefferey,
I have used ftoa() before to print float values to PC. I just copy the ftoa() code from the internet to my c file.
I have read in some posts here that you can use sprintf using CCS. However, I have not done that before.
-kel
Jefferey said:printf("radX:-%d.%d",(int32_t)temp ,(int32_t)((temp -(int32_t)temp )*1000));
But when I try to show 1.0045, I see 1.4 on PC side
Not surprising. Look up the documentation on the %d format specifier, it will drop leading zeros unless you tell it otherwise. Also you only ask for three digits after the decimal point.
Robert
Another option, particularly if you are short of flash space, is to multiply the number you want to output by a factor of 10. For example, if you have 2.718 , you could multiply by 1000 to get 2718, send that out the UART and on the PC side, divide again by 1000.
Apologies to Jeffrey who suggested this already above.
@ Gareth,
Our small band likes your suggestion. Beyond those, "short of flash space" - also works for those, "devoid of any flash space!"
Unstated are additional, "tricks/handling" required in the calculation/generation of the initial, float value - should a "float-free" MCU be in play. (i.e. for illustration: 5 / 1.8396 = 2.718) Thus - scaling up 1.8396 to 18,396 and 5 to 50,000,000 (note that both "fit" w/in 32 bit register) yields 2717. (2717.982 w/real calculation) The receiving (workhorse) PC can then easily massage this integer into the required, decimal point laden format...
Avoiding floating point does offer certain (and other) advantages...