Sei sulla pagina 1di 3

Using Floating Point with PICBASIC PRO Compiler The PICBASIC PRO Compiler has several built-in data

types: bits, bytes, words and longs, along with arrays of each. All of these types are 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 written in assembler. These routines can be integrated into a PICBASIC PRO program and called, if floating point is a necessity. While it is not difficult, it is not immediately 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 d) FP0C32.BAS ned) FP20.BAS d) FP2032.BAS ned) FP40.BAS d) FP4032.BAS ned) FP17.BAS FP1732.BAS FP18.BAS FP1832.BAS FP1832L.BAS 24-bit floating point for 14-bit core with RAM at $0c (signe 32-bit floating point for 14-bit core with RAM at $0c (unsig 24-bit floating point for 14-bit core with RAM at $20 (signe 32-bit floating point for 14-bit core with RAM at $20 (unsig 24-bit floating point for 14-bit core with RAM at $40 (signe 32-bit floating point for 14-bit core with RAM at $40 (unsig 24-bit 32-bit 24-bit 32-bit 32-bit floating floating floating floating floating point point point point point for for for for for PIC17Cxxx (signed) PIC17Cxxx (unsigned) PIC18Xxxx (signed) PIC18Xxxx (unsigned) long PIC18Xxxx (signed)

It is important to note that the 32-bit routines, with the exception of the PIC18Xxxx long routines, won't convert signed values, and that the 24-bit routines always convert signed values. Therefore, the non-long 32-bit routines will convert values from 0 to 65535, while the 24-bit routines will convert values from -32767 to +32767. The long 32-bit routines will convert values from -2147483648 to +2147483648. 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 PIC MCU 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 itofb fpadd fpsub fpmul fpdiv ftoia Convert Convert Perform Perform Perform Perform Convert integer aint to floating point aarg integer bint to floating point barg floating point addition: aarg + barg floating point subtraction: aarg - barg floating point multiplication: aarg * barg floating point division: aarg / barg floating point aarg to integer aint

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. AB-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 PIC16F84. microEngineering Labs, Inc. Box 60039 Colorado Springs CO 80960 (719) 520-5323 (719) 520-1867 fax http://www.melabs.com email:support@melabs.com

Potrebbero piacerti anche