NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Basic Electronics » Debouncing and External Interrupts
February 17, 2010 by ecornwell |
Hello, I've run into a little problem I can't quite wrap my head around. I've got a little project setup where I use the servo squirter code to move a servo. I added another mode that has timed movements for the servo. I added another "crazy" mode where the servo moves to random positions and waits a random amount of time between the movements. This is pretty cool and helped me get a better understanding of some concepts. (I still don't fully understand how the PWM signal is generated and works but I'll get there.) What I wanted to do is be able to use all 3 modes and have the included button allow me to cycle through them. I'm having a little problem with the debouncing. I've got the external interrupt setup but I noticed when I hit it, it would go through all 3 modes. (The interrupt handler increments a counter.) I tried using the debounce function from the motors tutorial but it doesn't seem to be working correctly. Sometimes I get a response and other times I don't. I can get it to cycle through if I press the button enough times but I want it to change once when I press the button. I would also like it to break out of whatever its doing as well but I'm really unsure on that one. It is either in a delay_ms() step or a uart_read() which the latter forces me to give uart input in order for the program to move along. So what's the best way to debounce a button when you're using external interrupts? Thanks! |
---|---|
February 17, 2010 by pbfy0 |
Well, I din;t know if you're doing this, but in your external interrupt you should at least wait for the end of the button press ( |
February 18, 2010 by Noter |
Basically you need to ignore button input for a short period after the 1st interrupt. Using a timer to control the button disabled period works well ...
|
February 18, 2010 by johnh |
For more on PWM, refer to the Timer/Counter0 documentation. ATMega 168 data sheet Pg 90. specifically PWM, Pg 97+. Noter's code is very detailed. It looks to be very effective. An alternative, albeit possibly less effective, solution is something like the following:
The big advantage of that particular code is that it frees up Timer0 for other uses. The big disadvantage is that it will cause your interrupt to hold for 200 ms, as opposed to Noter's code, which will not. This could be problematic if you have a lot of interrupts or events happening at the same time, as they will queue behind this interrupt as opposed to firing immediately. However, this should not impact your PWM adversely. |
October 24, 2010 by Gordito |
I found this great link on Hack a Day, so I hopt this helps with your project. http://www.ganssle.com/debouncing.htm G- |
January 29, 2011 by leedawg |
This was exactly what I was looking for trying to just make a simple on and off switch using an PCINT1 interrupt. I had mine structured in a similar fashion using a variable that was incremented each time the pin interrupt was called however I found it would increment up by 2 sometimes 7 which was probably due to me not being able to push the button on and off fast enough and several interrupts fired for each button push. This has solved that problem however I find that if I depress the button a moderate speed it will sometimes toggle right back off again. My question is if you hold the button down with the code provided by Noter what stops the "button processing code" after the if(!PINC & (1<<PC5) function from being executed over and over while the button is depressed or does the code not leave the if statement until the button is released? Thanks again Im a total Noob to both C and micro-controllers but learning slowly Thanks. Lee |
January 29, 2011 by hevans (NerdKits Staff) |
Hi leedawg, What keeps the code from executing over and over is that the whole thing is in a pin change interrupt handler, so only when there is a pin change on the proper pin will that code execute. The timer keeps it from executing twice if two pin transitions happen really close to each other (debouncing), but a sustained press will only generate one pin transition. Humberto |
March 26, 2011 by hariharan |
hey! i want to know how to set interrupts . like do something for the first interrupt. do something for the second interrupt. do something for the third interrupt. if there is no third interrupt, do something |
March 27, 2011 by Mujda |
Hi, I've also been trying to sort out debouncing issues on reed switches. Using the Timers in the software to create a pause before incrementing the pulse count worked fine, but it used up timers. My issue now is I need the mcu to run at 32kHz to save power - too slow to run an interrupt driven increment. I've reverted to using a hardware low-pass filter. All you need is 2 resistors and 1 capacitor. 1 resistor (22k) between input pin and Vcc, 1 resistor (3.6k)in-line with the input, and the capacitor (500nF)between the input pin and 0V. You need to turn off the input pin pull-up resistor, as the 22k now does this job. The problem with using the internal pull-up resistors is they vary between 20-50k, which is too much uncertainty - hence using an external one. |
Please log in to post a reply.
Did you know that you can build a circuit to convert the "dit" and "dah" of Morse code back into letters automatically? Learn more...
|