Trick for scaling in embedded processors

Suppose you have an ADC signal e.g 12-bit value that needs to be scaled. If your processor is for instance an 8-bit AVR and/or you have few cycles to waste on scaling you cannot afford using floating point arithmetic and not even division. Often the following scheme can be used with sufficient accuracy:

Suppose it is a current you want to find with the following “ideal” floating point scaling

curr_A = adc_raw_value * 0.121

This could be done in integer math by: curr_A = adc_raw_value * 121/1000, but that would use divisions which on many processors are a costly operation. Instead of division by 1000 we should use division by say 1024 (or any other 2^k number) – then division is simply replaced by a shift operation which is usually very fast. Hence, by trial and error we search for a number “k” s.t curr_A = adc_raw_value * (0.121*2^k)/2^k introduce the lowest rounding error instead. Just make sure that “k” is not so large to cause overflow…

 

Leave a comment