| ??? 03/10/09 19:35 Read: times |
#163326 - I was a bit hasty ... Responding to: ???'s previous message |
This approach is most likely faster than using a divison on a '51, but will result in four levels of nesting, which may or may not agree with your sense of aesthetics. :) <p>
It should be possible to do it without nesting that deeply.
unsigned char bar_value_tmp, speed_in_mph_tmp;
bar_value_tmp = 0;
speed_in_mph_tmp = SpeedCalculated;
if(speed_in_mph_tmp >= 80)
{
speed_in_mph_tmp -= 80;
bar_value_tmp += 8;
}
if(speed_in_mph >= 40)
{
speed_in_mph_tmp -= 40;
bar_value_tmp += 4;
}
if(speed_in_mph >= 20)
{
speed_in_mph_tmp -= 20;
bar_value_tmp += 2;
}
if(speed_in_mph >= 10)
{
speed_in_mph_tmp -= 10;
bar_value_tmp += 1;
}
/* Round. */
if(speed_in_mph >= 5)
{
bar_value_tmp += 1;
}
Bar_Value = bar_value_tmp;
There. No cycle-intensive divisions or 16 bit operations. |



