NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Question on tempsensor - division operation x100?

October 18, 2009
by puargs
puargs's Avatar

In the tempsensor example, you have a for loop which executes 100 times, and at the end of each loop performs a division:

for(i=0; i<100; i++) {
      last_sample = adc_read();
      this_temp = sampleToFahrenheit(last_sample);

      // add this contribution to the average
      temp_avg = temp_avg + this_temp/100.0;
    }

In this code, a division operation is being performed 100 times. My question is this - assuming the double temp_avg variable is large enough in memory to hold 100 samples before division, would it be better to place the division outside the for loop? For example:

for(i=0; i<100; i++) {
      last_sample = adc_read();
      this_temp = sampleToFahrenheit(last_sample);

      // add this contribution to the average
      temp_avg = temp_avg + this_temp;
    }
temp_avg = temp_avg / 100;

The reason I ask is because I know division operations take significantly more processing time in cycles. However, what I don't know is if the compiler will catch this and automatically change it so it doesn't matter in the assembly, or if it doesn't make any difference because the microcontroller is RISC based... Does anyone know? When I start diving into making my own projects, I want to be sure on what will/won't affect the number of cycles.

Thank you very much in advance!

October 18, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi puargs,

You're way should work, and should do the division only once. Indeed, since this is a floating point division, it does take lots of cycles. So yes, go for it! A few quick notes, though:

  1. The entire for loop is basically waiting on the ADC readings to get back from the adc_read() call, which take roughly 13 ADC cycles (see ATmega168 datasheet page 250), or about 117 microseconds -- which is about 1664 clock cycles. So the real question is as to whether the division takes shorter or longer than that (since the other steps are so simple). I'm not 100% sure, but I'd guess that a floating point division was required perhaps a few hundred cycles at most.
  2. Since it's floating point, these instructions will take quite a bit of time. If cycles really count, then you need to use integers (or fixed-point math) whenever possible. But then you need to be careful about overflowing the capacity of the variable.
  3. You can look at the instructions being used by doing something like "make tempsensor.ass" and looking at the disassembled code. It can be very tough to follow the assembly, particularly with jumps around as part of the floating point code, but just from the length of the "divsf3" function you can tell that it'll take a bunch of cycles!

Hope that helps,

Mike

October 19, 2009
by puargs
puargs's Avatar

Exactly the answer I needed, thanks very much! :D

Post a Reply

Please log in to post a reply.

Did you know that you can aim and fire a servo-controlled water gun from your computer? Learn more...