SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on the software and hardware for Atmel's STK standard.
By jamal014
#152446
Hi all,
i want to send data (float type) from uC ATMega.
i'm currently using this
Code: Select all
printf("%.2f\r",variable);
so the uC sent data in 'variable'. but i think this code spend a lot time.
can anyone tell me sending data more faster?
i'm currently using CV avr. any code is accepted.
thank you.
By fll-freak
#152447
Floating point format operations do indeed take time and code space on any processor. No surprise here.

There is no magic "faster" code. If there was, it would be used by the printf function. With that said, there are tricks to making things faster but at the expense of other factors.

Just what is it you are trying to do? What is receiving this data? How accurate does it need to be? Could it be send in binary and not is ASCII?

one possibility is:
Code: Select all
int q;
q = (int)(variable*100 + 0.5)
Now print q, but know that it is 100 times bigger than 'variable' and needs to be divided by 100 by the receiver.
User avatar
By viskr
#152451
Just some rough benchmarks on an ARM show floating point operations are about 10 times slower than integer ones, maybe a little worse on an AVR.

Most likely you are sending the number to the PC, much better to send the raw value to the PC and let it do the floating point conversion, it's running a few hundred times faster, has floating point hardware and wider accumulators.
By jamal014
#152461
Ok, thank you for the answers. i'll try both of yours.

i want to send 'angle' data from a filter calculation. there are 3 data.
most likely i use
Code: Select all
printf("%.2f %.2f %.2f",var1,var2,var3);
as you know, it take alot time.
By stevech
#152594
let's assume your algorithm is in floating point and you can't / don't want to convert it to fixed point for the sake of size/speed of calculations.

You could convert each variable to a whole number with an implied decimal point and send those on some interface medium.
Like
printf("%d", int()(fpVar * 100.0);
assuming the integer part is small and the fractional part is OK at 2 digits.

Sending the binary form of floats on an interface is a problem because the sender and receiver have to agree on what format (such as one of the IEEE standards), that has to be the intrinsic type on both ends, and the transmission byte order has to be considered, etc. So it's much easier to send as text.