Contact Bounce
bob kruggel
Posts: 50
I·have searched this forum for a discussion for contact bounce on the propeller without success. Here is a snippet of code that is included in"The Propeller Education Kit Labs: Fundamentals".
PUB ButtonTime(pin) : delta | time1, time2
··· repeat until ina[noparse][[/noparse]pin] == 1
··· time1 := cnt
··· repeat until ina[noparse][[/noparse]pin] == 0
··· time2 := cnt
··· delta := time2 - time1
Why doesn't delta have a value that is a function of the switch bouncing between 0 volts and 3.3 volts? [noparse][[/noparse]pin] is connected to a normally open push button switch with one terminal tied to 3.3v and the other tied to a 10k resistor to gnd. (I don't know how to draw a schematic with the propeller tool yet). Is there some magic occuring in the propeller?
BobK
PUB ButtonTime(pin) : delta | time1, time2
··· repeat until ina[noparse][[/noparse]pin] == 1
··· time1 := cnt
··· repeat until ina[noparse][[/noparse]pin] == 0
··· time2 := cnt
··· delta := time2 - time1
Why doesn't delta have a value that is a function of the switch bouncing between 0 volts and 3.3 volts? [noparse][[/noparse]pin] is connected to a normally open push button switch with one terminal tied to 3.3v and the other tied to a 10k resistor to gnd. (I don't know how to draw a schematic with the propeller tool yet). Is there some magic occuring in the propeller?
BobK
Comments
clock cycles, it takes for the switch to settle from one state to the other--high to low in this case.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
repeat until ina[noparse][[/noparse]pin] == 1 - repeats this instruction until the input on the pin is high (3.3v)
time1 := cnt - saves the current value in the free running counter "cnt" in "time1"
repeat until ina[noparse][[/noparse]pin] == 0 - repeats this instruction until the input on the pin is low (0.0v)
time2 := cnt - saves the current value in the free running counter "cnt" in "time2"
delta := time2 - time1 - calculates the difference (how long the input pin was high) and stores it in "delta"
I found it in the Prop Manual. I'm pretty sure that you could use fewer lines of code.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
Bob Kruggel
Note that you can debounce both the switch closing and opening.
I understand the technique you talked about, and my question was related to the apparent lack of discussion of contact bounce in the forum and in the education courses. The code snippet certainly doesn't acknowledge contact bounce and the code snippet seems to work correctly. It's as if the code assumes a bounceless switch and gets away with it. That's why I asked if there is some magic in the propeller.
BobK
It is extremely common even among highly trained people to ignore contact bounce. I have seen multi-thousand-dollar industrial controls plagued with these issues because the guy behind the firmware didn't know better and there wasn't a team on board with someone to correct him. In practice, if your project is going to be very busy for a while after a keypress, you may be able to get away without explicitly debouncing contacts, but you might also find yourself receiving occasional random keypresses in the form of microsecond-duration noise. Most people just don't notice the latter problem at all or blow it off as user error.
This isn't good, and I advise debouncing keystrokes if you're scanning keys.