Using Floating Point with PicBasic Pro Compiler

The PicBasic Pro Compiler has several built-in data types: bits, bytes and
words, along with arrays of each.  All of these types are unsigned integers.
This means there is no decimal point so real or floating point numbers cannot
be represented. There are several workarounds for this, including multiplying
each value by 10 or 100 for calculations and dividing back when it is time to
display the value.

Microchip has developed several different floating point routines in
assembler.  These routines can be integrated into a PicBasic Pro program and
called, if floating point is a necessity.  While it is not that difficult, it
is not obvious how to make this work properly.  The necessary PicBasic Pro
source code to integrate either the 24-bit or 32-bit Microchip floating point
routines into your program is contained in the following files:

FP0C.BAS            24-bit floating point for 16F84(A)
FP0C32.BAS          32-bit floating point for 16F84(A)
FP20.BAS            24-bit floating point for most other 14-bit core devices
FP2032.BAS          32-bit floating point for most other 14-bit core devices
FP17.BAS            24-bit floating point for 17Cxxx
FP1732.BAS          32-bit floating point for 17Cxxx
FP18.BAS            24-bit floating point for 18Xxxx
FP1832.BAS          32-bit floating point for 18Xxxx

It is important to note that the 32-bit routines won't convert signed values,
and that the 24-bit routines always convert signed values.  Therefore, the
32-bit routines will convert values from 0 to 65535, while the 24-bit
routines will convert values from -32767 to +32767.

In addition, the appropriate assembler routines must be included.  The
following files are modified versions of Microchip's AN575 files.
Microchip's files will work, but they will not compile using PM as the
assembler, and they don't include many of the newer PICmicro devices.
FP24.A18, FP32.A18, and MATH18.INC were not part of AN575. They were created
to support the PIC18Xxxx devices.  The files are:

DEV_FAM.INC
FP24.A16
FP24.A17
FP24.A18
FP32.A16
FP32.A17
FP32.A18
MATH16.INC
MATH17.INC
MATH18.INC

All of these files should be copied into your project directory. 


PicBasic Pro programs including the floating point routines can be compiled
using the -ampasmwin switch to invoke Microchip's assembler after compilation.

        PBPW -ampasmwin fp

PM may be used with the 14-bit core floating point files (.A16).  To use PM,
you must include an extra DEFINE statement in your code.  The name of this
DEFINE should be the target MCU part number preceded by 2 underscores. Set the
variable to a value of 1. For example, if you are compiling for the 16F877,
add the following:

     Define  __16F877  1

The floating point routines are accessed in PicBasic Pro by setting up
specific integer variables (aint and bint) and performing a GOSUB to a
floating point routine.  The first routine should convert the integer value
(aint) to a floating point value.  Generally, floating point operations occur
between 2 numbers, so a second integer (bint) should also be converted.  Next
a GOSUB to the required floating point operation, multiply for example, is
performed.  Finally, the floating point number is converted back into an
integer (aint) so that PicBasic Pro can use it again.  These GOSUB routines
are created in an additional PicBasic Pro file that must be INCLUDEd at the
beginning of the program.

The names of the subroutines are;

itofa          Convert integer aint to floating point aarg
itofb          Convert integer bint to floating point barg
fpadd          Perform floating point addition: aarg + barg
fpsub          Perform floating point subtraction: aarg - barg
fpmul          Perform floating point multiplication: aarg * barg
fpdiv          Perform floating point division: aarg / barg
ftoia          Convert floating point aarg to integer aint


The following devices are currently supported by these routines.

12C671, 12C672, 12CE673, 12CE674, 12F508, 12F509, 12F629, 12F675, 12F683

14000

16C432, 16C433

16C554, 16C557, 16C558, 16C61, 16C62(AB), 16C620(A), 16C621(A), 16C622(A),
16C63(A), 16C64(A), 16C642, 16C65(AB), 16C66, 16C662, 16C67, 16C71, 16C710,
16C711, 16C712, 16C715, 16C716, 16C717, 16C72(A), 16C73(AB), 16C74(AB),
16C745, 16C76, 16C765, 16C77, 16C770, 16C771, 16C773, 16C774, 16C781, 16C782,
16C84, 16C923, 16C924, 16C925, 16C926, 16CE623, 16CE624, 16CE625, 16F505,
16F54, 16F57, 16F627(A), 16F628(A), 16F630, 16F648A, 16F676, 16F684, 16F688,
16F716, 16F72, 16F73, 16F737, 16F74, 16F747, 16F76, 16F767, 16F77, 16F777,
16F818, 16F819, 16F83, 16F84(A), 16F87, 16F870, 16F871, 16F872, 16F873(A),
16F874(A), 16F876(A), 16F877(A), 16F88

17C42A, 17C43, 17C44, 17C752, 17C756(A), 17C762, 17C766

18C242, 18C252, 18C442, 18C452, 18C601, 18C658, 18C801, 18C858, 18F1220,
18F1320, 18F2220, 18F2320, 18F2331, 18F242, 18F2431, 18F2439, 18F248, 18F2515,
18F252, 18F2525, 18F2539, 18F258, 18F2585, 18F2610, 18F2620, 18F2680, 18F4220,
18F4320, 18F4331, 18F442, 18F4431, 18F4439, 18F448, 18F4515, 18F452, 18F4525,
18F4539, 18F458, 18F4585, 18F4610, 18F4620, 18F4680, 18F6410, 18F6490,
18F6520, 18F6525, 18F6585, 18F6620, 18F6621, 18F6680, 18F6720, 18F8410,
18F8490, 18F8520, 18F8525, 18F8585, 18F8620, 18F8621, 18F8680, 18F8720


The following are sample programs that use the floating point routines

4FUNCTN.BAS

This program demonstrates the four basic arithmetic functions (+,-,*, and /).  To
display the results, the fpdisplayr (rounded) and fpdisplay (unrounded)
subroutines are used.  The number of decimal places displayed may be specified
with the fpplaces byte variable. Negative numbers are displayed with the sign
indicator.  The display is limited to an absolute value of 32767.  Since this
program is written to accommodate negative values, the 32-bit floating point
routines cannot be used.

4FUNC32.BAS

This program is the same as 4FUNCTN.BAS, except that it is written for the
32-bit floating point routines.  It does not display negative values, and is
limited to an positive value of 65535.

AC-CD.BAS

This program illustrates the technique of working with numbers in floating
point format without converting them to integers.  Conversions are performed
on the inputs and the final results, but not on the intermediate steps of the
calculation.  This allows you to make calculations where some intermediate
steps result in values far beyond the 16-bit integer limit. The end result is
still subject to this limit.

The program can use either 24 or 32-bit floating point routines.  Keep in mind
that the 32-bit routines don't work with negative values, and that the 24-bit
routines always use signed values.

FP.BAS

This is a simple example of multiplication and division.  It is written for
the 16F84.


microEngineering Labs, Inc.
Box 60039
Colorado Springs CO 80960
(719) 520-5323
(719) 520-1867 fax
http://www.melabs.com
email:support@melabs.com

