View previous topic :: View next topic |
Author |
Message |
ll
Joined: 10 Jan 2010 Posts: 16

|
Posted: Fri Feb 24, 2023 4:23 pm Post subject: problem DEBOUNCE |
|
|
It seems that in one program a DEBOUNCE command linked to a port can only be used for one subroutine.
See also the example program.
So if the same port/switch is used in different places in a program, a different subroutine cannot be called per place.
If you do want one switch to be able to call several subroutines, this is only possible by
requesting the PIN status of a port with an “IF” statement.
Is this statement correct or am I doing something wrong?
Thank you in advance, kind regards,
Loek
Compiler version :2.0.8.5
Compiler build :2.0.8.5.004
IDE version :2.0.8.5.002
$regfile = "m328pdef.dat"
$crystal = 16000000
$hwstack = 250
$swstack = 250
$framesize = 250
dim ijk as byte
dim niv_teller as byte
dim freq_band_teller as byte
Config Portd.5 = input 'freq_band
Portd.5 = 1 'pull-up
Config Portd.6 = input 'ijk_toets
Portd.6 = 1 'pull-up
'*******************************************************************************
ingedrukt Alias 0
ijk_toets alias pind.6
freq_band alias pind.5
startt:
'the subroutine "is_freq_band_gedrukt" is executed
Debounce freq_band , 0 , is_freq_band_gedrukt,sub
'the subroutine "is_niv_counter_gedrukt" is executed
Debounce ijk_toets , 0 , is_niv_teller_gedrukt, sub
select case niv_teller
case 2 to 3 :
if freq_band = ingedrukt then
gosub op_bergen_eprom 'this subroutine is being executed
endif
case 4:
Debounce freq_band , 0 , stoppen_ijken ,sub 'THIS SUBROUTINE IS NOT EXECUTING
'WHEN I REPLACE THIS LINE WITH:
if freq_band = ingedrukt then
gosub stoppen_ijken 'the subroutine is executed
endif
case 5:
if freq_band = ingedrukt then
niv_teller= 1 'this is executed
endif
end select
goto startt
'*********************************************************
stoppen_ijken:
ijk=0
return
'**********************************************************
is_freq_band_gedrukt:
freq_band_teller = freq_band_teller + 1
if freq_band_teller >5 then
freq_band_teller =1
endif
return
'**********************************************************
is_niv_teller_gedrukt:
niv_teller = niv_teller + 1
if niv_teller >5 then
niv_teller =1
endif
return
'**********************************************************
op_bergen_eprom:
return
'**********************************************************
end
[b][color=red](BASCOM-AVR version : 2.0.8.5 )[/b][/color] |
|
Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2335

|
Posted: Fri Feb 24, 2023 6:15 pm Post subject: |
|
|
Check the help:
Quote: | When DEBOUNCE is executed again, the state of the switch must have gone back in the original position before it can perform another branch. |
The way your code executes, these two working on the same pin:
Code: | Debounce freq_band , 0 , is_freq_band_gedrukt,sub
' ...
Debounce freq_band , 0 , stoppen_ijken ,sub |
may follow close to each other and then above quote applies.
The way you use Debounce() seems unsuitable for the menu structure.
I would create a single sub, where all keys are checked and key messages are returned.
Code: | Const key_msg_void = 0
Const key_msg_left = 1
Const key_msg_right = 2
Const key_msg_up = 3
Const key_msg_down = 4
Dim akey as Byte
Do
' ...
' sub returns key_msg_left in akey
' ...
If akey = key_msg_left Then
' do something
akey = key_msg_void ' mark message as processed
End If
Loop |
This kind of message system has also the benefit, that any key handling is done in a single routine, for a different key hardware or key wiring, only this routine needs an edit. |
|
Back to top |
|
 |
ll
Joined: 10 Jan 2010 Posts: 16

|
Posted: Sat Feb 25, 2023 1:00 pm Post subject: problem DEBOUNCE |
|
|
Hello,
Thanks for your comment. My problem is not
“When DEBOUNCE is executed again, the state of the switch must have gone back in the
original position before it can perform another branch."
At the first debounce “freq_band “ the sub “is_freq_band_gedrukt” works good then the second debounce “ijk_toets “ works good.
In both situation the switch are back in the old rest state.
Due to the actions performed in the subroutines “ is_freq_band_gedrukt” and “is_niv_teller_gedrukt” the switch on pind.5 (freq_band)
gets a different function.
So on the third debounce “freq_band “ with a new subroutine “stoppen_ijken” you would expect the functions listed in these subroutine
to be called.
This is not the case, the applications listed in the subroutine “is_freq_band_gedrukt” are executed
So this has nothing to do with :
“When DEBOUNCE is executed again, the state of the switch must have gone back in the original position before it can perform another branch."
because at the last debounce the key is also in rest state again.
Calling the same subroutine immediately afterwards via “if freq_band = ingedrukt then” works good and this is about the same switch.
Yours sincerely,
Loek |
|
Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2335

|
Posted: Sat Feb 25, 2023 3:55 pm Post subject: |
|
|
I've demonstrated you a way which works, and it is used by the OS where you write your posts.
If you want to follow up your failing way: Be my guest.
It's a simple riddle by omitting all unimportant instructions.
Assume niv_teller equals 4, <n> are symbolic steps:
Code: | startt: ' <1>
Debounce freq_band , 0 , is_freq_band_gedrukt ,sub ' <2>
case 4:
Debounce freq_band , 0 , stoppen_ijken ,sub ' <3>
goto startt ' <4> |
If no key is pressed, this row 1 - 4 is finished within 71 cycles at 16 million clocks per second.
Thus the whole loop is executed 225,352 times per second, that's about 0,0044 milliseconds per loop.
Now tell me: How do you manage it with an average reaction time of about 180 milliseconds to hit the key exactly between <2> and <3>?
If you manage that, the correct label stoppen_ijken is called.
You can check with the simulator. |
|
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
|
|