A code example that shows the four states of a single switch. This is very useful.
It can be used when you want to complete an action upon the change of state - you can now create loop that checks the state of a switch and complete actions upon any of the four states... not just on or off.
You can call the function within a loop or from interrupts.
This was ported from the Microchip F1 Evaluation Code.
#define LedPort PORTc
DIR PORTC OUT
#define DS1 LATC0
#define DS2 LATC1
#define DS3 LATC2
#define DS4 LATC3
' Define the switch port
'address of switch - required by method. the title 'check_switch' is required
#define check_switch RA3
dir Porta.3 in
'initial state of the switch - required by method, the title ' state_switch' is required
#define state_switch Off
btn_event = BUTTON_UP
btn_pv = BUTTON_UP
do while ( true )
btn_event = switch_event()
select Case btn_event
Case BUTTON_UP
' add your code here...
SET DS1 on:SET DS2 off: SET DS3 off: SET DS4 Off: wait 250 ms
Case BUTTON_DOWN
' add your code here...
SET DS1 off: SET DS2 on: SET DS3 off: SET DS4 Off: wait 250 ms
CASE BUTTON_RELEASED
' add your code here...
SET DS1 off: SET DS2 off: SET DS3 on: SET DS4 Off: wait 250 ms
CASE BUTTON_PRESSED
' add your code here...
SET DS1 Off: SET DS2 Off: SET DS3 off: SET DS4 On: wait 250 ms
end select
' version 1.01
'************
' Function:
' switch_event
'
' Summary:
' Processes the single button into the states UP, DOWN, PRESSED & RELEASED.
'
' Description:
' This function helps write user interface state machines by determining when
' the button was pressed, released
'
' Precondition:
' Starting state. As define by variables
' btn_event = BUTTON_UP ... current event
' btn_pv = BUTTON_UP .... previous event
'
' Parameters:
' None
'
' Returns:
' the value of the current button events.
' Valid responses are BUTTON_UP, BUTTON_DOWN, BUTTON_PRESSED, BUTTON_RELEASED
'
' Remarks:
' state_switch inverts the port. If high then use state_switch=off
' Define the switch port
' address of switch - required by method. the title 'check_switch' is required
' #define check_switch RA3
' dir Porta.3 in
' initial state of the switch - required by method, the title ' state_switch' is required
' #define state_switch On
'
' ***********/
#ifndef check_switch
Warning: check_switch needs to be define, check_switch is a port alias like RA3
#endif
#ifndef state_switch
Warning: state_switch needs to be define, state_switch is a state like ON or OFF
#endif
function switch_event()
dim ret As byte
Dim btn as Byte
Dim ifstate as BIT
if check_switch = state_switch then
btn = 1
else
btn = 0
end if
if !btn & !btn_pv then
' button is not pressed now nor was it pressed previously
ret = BUTTON_UP
END if
if btn & !btn_pv then
' button is pressed now but it wasn't previously
ret = BUTTON_PRESSED
End if
if btn & btn_pv then
' button was pressed previously and is still pressed
ret = BUTTON_DOWN
end if
if !btn & btn_pv then
' button is not pressed now but it was previously
ret = BUTTON_RELEASED
End If
btn_pv = btn
switch_event = ret
End Function
' Debounce button, Debounce switch
Function input_switch ( )
dim ButtonCount as byte
input_switch = false
If check_switch = state_switch Then
ButtonCount = 0
Do While check_switch = state_switch and ButtonCount < 4
wait 5 ms
ButtonCount += 1
Loop
end if
If ButtonCount > 3 then
input_switch = true
ButtonCount = 0
end if
End Function
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A code example that shows the four states of a single switch. This is very useful.
It can be used when you want to complete an action upon the change of state - you can now create loop that checks the state of a switch and complete actions upon any of the four states... not just on or off.
You can call the function within a loop or from interrupts.
This was ported from the Microchip F1 Evaluation Code.
Have fun, I will post a YouTube video soon.
Anobium
~~~~~~
#chip 16F1825, 32
#config Osc = INTOSC, MCLRE_OFF
#define LedPort PORTc
DIR PORTC OUT
#define DS1 LATC0
#define DS2 LATC1
#define DS3 LATC2
#define DS4 LATC3
' Define the switch port
'address of switch - required by method. the title 'check_switch' is required
#define check_switch RA3
dir Porta.3 in
'initial state of the switch - required by method, the title ' state_switch' is required
#define state_switch Off
btn_event = BUTTON_UP
btn_pv = BUTTON_UP
do while ( true )
Loop
end
#Define BUTTON_UP 0
#Define BUTTON_PRESSED 1
#Define BUTTON_DOWN 2
#Define BUTTON_RELEASED 3
#Define BUTTON_UNKNOWN 4
Dim btn_pv
' version 1.01
'************
' Function:
' switch_event
'
' Summary:
' Processes the single button into the states UP, DOWN, PRESSED & RELEASED.
'
' Description:
' This function helps write user interface state machines by determining when
' the button was pressed, released
'
' Precondition:
' Starting state. As define by variables
' btn_event = BUTTON_UP ... current event
' btn_pv = BUTTON_UP .... previous event
'
' Parameters:
' None
'
' Returns:
' the value of the current button events.
' Valid responses are BUTTON_UP, BUTTON_DOWN, BUTTON_PRESSED, BUTTON_RELEASED
'
' Remarks:
' state_switch inverts the port. If high then use state_switch=off
' Define the switch port
' address of switch - required by method. the title 'check_switch' is required
' #define check_switch RA3
' dir Porta.3 in
' initial state of the switch - required by method, the title ' state_switch' is required
' #define state_switch On
'
' ***********/
#ifndef check_switch
Warning: check_switch needs to be define, check_switch is a port alias like RA3
#endif
#ifndef state_switch
Warning: state_switch needs to be define, state_switch is a state like ON or OFF
#endif
function switch_event()
dim ret As byte
Dim btn as Byte
Dim ifstate as BIT
if check_switch = state_switch then
btn = 1
else
btn = 0
end if
End Function
' Debounce button, Debounce switch
Function input_switch ( )
dim ButtonCount as byte
input_switch = false
If check_switch = state_switch Then
ButtonCount = 0
Do While check_switch = state_switch and ButtonCount < 4
wait 5 ms
ButtonCount += 1
Loop
end if
If ButtonCount > 3 then
input_switch = true
ButtonCount = 0
end if
End Function