Integer or float divide TOO complex

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would like to take the output of an 8 bit ADC, 0 to 255, and "divide" it up into 32 segements or "buckets" for a datalogging application.

Didn't want to use float maths OR 32 if statements OR 32 case statements. Is there some other way to do this?

Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm ... What about masking/ignoring the 3 least significant bits using AND/shift right. Then the smallest "change" would be 8 ==> 256/8=32 ...

Please tak into account that it's Monday morning :-)

/J

Last Edited: Mon. Feb 13, 2006 - 08:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take the value and shift it right three times.

C:

bucket = ADCvalue >> 3;

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks guys. Then just use bucket as an index into my array, correct?

Cheers,davef

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Following the (not so perfect) code. But for illustrating the algorithm it should be ok:

uint16_t bucket[32];

while ( theLoopShouldRun )
{
     bucket[ADCValue>>3]++;
     doStufWithBucket();
}