View previous topic :: View next topic |
Author |
Message |
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Sun Jul 05, 2020 5:10 pm Post subject: Debounce |
|
|
No mention made in HELP but can DEBOUNCE be reconfigured during runtime or only at compilation? Specifically, change the amount of time during runtime?
(my suspicion is no)
(BASCOM-AVR version : 2.0.8.2 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Jul 05, 2020 5:24 pm Post subject: |
|
|
You can find in the Help topic about "Config" where is the table of RE-USABLE configs |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Sun Jul 05, 2020 5:33 pm Post subject: |
|
|
Thanks.
As I suspected... too bad...darn |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 585 Location: Cleveland, OH
|
Posted: Mon Jul 06, 2020 1:04 am Post subject: |
|
|
Using all of the "High Level" commands / instructions in Bascom makes coding projects easy, as one can focus on the big picture and not get dragged down worrying about nitty-gritty details.
That said, however, something like Debouncing Push Button Switches is easily done within one's own code, and then one has complete control over the time frame for considering a switch debounced, can vary it for different types of switches, can add auto-repeat flags, or add a piezo beep if it is newly pressed, etc.
Some code is very well written for fast speed, or to conserve memory.
The example below does neither!
It demonstrates debouncing two PB Switches in software.
When PB Sw #1 is pressed, and debounced, it turns on LED #2.
When PB Sw #2 is pressed, and debounced, it turns off LED #2.
Led #1 flashes at 1 Hz to show that the system is alive and running.
A timer/counter is set up to fire an interrupt at 1 KHz, (i.e. once per mSec).
That is used to check the status of the switches and set a debounce flag when
they are pressed and debounced.
One clears the switch's flags within the Main Loop, when an action is performed
for the switch having been pressed.
The Variable PBSwDBVal (Psu Button Switch Debounce Value) is the number of
mSec that the switch must be continuously low for, (without any further
bouncing), before setting the DeBounced Flag.
For a typical switch and project it might be 25 (25 mSec).
For demonstrating the program one can increase it to 255.
At that level a very short press won't be seen as pressed and debounced, and
the LED won't be turned on, (or off).
As the Debounce time duration is a variable, you can set it on the fly, during the program.
You could even have different timeframes for different buttons!
JC
Code: |
'File: Nano Bosch PCB Testbed V1.bas
'This program demonstrates a simple
'approach to Push Button Switch debouncing
'Doesn't implement autorepeat or piezo beep.
'PBSwDBVal = # of mSec of continuously pressed time
'without any bounce for the Debounce Flag to be set.
'Set = 25 for real projects, (typically)
'Set to 255 to see that fast pushes do get set as
'having been pressed.
'Heartbeat from ISR flashes LED 1 once / second.
'PB Sw1 Turns On LED#2
'PB Sw2 Turns Off LED#2
'
'Hardware:
'Arduino Nano:
'Mega328P 5V
'16 MHz External Xtal,
'Blue PCB name: Nano Testbed V1
'PB Switches are tied to Ground, and the
'internal pull-up resistors are enable to
'hold the pin high unless it is pressed.
'The pin is LOW when pressed.
'Port C.2 I PBSw 2
'Port C.3 I PBSw 1
'Port D.5 O LED 2
'Port D.6 O LED 1
'Port C.1 O LED 3
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
$regfile = "m328pdef.dat" 'Define the uC
$crystal = 16000000 '16 MHz
$hwstack = 40 'Hardware Stack
$swstack = 40 'Software Stack
$framesize = 40 'Frame Space
LED1 Alias PortD.6 'LED
LED2 Alias PortD.5 'LED
LED3 Alias PortC.1 'LED
PBSw1 Alias PinC.3 'PB Sw #1 Read Input
PBSw2 Alias PinC.2 'PB Sw #2 Read Input
'Variable Definitions:
Dim Lpcnt As Word 'Loop Counter
Dim Ttcnt As Word 'Tick Toc ISR Counter
Dim Rvbit As Bit 'For Register Setup
Dim Regdata As Byte 'For Register Setup
Dim Tmpstr As String * 16
Dim Hbcnt As Word 'Hear Beat Counter for LED
Dim Regval As Byte 'Register Value for manual config
'PB Switch Debounce variables:
Dim PBSw1DBF as Byte 'PB Sw #1 Debounce Flag, 1 = Pressed
Dim PBSw2DBF as Byte 'PB Sw #2 Debounce Flag, 1 = Pressed
Dim PBSw1Cntr as Byte 'Debounce couunter
Dim PBSw2Cntr as Byte 'Debounce couunter
Dim PBSwDBVal as Byte 'Threshold for Debounce, mSec
Dim Pressed as Byte 'PB Sw Status for User stopped pressing
'===============================================================================
'Hardware Startup Delay:
Waitms 500 'Power Up Time
'Configure the Port's Pins for Input or Output mode.
'0 = Input, 1 = Output
'Nano Testbed Blue PCB with Bosch Sensor configuration:
Config Portb = 47 'PortB
Config PortC = 35 'PortC
Config PortD = 246 'PortD
'Hardware startup Flash everything:
Gosub LEDFlash 'Startup flash
'Enable internal Pull-Up resistors for the two PB Switches
PortC.3 = 1 'PB Sw 1
PortC.2 = 1 'PB Sw 2
'............................................................................
'Config Timer/Counter #0, 8-Bit, for q 1 mSec ISR, (CTC Mode)
'This is the Master ISR for the Project's timing needs.
'Clock = 16 MHz, Pre-Scaler = Div by 64
'Clock into T/C#0 = 16M/64 = 250k
'Set Top to 250 - 1, (for roll over to 0), for q 1 mSec ISR.
'First Turn On the Timer/Counter#0 Module.
'Read the Power Reduction Register, set the Tim0 bit to 0 to Enable it.
'Then write it back. This does NOT alter any other module's activity.
'Bitwise AND with 1101 1111 All bits unchanged, except Bit 5 = 0.
Regval = Prr 'Read in current Power Reduction Register Values
Regval = Regval And &B11011111 'Force Bit 5 = 0
Prr = Regval 'Write back the new value
tccr0a = 2 'CTC Mode, No I/O pins active
tccr0b = 3 'Pre-Scaler = Div By 64
timsk0 = 2 'Intr Output Comp A Enabled
ocr0a = 249 'Div by 250
On Oc0a TicToc 'Timer/Counter0 CTC A Intr
'...............................................................................
Inits:
Ttcnt = 0 'Init Tick Tock ISR Counter
PBSwDBVal = 25 '25 or 255 mSec Debounce Time
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Main:
Enable Interrupts 'Global Enable Interrupts
Do
'Do something useful here
'
'
'Now check the User Interface and act accordingly
'Check User Interface Push Button Switch flags from debounce routine:
If PBSw1DBF = 1 then
'PB Sw #1 is pressed and debounced
Set LED2
'Now clear the flag
PBSw1DBF = 0
End if
If PBSw2DBF = 1 then
'PB Sw #2 is pressed and debounced
Reset LED2
'Now clear the flag
PBSw2DBF = 0
End if
Loop
End
'.........................................................................................
TicToc:
'Timer/Counter#0 ISR
'Master Timer for Project
'ISR Fires at 1 KHz rate, (once / 1 mSec)
'HeartBeat LED Flashes once per Second
Ttcnt = Ttcnt + 1 'Incr
If Ttcnt < 5 Then
Set Led1 'LED On
Else
Reset Led1 'LED Off
End If
If Ttcnt > 999 Then
Ttcnt = 0 'Rollover
End If
'PB Sw debouncer code
'On Exit set PBSw1DBF Debounce Flag = 1 if pressed
'On Exit set PBSw2DBF Debounce Flag = 1 if pressed
'These Flags are cleared by the Main Loop processing them.
'PBSwDBVal is set in inits.
'It is the number of mSec the PB Switch must be pressed
'continuously without any further bounces before the
'switch is considered debounced. e.g. 20
'Master TicToc ISR fires once per mSec.
'Process PB Sw#1:
If PBSw1 = 0 then
'It is pressed
PBSw1Cntr = PBSw1Cntr + 1 'Add 1 mSec
If PBSw1Cntr >= 255 then 'Hold so variable doesn't wrap to 0
PBSw1Cntr = 255
End If
Else
'It either isn't pressed, or it bounced
'So reset the counter and start over
PBSw1Cntr = 0
End if
If PBSw1Cntr >= PBSwDBVal then
'It is debounced
PBSw1DBF = 1 'Set Debounced Flag
End if
'Process PB Sw#2:
If PBSw2 = 0 then
'It is pressed
PBSw2Cntr = PBSw2Cntr + 3 'Add 3 mSec
If PBSw2Cntr >= 255 then 'Hold so variable doesn't wrap to 0
PBSw2Cntr = 255
End If
Else
'It either isn't pressed, or it bounced
'So reset the counter
PBSw2Cntr = 0
End if
If PBSw2Cntr >= PBSwDBVal then
'It is debounced
PBSw2DBF = 1 'Set Debounced Flag
End if
Return
LEDFlash:
'Flash LEDs on startup
For LpCnt = 1 to 3
Set LED1
Waitms 75
Set LED2
Waitms 75
Set LED3
Waitms 75
Reset LED1
Waitms 75
Reset LED2
Waitms 75
Reset LED3
Waitms 75
Next LpCnt
Return
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|