There have been several modifications to standard Basic in order to make programming the PIC easier.
A variable can be assigned to a specific address using the at symbol (@) when defining the variable in the DIM statement. The address can either be symbolic or literal:
Dim v1 @ PORTB as
uByte ' v1 is now at PORTB address
Dim v2 @ 0x002 as uInteger ' v2 now starts as address 2
Absolute Bit assignment is similar to Variable address assignment except the address is followed by a period (.) and a bit number, and the variable type is always Boolean:
Dim RB0 @ PORTB.0 as
Boolean
Dim userFLG @ 0x002.2 as Boolean
To access a bit of any variable simply follow the variable name with a period (.) and a bit number. This works for multi byte variables as well.
PORTB.0=1 ' set bit
0 of portb (RB0 pin)
If PORTD.3=0 Then
call go()
End If
Dim a as uInteger
a.13=1 ' set bit 13 of uInteger a
Four variable extensions have been added for individual byte access of multi byte variables. Those are Byte0, Byte1, Byte2, and Byte3. They are used as follows:
Dim a as uInteger
Dim b as uLong
a.Byte0=0
a.Byte1=b.Byte2+b.Byte3
Two variable extensions have been added for word access to 4 byte variables:
Word0
Word1
Basic is great, however there are limitations to high level languages. Sometimes things are easier to do in assembly. That is why PIC assembly is fully integrated into Basic18. The compiler will handle conversion of labels, variable names, access flags, and bank registers automatically. Here are some examples:
Function sum(v1 as
uByte, v2 as uByte)
Dim a as uInteger
clrf a ' clear a (LSB first)
clrf a+1
movf v1,W ' WREG=v1
addwf v2,W ' WREG=v1+v2
movwf a ' Store LSB
clrf WREG ' WREG=0
addwfc a+1,F ' add in a possible carry bit
End Function a ' return a to the calling routine
Optionally, a single line of assembly can be sent directly to the output assembly file without being modified. This is accomplished by appending an "@" character to the beginning of the line IE:
@ org 0x1000
Both high and low priority interrupts are supported through special Sub Procedures. Context saving is also handled automatically for WREG, STATUS, and BSR. If the interrupt is going to modify FSR0 or FSR1 it is strongly suggested you save them as well. One of the following keywords should be added to the end of a Sub definition to make it an interrupt service routine:
intHigh
intLow
Example:
' Define the high
priority ISR
Sub HighPriorityISR() intHigh
End Sub
' Define the low
priority ISR
Sub LowPriorityISR() intLow
End Sub