microEngineering Labs, Inc.
    719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

Using Microchip's Floating Point Routines with PICBASIC PRO™

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 and placed them on their web site. 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, FP.ZIP, may be downloaded. The appropriate floating point routines (Microchip's AN575) are included in this zip. The PICBASIC PRO programs must be compiled using the -ampasm switch to invoke Microchip's assembler.  (In MicroCode Studio, click View - Compile And Program Options, then check the box labeled "Use MPASM".)   You must have the MPASMWIN assembler installed on your harddrive.  It is part of the MPLAB development environment, which is available as a free download from Microchip.

The 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 readme files included in the zip explain the procedures in more detail. Also, several example programs are included. Below is one example program that demonstrates the use of the libraries.

' Floating point test program

       include "fp0c.bas"      ' Include file for 'F84 (24-bit)
'       include "fp20.bas"      ' Include file for most other PICmicros (24-bit)
'       include "fp0c32.bas"    ' Include file for 'F84 (32-bit)
'       include "fp2032.bas"    ' Include file for most other PICmicros (32-bit)
'       include "fp17.bas"      ' Include file for 17Cxxx (24-bit)
'       include "fp1732.bas"    ' Include file for 17Cxxx (32-bit)
'       include "fp18.bas"      ' Include file for 18xxxx (24-bit)
'       include "fp1832.bas"    ' Include file for 18xxxx (32-bit)
'       include "fp1832l.bas"   ' Include file for 18xxxx (32-bit, PBPL.EXE)

        Pause 500       ' Wait for LCD to start

' Demonstrate floating point multiply
        aint = 10
        Gosub itofa     ' Convert int to float

        bint = 20
        Gosub itofb     ' Convert int to float

        Gosub fpmul     ' FP multiply (10 * 20)

        Gosub ftoia     ' Convert float to int

        Lcdout $fe, 1, "* ", dec aint
        Pause 1000


' Demonstrate floating point divide
        Gosub itofa     ' Convert int back to float (200)

        bint = 3
        Gosub itofb     ' Convert int to float

        Gosub fpdiv     ' FP divide (200 / 3)

        Gosub ftoia     ' Convert float to int

        Lcdout $fe, 1, "/ ", dec aint


' Demonstrate floating point remainder
        aint = 200
        Gosub itofa     ' Convert int to float

        bint = 3
        Gosub itofb     ' Convert int to float

        Gosub fpdiv     ' FP divide (200 / 3)

        bint = 100
        Gosub itofb     ' Convert int to float

        Gosub fpmul     ' Multiply by 100 to move remainder up 2 places

        Gosub ftoia     ' Convert float to int

        aint = aint // 100      ' Get to the remainder
        Lcdout ".", dec2 aint

        End