To declare a variable you use the dim statement as follows
Dim variablename As type
Variables declared within a procedure will only exist within that procedure. These are known as local variables. When a local variable is not in existence, its memory space will be used by other local variables in other procedures. Therefore the contents of a local variable can change from one call to a procedure to the next. This also allows you to use the same variable names in different procedures without worrying about naming conflicts.
Variables declared outside of a procedure are known as global variables and will exist throughout program execution. If however a local variable has the same name as a global variable, the local variable will be used instead of the global one.
The type clause in the Dim statement will allow you to define the type of data the variable can contain. Supported variable types include Byte, uByte, Integer, uInteger, Long, uLong, Fixed, Single, and String.
The following restrictions are placed on variable names:
Must be alpha-numeric.
Must start with an alphabetic character (an underscore is allowed if inside a library).
The following variable types are supported.
|
1 Bit Flag, either 1 or 0 |
|
|
8 Bit Integer, -128 to 127 |
|
|
8 Bit Integer, 0 to 255 |
|
|
16 Bit Integer, -32768 to 32767 |
|
|
16 Bit Integer, 0 to 65535 |
|
|
24 Bit Integer, -8388608 to 8388607 |
|
|
24 Bit Integer, 0 to 16777215 |
|
|
32 Bit Integer, -2147483648 to 2147483647 |
|
|
32 Bit Integer, 0 to 4294967295 |
|
|
Fixed |
32 Bit Real, 16 Bit Integer and 16 Bit Fractional 0 to 65535.9999847412109375 resolution of 0.0000152587890625 |
|
32 Bit Floating Point |
|
|
16 Bit String pointer |
Constants are define using the "Const" keyword as follows
Const constantname = value
Example:
Const maxValue = 10
Arrays are declared similarly to other explicitly defined variables using the DIM statement.
DIM arrayname(size) As type
The first element of an array is always 0, and the last element is always size-1.
The following example will define a uByte array x as having 15 elements (0 to 14), and initialize each element to 0
Sub test()
Dim x(15) as uByte
Dim j as uByte
For j=0 To 14
x(j)=0
Next j
End Sub
Arrays have the following limitations
An array cannot take up more than 256 bytes (1 bank)
String and Boolean types cannot be used in arrays
Strings are variables that point to blocks of character data. Defining a string is the same as defining an array.
Dim stringName(size) As String
All strings are terminated with a 0 character, therefore the size must be at least one greater than the number of characters you wish to hold. Most string operations are supported by built-in library calls, however there is native support for constant assignments.
Assigning a constant to a string:
Dim s(15) As String '
Create a 15 character String
s="Test String"
Strings are also different when being passed to a function or procedure in that they are passed by reference. Therefore no size information is needed in the parameter list.
Sub main()
Dim s(15) As String
s="Test"
call str2USART(s) ' A string must be loaded into a variable
' before it can be passed as a parameter
End Sub
' Send a string out the USART
Sub str2USART(s As
String)
FSR0=Addr(s)
While INDF0<>0
While TXIF=0: ' Wait on the Transmit buffer to empty
End While
TXREG=POSTINC0 ' Transmit this character and bump the pointer
' to the next character
Wend
End Sub
Single variables have been implemented using AN575 modified for the PIC18C series. Before using Single variables the file fp32.bas must be included. For advanced floating point operations fp32ext.bas must be included. See the floating point library reference.
The exception flags and option flags are defined as follows:
|
IOV |
Integer overflow flag |
|
SAT |
Saturate enable flag |
|
FOV |
Floating point overflow flag |
|
FUN |
Floating point underflow flag |
|
FDZ |
Floating point Divide by Zero flag |
|
RND |
Rounding enable bit |
|
DOM |
Domain error |
Example:
Option Explicit
' Turn off implicit variable declaration
Include <18c452.bas> ' include the processor definition file
Include <fp32.bas> ' include floating point variables and libraries
Sub main()
Dim a,b As Single ' Define variables
a=PORTB ' assign the value of PORTB to a
b=PORTD ' assign the value of PORTD to b
'
(type conversion is automatic)
RND=0 ' Disable rounding
SAT=0 ' Disable saturation
a=a/b ' floating point divide
If FDZ=1 Then
' Divide by Zero Error
End If
End Sub