> Van: David Olson > Aan: PICLIST@MITVMA.MIT.EDU > Onderwerp: Button Pressing Behavior > Datum: zondag 9 mei 1999 3:45 > > Friends, Hello David, > I've got a routine that polls a switch and turns an LED on or off. I solved > some of my earlier problems (subject: [OT] Newbie Toggle Woes...) by adding > a debouncing scheme. I'm using the Kunz (1999) method of TMR0 and checking > three times. Works great. > > However, if I hold the button down (RA1 is high), I get a flashing LED > (RB1) - because it's toggling on and off. > Also, I'm dying for a subroutine that I can pass parameters. This whole > switch routine has to work for 4 switches. I'm thinking that if I throw the > switch information in a "generic" register, I'll get what I want. > > Any clues? Not looking for code - just looking for a nudge to "discover" the > best way. Pseudo-code: 1) Set a Loop-counter to some preset value. 2) Poll the input-port your switch(es) are connected to. Mask off all port-bits that do not have a (required) switch attached. (Your "argument" !) Check if the result is the same as last time. If not, goto 1) Decrement Loop-counter. If not Zero goto 2) (if it is, the key is stable for {preset value} scan's !) Resulting byte contains a One on places where a switch is connected, but not pressed, and a Zero on all other places. To check for a Key-press : call this routine and test for pressed key's (Zero-bits) in the returned byte To wait for Key-release : call this routine and repeat (calling the routine) if any key is pressed I'm using this setup in several devices, and it works like a charm. For your program: Check for a Key-Press if so : Toggle the Led & wait for Key-Release repeat from top Greetz, Rudy Wieser