IBD Limited Şirketi - İzmir / Türkiye
379/1 Sokak A-Blok No: 2/101 - AFA Sanayi Çarşısı - II. Sanayi - 35100 Bornova / İzmir
Telefon : 90-232-462 7477-78 Telefax : 90-232-462 7545 - e-mail : [email protected]

ibdline

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. In addition, the appropriate floating point routines must be downloaded from Microchip's web site (AN575). 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.

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. Below is an example program that demonstrates these steps.


' 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)



        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



Home | PicBasic Pro Compiler | PicBasic Compiler | EPIC Plus Programmer | PICProto Boards | Bundles | Books | Ordering Information


IBD Ltd. Şti.
Back to the IBD Ltd. Şti. home page.
Please send your comments about our website to the Dipl.-Ing. Faruk DERİ (VDI)