<<Previous

Next>>

 BRANCH

BRANCH Index,[Label{,Label...}]

BRANCH causes the program to jump to a different location based on a variable index. This is similar to On..Goto in other BASICs.

Index selects one of a list of labels. Execution resumes at the indexed label. For example, if Index is zero, the program jumps to the first label specified in the list, if Index is one, the program jumps to the second label, and so on. If Index is greater than or equal to the number of labels, no action is taken and execution continues with the statement following the BRANCH. Up to 255 (256 for 18Cxxx) Labels may be used in a BRANCH.

For 12- and 14-bit core and 17Cxxx devices, Label must be in the same code page as the BRANCH instruction. If you cannot be sure they will be in the same code page, use BRANCHL below.

For 18Cxxx devices, the Label must be within 1K of the BRANCH instruction as it uses a relative jump. If the Label is out of this range, use BRANCHL below.

	BRANCH B4,[dog,cat,fish]
	' Same as:
	'  If B4=0 Then dog (goto dog)
	'  If B4=1 Then cat (goto cat)
	'  If B4=2 Then fish (goto fish)

 BRANCHL

BRANCHL Index,[Label{,Label...}]

BRANCHL (BRANCH long) works very similarly to BRANCH in that it causes the program to jump to a different location based on a variable index. The main differences are that it can jump to a Label that is in a different code page than the BRANCHL instruction (or further away than 1K for 18Cxxx devices) and that it generates code that is about twice the size as code generated by the BRANCH instruction. If you are sure the labels are in the same page as the BRANCH instruction or if the microcontroller does not have more than one code page, using BRANCH instead of BRANCHL will minimize memory usage.

Index selects one of a list of labels. Execution resumes at the indexed label. For example, if Index is zero, the program jumps to the first label specified in the list, if Index is one, the program jumps to the second label, and so on. If Index is greater than or equal to the number of labels, no action is taken and execution continues with the statement following the BRANCHL. Up to 127 (256 for 18Cxxx) Labels may be used in a BRANCHL.

	BRANCHL B4,[dog,cat,fish]
	' Same as:
	'  If B4=0 Then dog (goto dog)
	'  If B4=1 Then cat (goto cat)
	'  If B4=2 Then fish (goto fish)

 BUTTON

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

Read Pin and optionally performs debounce and auto-repeat. Pin is automatically made an input. Pin may be a constant, 0-15, or a variable that contains a number 0-15 (e.g. B0) or a pin name (e.g. PORTA.0).

Down     State of pin when button is pressed (0..1).
Delay

Cycle count before auto-repeat starts (0..255). If 0, no debounce or auto-repeat is performed. If 255, debounce, but no auto-repeat, is performed.

Rate

Auto-repeat rate (0..255).

BVar

Byte-sized variable used internally for delay/repeat countdown. It must be initialized to 0 prior to use and not used elsewhere in the program.

Action

State of button to act on (0 if not pressed, 1 if pressed).

Label 

Execution resumes at this label if Action is true.

	' Goto notpressed if button not pressed on Pin2
	BUTTON PORTB.2,0,100,10,B2,0,notpressed

BUTTON needs to be used within a loop for auto-repeat to work properly.

BUTTON accomplishes debounce by delaying program execution for a period of milliseconds to wait for the contacts to settle down. The default debounce delay is 10ms. To change the debounce to another value, use DEFINE:

> Set button debounce delay to 50ms

DEFINE BUTTON_PAUSE 50

Be sure that BUTTON_PAUSE is all in upper case.

In general, it is easier to simply read the state of the pin in an IF..THEN than to use the BUTTON command as follows:

If PORTB.2 = 1 Then notpressed

Example program:
INCLUDE "modedefs.bas" 	' Include serial modes
SO 	Con 	0 	' Define serial output pin
Bpin 	Con 	2 	' Define Button input pin
B0 	Var 	Byte

	B0 = 0 		' Zero Button working buffer

loop: BUTTON Bpin,1,10,5,B0,0,notp 	' Check button
					(skip if not
					pressed)
	Serout SO,N2400,["Press",13,10] ' Indicate button
					pressed
notp: Serout SO,N2400,[#B0,13,10] 	' Show working variable
	
	Pause 100 	' Wait a little
	Goto loop 	' Do it forever  
<<Previous

Next>>