Re: [PIC}Discriminating between button presses
Spehro Pefhany <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.pic |
|---|---|
| Message-ID | <[email protected]> |
At 12:15 PM 2024-12-28, you wrote: >Using mid range 16bit PICs I am attempting to write an algorithm that can >discriminate between short - less than 1 second - and long - greater than 3 >second - button presses. >I have written some thing which works but I am not entirely satisfied with >it and would appreciate and guidance/suggestion Hi, David:- I could go through a more general approach that I have used successfully, but,. Let me distill a couple elements down to the essentials for just this one task, and let's assume it's just one button. We set up a periodic timer interrupt. Let's say it's 2 milliseconds. Every interrupt we take a gander at the hardware button state and compare it to both the previous state (2 milliseconds ago) and the state we are presenting to the world outside the ISR. There are 8 possibilities, two of which result in a change in the debounced button state (the cases where two reads agree but disagree with the debounced button state). This gives us a program flow in the ISR for a debounced key press and a debounced key release. Assuming the periodic ISR is roughly the right frequency*. The only additional thing we need to add to our ISR infrastructure is one or more registers that are conditionally decremented every time the ISR is triggered. Decremented until they reach zero. Now you can write a number such as 3 * 500 to the register when a key press is accepted and when the release comes along you'll know if the 3 seconds has expired (and you can clear the timer if it has not). And you can set flags so the rest of your program knows what happened, that can be reset once the release has been dealt with. I'm sure you can think of other ways of dealing with this. I've used a much more modular approach that generates events, sticks them in a queue within the ISR (maybe with time stamps) and processes them in the background program (which keeps the ISR relatively simple and fast and allows event responses that are extremely complex using miserably bad processors), but I suspect that's not needed here. *if you need a faster than desired periodic timer interrupt for other reasons, you can choose to look at (or scan) the keys only every 'n' interrupts. Best regards, Spehro Pefhany