www.allpcb.com

Author Topic: Binary point math in C (PIC micro)  (Read 467 times)

0 Members and 1 Guest are viewing this topic.

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 55
  • Country: us
Binary point math in C (PIC micro)
« on: May 19, 2017, 05:35:51 AM »
I'm working on a project writing software for a PIC16 micro. For one of the functions I need to calculate a floating-point value based on a sensor input.

Since it's an 8-bit microcontroller I'm being asked to use binary point math (which I never used before). After doing some research I found few articles that explains binary point but really doesn't go into the details of using it to solve equations.

The equation is a quadratic formula with couple of coefficient in the 5th decimal place. Does anyone have any good tutorial suggestions?

Basically, I have something like this:
y = x + 0.0000125*k^2 - 0.045*k^ + 0.5

Where "x" is a floating-point value from 0 to 150 and "k" is my value which can be from 0 to 500 in increments of 10. “y” can be from 0 to a 100 and is 10-bits.

Any help is appreciated! Thanks.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 3545
  • Country: gb
Re: Binary point math in C (PIC micro)
« Reply #1 on: May 19, 2017, 05:46:39 AM »
Not sure what is meant by binary point math, do you mean fixed point?

Is this high speed and/or real time?
 

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 55
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #2 on: May 19, 2017, 05:52:44 AM »
Not sure what is meant by binary point math, do you mean fixed point?

Is this high speed and/or real time?

Yes I think I mean fixed point (http://www-inst.eecs.berkeley.edu/~cs61c/sp06/handout/fixedpt.html -- something like this). Sorry not sure about the terminology because I'm new to this. I always used 16-bit micro's and floating point.

System is in real time, 1ms refresh rate, although this can be in seconds, not a fast system.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 1312
  • Country: fi
Re: Binary point math in C (PIC micro)
« Reply #3 on: May 19, 2017, 05:55:15 AM »
Sometimes lookup-table(s) with pre-calculated results can ease the pain and speed up the computation.
 

Offline Andreas

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: de
Re: Binary point math in C (PIC micro)
« Reply #4 on: May 19, 2017, 05:59:39 AM »
Hello,

in this special case you could also use a pre-calculated table with 51 entries (16 bit ea).

with best regards

Andreas
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 1312
  • Country: fi
Re: Binary point math in C (PIC micro)
« Reply #5 on: May 19, 2017, 06:01:51 AM »
A piece-wise interpolated lookup-table may take somewhat less space.

Edit: Andreas was faster :)
 

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 55
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #6 on: May 19, 2017, 06:09:10 AM »
Hmmm... Look up table is interesting but I think the table will be ver large. I have two variables, one is 10-bit and the other 8-bit so that's a lot or combination.
 

Offline Andreas

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: de
Re: Binary point math in C (PIC micro)
« Reply #7 on: May 19, 2017, 06:13:50 AM »
The table of course only for the "k" term.
the y-term is only a simple 16 bit add
(normalized by whatever factor you need).

with best regards

Andreas
 

Online Rerouter

  • Super Contributor
  • ***
  • Posts: 2644
  • Country: au
  • Question Everything... Except This Statement
Re: Binary point math in C (PIC micro)
« Reply #8 on: May 19, 2017, 07:56:06 AM »
What they are refering to is take "y = x +" this you calculate each time, ideally you would make them both fixed point, but a float add is not as expensive as calculating the rest,

For k you can precomute each value and then save that to a table
 

Online rstofer

  • Super Contributor
  • ***
  • Posts: 2369
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #9 on: May 19, 2017, 08:21:32 AM »
That equation just says y=x + (some constant) where the quadratic terms don't involve x.  That seems a little odd but it's your equation.  Precompute a table for the constant terms and just do the addition at run time.  You can use some external method of building the table as long as the floating point representation is correct at run time.  Do the lookup then do the floating point add.

That floating point value for 'x' came from somewhere.  It would be an odd sensor that actually generated IEEE floating point as an output.  The point is, you probably already have the floating point library linked into your code so you might as well just do a floating point add.

Assuming the equation is correct and that the higher order terms don't involve x, I would probably compute the lookup table at startup using the floating point library that is already being linked.

The assumptions are:  'k' is truly a constant, never to be changed and 'x' doesn't get involved in the correction factors.

 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 7990
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Binary point math in C (PIC micro)
« Reply #10 on: May 19, 2017, 08:37:12 AM »
k only needs six bits, because it takes 51 values (0 to 500 in steps of 10, inclusive?), which fits nicely into a 64-count field.

Probably round it up to 8 bits, because why bother packing bits.

Does x have to be floating?  What does it come from?

So, divide k by 10, and multiply everything else accordingly.

That's the first step.

Tim
Seven Transistor Labs, LLC
Electronic Design, from Concept to Layout.
Need engineering assistance? Drop me a message!
 

Online BrianHG

  • Frequent Contributor
  • **
  • Posts: 695
  • Country: ca
Re: Binary point math in C (PIC micro)
« Reply #11 on: May 19, 2017, 11:37:43 AM »
Just so you know, if you don't care about processing speed, in PIC C, even the 8 bit MCU varieties, if you define x, and/or y, and any other variable as a float, they will be a 32 bit floats and just let the compiler do the work on any mathematical equation or function you like.

Do not trouble yourself with such details unless you really need super processing speed, or need to save every ounce of program space which the compiler would generate code to compute the floating point functions.

Just pretend you 8 bit PIC is a 32 bit floating point CPU and program away.  Let the compiler work out all the finer details in the background such is the purpose of a high level language like C instead of programming the PIC in assembly.

You may also mix variable types, like making Y and int (16 bit integer), x a float or double, k a char (8 bit integer).  The compiler will work things out.
« Last Edit: May 19, 2017, 11:43:38 AM by BrianHG »
__________
BrianHG.
 

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 55
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #12 on: May 19, 2017, 01:19:42 PM »
Thanks for all the tips. I don't think I was clear enough, both "x" and "k" are variables (they change over time). "x" and "k" are sensor information. Based on this information I need to calculate "y".
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 3545
  • Country: gb
Re: Binary point math in C (PIC micro)
« Reply #13 on: May 19, 2017, 04:54:29 PM »
Thanks for all the tips. I don't think I was clear enough, both "x" and "k" are variables (they change over time). "x" and "k" are sensor information. Based on this information I need to calculate "y".

In your OP, k can only have values 0 to 500 in increments of 10, then as mentioned by another poster, that leaves the equation to the RHS of the "y = x +" with only 51 values, which could be precomputed and stored ahead of time, leaving you only with an addition to perform.

Is there a particular reason to switch between fixed and floating point? That square root is going to be the slowest part whether you're in fixed or floating point. A perfect reason to pre-calculate a LUT.
 
The following users thanked this post: aiq25

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 55
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #14 on: May 19, 2017, 10:44:45 PM »
In your OP, k can only have values 0 to 500 in increments of 10, then as mentioned by another poster, that leaves the equation to the RHS of the "y = x +" with only 51 values, which could be precomputed and stored ahead of time, leaving you only with an addition to perform.

Is there a particular reason to switch between fixed and floating point? That square root is going to be the slowest part whether you're in fixed or floating point. A perfect reason to pre-calculate a LUT.

I forgot about the values of "k".  |O Now it makes sense.

I prefer to use floating point but that's not the procedure used for the project I'm working on (the other team members only use fixed point math).
 

Online rstofer

  • Super Contributor
  • ***
  • Posts: 2369
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #15 on: May 20, 2017, 01:22:21 AM »
Have you actually verified that equation?  At a minimum, I would put it in a spreadsheet and iterate over min/max values of sensor data and values 'k'.

You stated that the output 'y' would range from 0 - 100 but if the sensor reading is 150, y is never less than 149.674 at k=90 and for the same k and a sensor input of 0, y=-0.326.

Assuming (always a bad idea) that my code is correct, I get the values in the attachment:

One of the things you will deal with is a loss of precision.  You have a value of .125*10^-4 and a value of 10^2 so this takes about 6 digits to represent yet your output is only 4 digits.  In any event, fixed point is simply lining up the decimal points and doing the math.  Floating point differs only in the idea that the value is shifted left as far as possible and the exponent manipulated to account for the shift.  By shifting, floating point retains as much precision as possible.  Fixed point just loses the precision when it runs out of range.
 

Online rstofer

  • Super Contributor
  • ***
  • Posts: 2369
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #16 on: May 20, 2017, 03:17:04 AM »
Once you determine a fixed point notation and the appropriate algorithms, it would be worthwhile to check the precision against a floating point solution.  Any loss of precision will be easy to spot.
 

Online rstofer

  • Super Contributor
  • ***
  • Posts: 2369
  • Country: us
Re: Binary point math in C (PIC micro)
« Reply #17 on: May 23, 2017, 04:50:46 AM »
Mathworks (Matlab) has a Fixed Point Designer tool that might be interesting:

https://www.mathworks.com/products/fixed-point-designer.html
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf