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.
------------------------------------------------------------------------------------
example:
' 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