![]() |
|||||||||||||||||||||||
|
[?]
Subscribe To This Site
|
Custom Search
Readers
comments
|
||||||||||||||||||||||
12F675 Tutorial 2 : Switch debounceThis 12F675 microcontroller switch debounce tutorial shows you how to read keys at a microcontroller port and how to debounce a switch for reliable switch detection.Sponsored Links...
Jump to Solderless breadboard. Jump to Circuit diagram. Jump to Software. A safe storyWe bought a small safe, not to store gold bullion in but to store my wife's medicines in - having a curious three year old and needing access to the medicines required it.I could not believe it when I entered in an access code that virtually every time I hit a key - the key bounced i.e. there were multiple key presses for every single key press unless I hit the keys very precisely and quickly - They were obviously in a hurry when they designed it. When this happened the safe sat there bleeping for one minute - very annoying. This is a example of why switch debounce is important and the designer of the electronics obviously had no idea about it. I even opened it up - it had an atmel microcontroller inside so they could have easily got rid of the switch bounce problem. One day I'll have to change it (using a PIC Micro of course) we just use the key now! Switch debounce : What is it ?When a switch is pressed it does not fully close the first time you press it because the metal contacts bounce off each other!Feeding the signal into a logic gate or a microcontroller sends multiple key press signals which is not what you want so you have to ignore the bouncing signal - this is known as debouncing the switch. The basic switch debounce microcontroller solution is:
The switch debounce solutionThe solution in all cases is time - you have to wait until the bouncing has stopped. You either use a debouncing circuit or use software to wait for a period after the bouncing has stopped.The most common discrete switch debouncing circuit is a resistor and capacitor pair which slows the input signal feeding into a logic gate (charging the capacitor when the switch is closed) - in this case the gate must have hysteresis so that it reacts correctly otherwise it could oscillate anyway. When using a microcontroller the best way is to use software to debounce the switch as the microcontroller can easily wait a set time period before deciding that the switch value is valid. You can also change the time period and do not need extra components. Solderless breadboardAdd the switch and pull up resistor and then program the chip using the the hex file.![]() Circuit diagramAgain the switch debounce circuit is easier to see on the schematic.![]() Software
|
|||||||||||||||||||||||
| Buy
all the 12F675 Tutorial
source code ...with the MikroC project files and compiled hex files Click here for more information. |
|
| ////////////////////////////////////////////////////////////////////// void init_ports(void) { TRISIO = (1<<5); // set as output except bit 5 GP5=i/p } ////////////////////////////////////////////////////////////////////// int get_key(void) { // Is GP5 low - no so exit if (GPIO & (1<<5)) return 0; // no key yet delay_ms(1); // wait for key to settle // Is GP5 high ? yes so exit = false key. if ( (GPIO & (1<<5))>0 ) return 0; // was a false key so restart return 1; // key ok so return valid } |
Basically for the switch debounce procedure you test a key, wait a while and re-test - if the result is the same then it was a valid key.
| Back | 12F675 Tutorial Index | Next |
Jump from 12F675
Tutorial 2 : switch
debounce
to
Best-Microcontroller-Projects Home Page
![]() |
Don't forget to Sign Up for your Microcontroller
Newsletter
With "Essential tips and techniques", ..."New Site Info" and more... |
![]() |
|
![]() |
Including a
free project :
How to drive
an LCD and 12key keypad using "Only
One 8 Bit Port"
with no
interface logic!...
(Works for any microcontroller)
This costs you : Nothing... ![]() Just fill out the form below and you'll get full C source code and project schematic and description. |
||
|