Skip to main content

PicBasic program flow control commands

Program flow control commands are important in every programming language since they enable the programmer to make a decision and change the flow of the program based on this decision. PicBasic language supports the following program flow control commands:

BRANCH BUTTON CALL FOR…NEXT
GOSUB…RETURN GOTO
IF…THEN



We shall now see what the functions of these commands are and how to use them in programs.


BRANCH

BRANCH offset, (Label0, Label1,)


When this command is executed, the program will jump to the program label based on the value of offset. Offset is actually a program value and if offset is zero, the program jumps to the first label, if offset is one, the program jumps to the second label, and so on.


Example:


BRANCH B2, (Lbl1, Lbl2, Lbl3)      ‘ If B2     0 then goto Lbl1
‘ If B2     1 then goto Lbl2
‘ If B2     2 then goto Lbl3


BUTTON

BUTTON Pin, Down, Delay, Rate, Var, Action, Label


This command is used to check the status of a switch. The command operates in a loop and con- tinuously samples the pin, debouncing it and comparing the number of iteration performed with the switch closed. The parameters are


Pin           Pin number (0 to 7). PORTB pins only
Down       State of pin when button is pressed (0 or 1)
Delay        Delay before auto-repeat begins (0 to 255). If 0, no debounce or auto-repeat is performed. If 255, only debounce, but no auto-repeat is performed
Rate          Auto-repeat rate (0 to 255)
Var           Byte variable used for delay/repeat countdown. Should be initialised to 0 before use
Action      State of pin to perform goto (0 if not pressed, 1 if pressed)
Label        Program execution continues at this label if Action is true


Figure 4.1 shows the two types of switches that can be used with this command.


For example, the following command checks for a switch pressed on pin 2 (of PORTB) and jumps to Loop if it is not pressed (this command assumes that the port pin will be logic 0 when the switch is pressed, i.e. the figure on the left in Figure 4.1):


BUTTON 2, 0, 255, 0, B0, 0, Loop




Switches that can be used for the Button command


The following command checks for a switch pressed on pin 2 as above, but jumps to Loop if the switch is pressed:

BUTTON 2, 0, 255, 0, B0, 1, Loop



CALL



CALL Label



This command executes the assembly language subroutine named Label. For example, the com- mand calls to assembly language routine with the name calculate.

CALL calculate

FOR…NEXT

FOR index     Start TO End (STEP (   ) Inc)
(body)
NEXT index

This command is used to perform iterations in a program. Index is a program variable which holds the initial value of the iteration count Start. End is the final value of the iteration count. STEP is the value by which the index is incremented at each iteration. If no STEP is specified, the index is incremented by 1. The iteration repeats until index     End and then execution continues with the next instruction following the NEXT. Index can be a byte (0 to 255), or a word (0 to 65535).

In the following example, the two statements enclosed within the FOR…NEXT are executed 10 times.

FOR    B0     1 TO 10
B1     B1     1
B2     B2     1
NEXT B0

or in the following example, the index is incremented by 2 in each iteration.

FOR B0     1 TO 100 STEP 2
B1     B0     2
NEXT B0

GOSUB…RETURN

GOSUB Label

This program calls a subroutine starting at Label. It is like a GOTO command, but here the program returns when the RETURN statement is reached, and continues with the instruction after the GOSUB. The RETURN statement has no parameters. A subroutine has the following characteristics:

    A label to identify the starting point of the subroutine
    Body of the subroutine where the required operation is performed
    RETURN statement to exit the subroutine and return to the main calling program

Subroutines can be nested in PicBasic where a subroutine can call to other subroutines. The nest- ing should be restricted to no more than four levels deep. In the following example, the subrou- tine labelled INC increments variable B1 by one and then returns to the main program. On return to the main program, the statement B2     B1 is executed.

B0     0
B1     1
GOSUB INC      ‘ Jump to subroutine INC B2     B1             ‘ Subroutine returns here
………
………
INC:                                   ‘ Start of the subroutine B1     B1     1      ‘ Body of the subroutine RETURN           ‘ End of the subroutine



GOTO



GOTO Label



This command causes the program execution to jump to the statement beginning at Label. For example,

GOTO Loop
………
………
………

Loop:

IF…THEN

IF Comp (AND / OR Comp) THEN Label
 


This statement is used to perform comparisons (Comp) in a program. If the result of the compari- son is true then the program jumps to the statement at Label, otherwise execution resumes with the statement following IF…THEN.

A comparison can relate to a variable, to a constant, or to other variables. All comparisons are unsigned and the following comparison operators can be used:

           less than
         less than or equal
           equal
         not equal
         greater than or equal
           greater than
Additionally, logical operators AND and OR can be used in a comparison operation. For example, IF B0     10 THEN CALC      ‘ Jump to CALC if B0     10
…………………..
.............................. CALC:
Another example is given below. In this example, if B2 is greater than 40 and at the same time B3 is less than 20 then the program jumps to the statement at label EXT. Otherwise, execution con- tinues with the statement after the IF…THEN.

IF B2     40 AND B3     20 THEN EXT
………..........
……….......... EXT:
It is important to be careful that only a Label can be used after the THEN statement.



Comments

Popular posts from this blog

ANALOGUE to DIGITAL (A/D) Converter in PIC16F87X Series

ANALOGUE to DIGITAL (A/D) Converter What is a Sample & Sample rate? A "sample" is a single measurement of amplitude. Sample rate is simply the number of samples (or measurements of amplitude) taken per second. Sampling Intervals Quantization The samples are assigned a binary number approximating their sampled value. Quantizing divides up the sampled voltage range into 2n-1 quantizing intervals, where “n” is the number of bits per sample (the sampling resolution). For example, an 8-bit system can identify 28 (256) discrete sampled signal values (255 quantizing intervals). The amplitude of such a signal can occupy the entire quantizing range. A/D Reference Voltages Successive Approximation A/D Example 1 Example 2  Example 3 A/D Converter Modules in PIC16F87X series 28-pin devices has 5 modules. (AN0-AN4)         PIC16F873        PIC16F876 4...

Other PicBasic commands 01

W e shall n o w brief l y look at the remaining PicBasic commands in alphabetical order w hich are useful during the pr o g ram d e v elopment. More details about these commands can be obtained from the PicBasic manual. EEP R OM EEP R OM Location, (constant, constant,….., constant) Thi s comman d store s constant s i n consecut i v e b yte s i n on-chi p EEP R O M memo r y . Th e command on l y w ork s wit h th e PI C microcontroller s tha t h a v e EEP R OM , suc h a s th e PIC16F84 , PIC16F877, etc . Locatio n i s optional , an d i f omitte d th e f irs t EEP R O M locatio n i s assumed . Constant s ca n be numeri c constant s o r strin g constants . String s ar e store d a s consecut i v e b yte s o f ASCI I v alues . An e xampl e i s g i v e n bel o w . EEP R OM 3, (5, 2, 8)        ‘ Store 5 in location 3, ‘ 2 in location 4, and 8 in ‘ location 5 END END St...

Voltmeter

This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt. 1.Initialization: Setting up ports, configuring ADC, and Timer0. 2.Infinite Loop: Continuously reads analog input and updates the display. 3.ADC Conversion: Reads analog voltage from a sensor. 4.Voltage Calculation: Converts ADC value to voltage. 5.Digit Extraction: Extracts individual digits from the temperature/voltage value. 6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays. --------------------------------------------- #include <pic.h> // Include the header file for PIC microcontroller family. unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display. unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary sto...