Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer for switch debouncing

Status
Not open for further replies.

ansh11

Member level 4
Member level 4
Joined
Feb 27, 2018
Messages
71
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
659
Hi
I usually use software delay for switch debouncing. I just add a 50ms delay after the first bounce. I want to use hardware timer in place of software delay. Assume hardware timer start and stop at every 1ms

What I want is, Whenever I press switch, pin of PIC microcontroller should be on/off for 30ms.

This is a only question that has nothing to do with the actual project.But I want to know how it can be done.
 

Not quite sure what you’re asking. There are timers in a PIC that you can trigger from an external pin, and then use the timer output as your 30 ms pulse.
 

Hi,

try this:

set up a timer for generating in interrupt every 10ms

Code:
ISR: (pseudo code)
{
   static uint8_t last_key_state = released;
   static uint8_t debounced_key = released;  // this may be a public variable if used else where
   uint8_t key = read_key_state();

   if (if key == pressed && last_key_state == pressed && debounced_key == released)  // checks on a debounced ON_edge
   {
      debounced_key = pressed;    // now debounced key = ON
      out_pin = 3;  // sets counter for 3 x 10ms timeout
   }

   if (last_key_state  == released && key == released)   // checks if key is released
   {
      debounced_key = released;   // now it is debounced released
   }

   last_key_state = key;   // just to memorize state for next run
   if (out_pin > 0)   // checks whether output should be ON
   {
      pin = ON;  // set pin ON
      out_pin--;  // decrement counter
   }
   else  // pin should be OFF
   {
      pin = OFF;  // set pin OFF
   }
}

Klaus
 

As an aside I worked with some cheap switches that had 300 mS of bounce time,
pretty ugly.

Attached is a collection of various techniques, may be useful.


Regards, Dana.
 

Attachments

  • Debounce.zip
    6.8 MB · Views: 138

The normal way if not denouncing in software is to use a monostable, you can set the time from the last bounce of the switch.
 

There are also cpu families these days with HW debouncing -



1620038025441.png



This is a drag and drop component, a component being an onchip resource. Attached a summary
of onchip resources.


Regards, Dana.
 

Attachments

  • Component List (2).pdf
    183 KB · Views: 99

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top