Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

Using Fixed point maths in microcontrollers

Fixed point mathematics lets you calculate results that need to have a fractional 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 the following:

ADC reading Floating point calculation Floating point result
0 0 * 0.0196 0.0
127 127 * 0.0196 2.489
255 255 * 0.0196 4.998

With Fixed point mathematics you can do this quickly and efficiently using only integer type variables.

Note: Floating point 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 was used.

An example problem

Here 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 you can find here takes about 90 memory words).

So how to do it : First of all the step size of each ADC bit is:

5V/255 = 0.0196 (19.6mV)

Next 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 = 65535.

Since 49980 < 65535 the scheme is ok.

This has calculated the result using fixed point shown in the table below:

ADC reading Fixed point calculation Fixed point result
0 0 * 196 0.0
127 127 * 196 24892
255 255 * 196 49980

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 resource.

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:

   Click Here

Recent Articles

  1. PIC PWM: Easy Pulse width Modulation on the PIC Micro.

    PIC PWM : What its for and how to use it. Including an example showing typical setup of a PIC PWM module with register settings.

    Read more

  2. The Essential I2C Tutorial: All you need to know about I2C...

    How to use I2C. In this tutorial you will learn all about the 2 wire I2C serial protocol. Learn how easy it is to use, how it works and when to use it...

    Read more

  3. PIC LCD volt meter : How to use the ADC for voltage measurement.

    A 0-5V LCD volt meter project using an HD44870 display and one PIC micro ADC.

    Read more

  4. PIC Programming using ICSP

    PIC programming : How to use ICSP for programming a PIC microcontroller giving some ICSP connection diagrams and it also shows how to use MPLAB X and ICPROG.

    Read more

  5. PIC ICSP In Circuit Serial Programming

    How to program PIC ICSP, IN-Circuit, live and still have a working programmer and operational circuit!

    Read more

  6. An Ultrasonic Distance meter project: How to make one using a microcontroller.

    Understand how an Ultrasonic Distance meter works by building one your self – including Free software to drive the microcontroller and display.

    Read more

Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:

And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:


Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:

But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates

Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Back to Top