View previous topic :: View next topic |
Author |
Message |
throckmorton
Joined: 24 Apr 2015 Posts: 4
|
Posted: Fri Apr 24, 2015 7:08 pm Post subject: Need debounce help |
|
|
Hi. First time on the forum. I am an avr and bascom newbie. I know bas com has a debounce statement, but I would like some help in making one of my own and wonder if this approach is valid. Is my logic ok. I am not exactly sure what happens in nested if statements. Thank you.
do
if pinb.4 = 0 then 'check pinb.4
waitms 25 'wait 25ms
if pinb.4 = 0 then 'check pinb.4 again
led = 1 ' if button was held down at least 25 continuous ms then turn led on
end if
end if
loop
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Apr 24, 2015 8:42 pm Post subject: |
|
|
welcome.
yes that is a good approach : check if the key is 0, then wait some time, and check again if it is still 0. it is simple to test too. _________________ Mark |
|
Back to top |
|
|
throckmorton
Joined: 24 Apr 2015 Posts: 4
|
Posted: Mon Apr 27, 2015 5:43 pm Post subject: |
|
|
Thank you , mark for the reply. The more I think about it though, my solution my not be so effective. Are my revised comments maybe what is really happening?
The routine I wrote checks the switch a second time after 25ms, but what I think would be better is to make sure the
switch was actually held low for AT LEAST 25ms. I am looking for an approach that would mandate that the button be pressed for a minimum time. Any advice how to code for that? Thanks so much
do
if pinb.4 = 0 then 'check pinb.4
waitms 25 'wait 25ms IF EVEN THE SLIGHTEST BUMP CAUSED PINB.4 TO EQUAL ZERO
if pinb.4 = 0 then 'check pinb.4 again
led = 1 ' if button was held down at least 25 continuous ms then turn led on
end if
end if
loop |
|
Back to top |
|
|
throckmorton
Joined: 24 Apr 2015 Posts: 4
|
Posted: Mon Apr 27, 2015 7:11 pm Post subject: |
|
|
Thank you , mark for the reply. The more I think about it though, my solution my not be so effective. Are my revised comments maybe what is really happening?
The routine I wrote checks the switch a second time after 25ms, but what I think would be better is to make sure the
switch was actually held low for AT LEAST 25ms. I am looking for an approach that would mandate that the button be pressed for a minimum time. Any advice how to code for that? Thanks so much
do
if pinb.4 = 0 then 'check pinb.4
waitms 25 'wait 25ms IF EVEN THE SLIGHTEST BUMP CAUSED PINB.4 TO EQUAL ZERO
if pinb.4 = 0 then 'check pinb.4 again
led = 1 ' if button was held down at least 25 continuous ms then turn led on
end if
end if
loop |
|
Back to top |
|
|
throckmorton
Joined: 24 Apr 2015 Posts: 4
|
Posted: Mon Apr 27, 2015 7:33 pm Post subject: |
|
|
I have revised my code to:
do
If Pinb.4 = 0 Then
waitms 2
incr sbounce
If sbounce > = 28 Then '
'do stuff because with has been held at least 56ms
End If
If pinb.4 =1 then
' button has been released before count is up
sbounce = 0 'reset sbounce
' do different stuff while switch is open
end if
loop |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Apr 27, 2015 9:40 pm Post subject: |
|
|
What is bounce? when we have a digital switch we get proper 0 and 1. But an analog switch, and there are many different kinds so i take a simple push button, have a spring and 2 contacts. when you press the button, you press the spring and close the contact. if you let go the switch, the spring will push the button back. and the contacts bounce on/off very quick. this we can only measure with a microprocessor. Normal circuits like light switch do not care. So if we want to debounce, we mean that we want to detect the first switch, but not the bounce of the contacts. So a small delay, in which time the bounce occurs, is sufficient. Then after the wait we check again the state.
If you check the state during the debounce, the purpose is defeated since you would react on the bounce.
If you want to check how long somebody is keeping a button pressed, there is other logic.
I do advise to test with real hardware. there might be 1001 variations, but best to keep it simple. Also consider the fact that during the waitms, the processor is only waiting and not doing something else.
It is good practice to have a pull up resistor. The internal one is sufficient in most cases. turn it on by setting port register to 1.
config pinb.4=input : portb.4=1 'pull up _________________ Mark |
|
Back to top |
|
|
amirf
Joined: 25 Aug 2009 Posts: 154
|
Posted: Wed Apr 29, 2015 4:38 am Post subject: |
|
|
use config debounce . see bascom avr help |
|
Back to top |
|
|
|