PDA

View Full Version : FP routines with PBP



skimask
- 15th January 2007, 01:33
I finally got a project that I need to use some floating point math in.
So, I did what everybody should do... I did a search here, found out that on MeLabs site, there's an FP package just waiting to get compiled with PBP and work for me. Also downloaded AN575 (? I think that's what it was) from Microchip.

Question---the files in the FP.ZIP are dated 3/25/04. That's a bit old. Does anybody know if the FP package still works with PBP 2.47 and MPLAB 7.51 (or whatever is the latest version)?

paul borgmeier
- 15th January 2007, 05:48
I finally got a project that I need to use some floating point math in.

Unfortunately, I have never used FP with a PIC - I have been lucky enough to always get by with Fixed Point Math (for the "hard" math). I know some things are better solved with FP but would be curious to know what you are trying to do?

skimask
- 15th January 2007, 07:03
Unfortunately, I have never used FP with a PIC - I have been lucky enough to always get by with Fixed Point Math (for the "hard" math). I know some things are better solved with FP but would be curious to know what you are trying to do?

Quite frankly:

AFR x 6.17 x 454 x .621371 x VSS
--------------------------------
3600 x ( MAF / 100 )

I know most of it can be combined ahead of time, and that's what I'll do obviously.
AFR will be one of 2 values (maybe more), 12.5 or 14.7
6.17 = average density of gasoline in lbs/gallong
454 = grams in 1 pound
.621371 = miles in 1 kilometer
VSS - byte value for speed in kph
MAF - word value for mass air flow grams/second
3600 & 100 - time and decimal point unit conversions

Any guesses as to what I'm building? :)

paul borgmeier
- 15th January 2007, 08:23
Skimask,

Although it is late now, here is something I would try ...

Bash out all the constants as you noted ( 604.365, 710.734). You are left with

604.365VSS/MAF and 710.734VSS/MAF

Multiply these by 10 and keep the integer part (for added accuracy).

Const = 6044 or 7107

ANS = CONST * VSS
ANS = DIV32 MAF

Then use Darrel's trick to retrieve the remainder

http://www.picbasic.co.uk/forum/showthread.php?t=48

Put it back together as needed (Darrel shows this in his thread). This skips even the pain of Fixed Point if it works for you.

Do you need to use the answer in additional math or just display it?
What accuracy do you need?
If you want to pursue Fixed Point then the standard 24.8 format would work (2 word variables)

Oh, it's too late to guess - I'll try that tomorrow - over and out


EDIT: MAF would have to be greater than or equal to 23 or 27 (depending on 6044 or 7107, respectively) in order for the DIV32 result to be less than 65536 - if they were not, you would have to tweak the math or check the ratio of VSS/MAF before the mult / DIV32 - clear as mud?

skimask
- 15th January 2007, 21:59
Skimask,
Although it is late now, here is something I would try ...
Bash out all the constants as you noted ( 604.365, 710.734). You are left with
604.365VSS/MAF and 710.734VSS/MAF
Multiply these by 10 and keep the integer part (for added accuracy).
Const = 6044 or 7107
ANS = CONST * VSS
ANS = DIV32 MAF
Then use Darrel's trick to retrieve the remainder
http://www.picbasic.co.uk/forum/showthread.php?t=48
Put it back together as needed (Darrel shows this in his thread). This skips even the pain of Fixed Point if it works for you.
Do you need to use the answer in additional math or just display it?
What accuracy do you need?
If you want to pursue Fixed Point then the standard 24.8 format would work (2 word variables)
Oh, it's too late to guess - I'll try that tomorrow - over and out

EDIT: MAF would have to be greater than or equal to 23 or 27 (depending on 6044 or 7107, respectively) in order for the DIV32 result to be less than 65536 - if they were not, you would have to tweak the math or check the ratio of VSS/MAF before the mult / DIV32 - clear as mud?

That Darrel trick is fairly slick. I'm going to use that one in the future.
I'd like it accurate down to +/- .01, but +/- .1 will do for now. In the end, like everything, I'd like as much accuracy as I can get as these results will be accumulated over time (don't we all hate tolerance stack-up).

And I thought of the MAF/VSS ratios and overflowing out of 16 bits thing. I might end up having to divide the 2 parts down or multiply them up a bit before the final DIV32. If I have any code space left over, I'm going to throw a couple of checks in there. If MAF/VSS won't overflow the end result, I go here, if they will, I go there.

I'm still looking over the FP routines to see if they're what I need or if I get to reinvent the wheel again :)

Acetronics2
- 16th January 2007, 09:04
Hi, Skimask

You can also remember Every number can be close-rounded as the division of two integers ... ( PI = 22/7 i.e. )

That give some brain work to find the couple of the year ... but a small GWBASIC ( LOL !!! ) program can easily sort them.

With the use of the DIV32 function ... that enables more than you ever dreamed !!!

Alain

PS: About Basics that include "High level" Functions : remember you do not have to twist your brain ... but must have a lot of Program memory; say, JUST for really "basic" GLCD functions ... no less than 2K !!!
No secret nor mystery !!!