Using Fixed point maths in microcontrollers
point mathematics lets you calculate results that need to have a
multiplier and it lets you calculate results that would normally need
floating point variables. This saves a huge amount of memory.
For example if you want to calculate an ADC result that uses a 19.6mV
step size then you need to multiply the ADC result by 0.0196.
Normal maths using a floating point variable would result in
With Fixed point mathematics you can do this quickly and
efficiently using only
variables use up huge amounts of resources in a microcontroller as
microcontrollers are only really good at integer type variables.
To do floating point complex library functions are called up.
This is similar to the old 386 processor (it too was no good
at floating point
operations being far too slow) so a separate floating point processor
is the problem to solve:
Using the minimum memory resources use an 8 bit ADC with 5V reference
to transmit using a PIC micro serial data to a PC via the serial port
the ADC reading every second.
The most important decision is not to use floating point - this saves
lots of memory resource - all the rest; sending data to the serial port
and timing will not take up much memory (the soft serial port (TX) that
can find here
takes about 90 memory words).
So how to do it : First of all the step size of each ADC bit is:
= 0.0196 (19.6mV)
work out the type of the variable that is big
enough to hold the maximum expected value but is also the minimum size
variable you can get away with. Factors such as the number of ADC bits
and required calculation accuracy affect the variable size.
To convert, using fixed point, multiply the ADC reading by an integer
scale factor but make sure the maximum value fits within the variable
e.g If you choose a scale factor of 196 check the maximum value:
Maximum value : 255 * 196 = 49980
Here you can use a 16 bit variable (or unsigned int) as the
maximum value is smaller than the maximum value that the 16 bit
variable can hold.
Max value that unsigned int can store is 216-1
Since 49980 < 65535 the scheme is ok.
This has calculated the result using fixed point shown in the table
The decimal point is fixed four to the left -
i.e. fixed point.
This has use the internal compiler routine '16 bit multiply' which is
much simpler than a floating point multiply so you save loads of memory
| About Me