LOOKUP function can be used to select one from the list of Byte constants, based on the value in the index Byte variable, that is supplied as the last separated argument of the function. The first constant in the list has index value 0. The selected constant will be loaded into the result Byte data type variable. If the value in the index variable goes beyond the number of constants in the list, the result variable will not be affected by the function. Here is one small example for a 7-segment LED display:
DIM DIGIT AS BYTE
DIM MASK AS BYTE
loop:
TRISB = %00000000
FOR DIGIT = 0 TO 9
MASK = LOOKUP(0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F), DIGIT
PORTB = MASK
WAITMS 1000
NEXT DIGIT
GOTO loop
If all constants in the list (or part of them) are ASCII values, then shorter form of the list can be created by using string arguments. For example:
MASK = LOOKUP("ABCDEFGHIJK"), INDEX
SHIFTLEFT and SHIFTRIGHT functions can be used to shift bit-level representation of a variable left and right. The first argument is input variable and the second argument is number of shifts to be performed. Here are two examples:
TRISB = 0x00
PORTB = %00000011
goleft:
WAITMS 250
PORTB = SHIFTLEFT(PORTB, 1)
IF PORTB = %11000000 THEN GOTO goright
GOTO goleft
goright:
WAITMS 250
PORTB = SHIFTRIGHT(PORTB, 1)
IF PORTB = %00000011 THEN GOTO goleft
GOTO goright
TRISB = 0x00
PORTB = %00000001
goleft:
WAITMS 250
PORTB = SHIFTLEFT(PORTB, 1)
IF PORTB.7 THEN GOTO goright
GOTO goleft
goright:
WAITMS 250
PORTB = SHIFTRIGHT(PORTB, 1)
IF PORTB.0 THEN GOTO goleft
GOTO goright
Structured programs can be written using subroutine calls with GOSUB statement that uses line label name as argument. Return from a subroutine is performed by RETURN statement. User need to take care that the program structure is consistent. When using subroutines, main routine need to be ended with END statement. END statement is compiled as an infinite loop. Here is an example:
SYMBOL ad_action = ADCON0.GO_DONE
SYMBOL display = PORTB
TRISB = %00000000
TRISA = %111111
ADCON0 = 0xC0
ADCON1 = 0
HIGH ADCON0.ADON
main:
GOSUB getadresult
display = ADRESH
GOTO main
END
getadresult:
HIGH ad_action
WHILE ad_action
WEND
RETURN