Files
SyncHome/trunk/workspace/gcb/Interrupt Counter to Hardware Serial Terminal - 16F886.gcb
2023-03-09 12:51:54 +00:00

185 lines
5.0 KiB
Plaintext

' ########### start ###########
' pic:PIC16F886
' ########### end ###########
; FILE: Interrupt Counter to Hardware Serial Terminal - 16F886.gcb
; DATE: 31.01.2015
; VERSION: 1.0a
; AUTHOR: Evan R. Venn
;
; Description.
' A demonstration program for GCGB and GCB.
' This program demonsates Interrupt upon the button press on portb.0
' Each microprocessor will have different commands to setup the Interrupt. Refer to datasheet.
' When each event occurs a counter is incremented.
' Afer each event the counter is shown on the terminal.
' The demonstration also show the four states a button can be in UP, PRESSED, DOWN and RELEASED.
' Enjoy
; This file is part of the Great Cow Basic compiler.
;
; This demonstration code is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
#chip 16F886,16
; ----- Define Hardware settings
'Config hardware UART
#define USART_BLOCKING
#define USART_BAUD_RATE 4800
'Required for switch_event method
Dir portb.1 out
Dir SwitchIn1 In
; ----- Constants
'Required for switch_event method
#Define BUTTON_UP 0
#Define BUTTON_PRESSED 1
#Define BUTTON_DOWN 2
#Define BUTTON_RELEASED 3
#Define BUTTON_UNKNOWN 4
'Required for switch_event method
#define SwitchIn1 PORTB.0
#define check_switch PORTB.0
#define state_switch ON
; ----- Variables
Dim ButtonPressCount as Word
ButtonPressCount = 0
'Required for switch_event method
Dim btn_pv
; ----- Main body of program commences here.
'INTEDG: Interrupt Edge Select bit
'1 = Interrupt on rising edge of INT pin
'0 = Interrupt on falling edge of INT pin
INTEDG = 0
'Enable portb.0 as the source of the interrupt. See the datasheet for more information.
IOCB0 = 1
On Interrupt PORTBChange Call ButtonPressed
'track button presses - required to manage serial display only
old_state = 0
do forever
'determine the state of the switch
current_state = switch_event
'if the current state is not the same as the saved state then print current state
if current_state <> old_state then
HSerPrint current_state
'save state
old_state = current_state
end if
'if the current state is Released then show the count.
'count is incremented in the Interrupt routine.
if current_state = BUTTON_RELEASED then
HSerPrint "("+str( ButtonPressCount )
HSerPrint ")"
end if
'print only if the button is Pressed. Formatting display only.
if current_state = BUTTON_PRESSED then
HSerPrint ":"
end if
loop
end
; ----- Support methods. Subroutines and Functions
sub ButtonPressed
ButtonPressCount++
end sub
'/****************************************************************************
' Function:
' input_event(void)
'
' 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:
' None
'
' Parameters:
' None
'
' Returns:
' event_t 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 SwitchIn1 PORTD.2
' Dir SwitchIn1 In
' #define check_switch RD2
' #define state_switch OFF
'
' ***************************************************************************/
function switch_event()
dim ret As byte
Dim btn as Byte
btn = check_switch = state_switch
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