Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
:
:
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

Back 12F675 Tutorial Index Next

12F675 Tutorial 2 : Switch debounce

This 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.



Jump to Solderless breadboard.
Jump to Circuit diagram.
Jump to Software.

A safe story

We 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:
  • Detect the 1st button press (or the 1st of many bounces!).
  • Wait for a while.
  • Check the switch again to see if it is still presed.
Note: Because each switch is constructed differently the characteristics of the key bounce will be different so you need to characterize different switches and assess how long the bounce lasts 400us, 800us or longer? (or just use a long delay).

The switch debounce solution

The 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 breadboard

Add the switch and pull up resistor and then program the chip using the the hex file.

12F675 switch debounce

Learn about the tool used for creating this diagram.

Circuit diagram

Again the switch debouncecircuit is easier to see on the schematic.

switch debounce circuit diagram

12F675 pinouts

 
12F675 pin out
Other views:

PIC12F675 PIC microcontroller micro controller PIC 12F675 micro controller


Learn about the tool used for creating this diagram.

Software

The program simply turns on the LED when the button is pressed but the button is debounced.

Source code files :
To get the file software project files and c source code click here.

Note: Initially at power up the led is flashed on and off 6 times to show the microcontroller is active.

Input button (switch) port direction

Note how init_ports() sets up the port direction for bit five of the port as an input (setting bit 5 high) using the TRISIO register all the others remain as outputs.

Switch debounce

The code that does the debouncing is shown below get_key(). It returns zero for failure and 1 if the key was ok.

First of all it simply tests the key state to make sure that the key is pressed (low input in this circuit) and if not it returns 0.

Then it waits for 1 millisecond to let the bouncing stop to (this time delay can be changed depending on the characteristics of the switch i.e. to get the correct switch debounce action).

Then the key is checked again -
if the key state has changed then it returns 0.  If it has not changed then the key has been held in a low state and is in the same state as first test - it is a valid key press so the routine returns 1.  

The following code is part of switch debounce source code:
//////////////////////////////////////////////////////////////////////
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


Privacy Policy | Contact | About Me

Site Map | Terms of Use




Visit our Facebook Page:
To Visit Click Here


Recent Articles

  1. Arduino EEPROM - how it works and how to use it - with examples.

    Arduino EEPROM: How to use it and How to presrve the life of EEPROM. Two examples sketches to save multiple values to EEPROM.

    Read more

  2. How to use the ADS1115

    A tutorial on using the ADS1115 precision 16 bit ADC for low power use.

    Read more

  3. The TP4056: Lithium Ion/polymer Battery Charger IC

    Learn how to use the TP4056 properly. There's a right, and a wrong way, to use it to safely charge Lithium Ion batteries.

    Read more

  4. DW01A Battery Protector IC

    The DW01A chip is a Lithium Ion battery protector commonly used on TP4056 boards.  Find out Exactly how it works and how to use it the correct way.

    Read more

  5. Arduino String: How to read commands from the serial port.

    For Arduino string operations you can use Object Class Strings or C style strings but which should you use? Also find out how to decode commands and control variables in your programs using strings.

    Read more

  6. A Real Time Clock design (DS1307) with a PIC microcontroller

    Real Time Clock Design (FREE): A Free and Complete RTC design using the DS1307 and a PIC micro (16F88) also re-targetable. This PIC project uses an I2C Clock chip and 7-segment display to create a fou…

    Read more

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's

SUPERB and FANTASTIC."

- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan

bursach<at>gmail.com<

"Awesome site,
very, very easy and nice
to navigate!"


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.
Regards."


- Anon

Back to Top