Tel: +44 7814 044 754 -  Fax: +44 8715 283 948 -  Skype: warburtech.co.uk

Add to Favourites

PicBasic Pro Floating Point Maths

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. If you don't have their assembler, it also may be downloaded from their web site.

        PBP -p16f84 -ampasm fp

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 for 'F84 (24-bit)
'   include "fp20.bas"      ' Include for other PIC micros (24-bit)
'   include "fp0c32.bas"    ' Include for 'F84 (32-bit)
'   include "fp2032.bas"    ' Include for other PIC micros (32-bit)
'   include "fp17.bas"      ' Include for 17Cxxx (24-bit)
'   include "fp1732.bas"    ' Include for 17Cxxx (32-bit)
'   include "fp18.bas"      ' Include for 18Cxxx (24-bit)
'   include "fp1832.bas"    ' Include for 18Cxxx (32-bit)

    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

Page Updated:
05/08/10

Tel: +44 7814 044 754 - Fax: +44 8715 283 948 - Skype: warburtech.co.uk

Warburton Technology, 33 Selkirk Close, Wimborne, Dorset, BH21 1TN, United Kingdom

VAT Number: GB 846 917 877