David BALDWIN wrote: > > Hi, > > As usual, I have some questions today... I am using the check_keys > routine from Bob Blick. In fact, it only detects falling edges on portA. > I am searching for a routine that could decide between a single press on > a switch and a long press on a switch (more than 500ms or 1s). The > reason is that I don't have so much I/O on my 16C84 and had like one > switch to have more than one function. Can anybody help? > I use a routine in my Timer0 interrupt service that does the following : Is button pressed ? Yes : Increase counter BUTTON_COUNTER Is BUTTON_COUNTER > 10 (This value depends on your app) Yes : Set BUTTON_PRESSED_FLAG No : BUTTON_COUNTER = 0 All you have to do in your main routine, is to look if BUTTON_PRESSED_FLAG is set. If you want to use look for long and short presses, do the following : Use a routine in your Timer0 interrupt service that does the following : Is button pressed ? Yes : Increase counter BUTTON_COUNTER Is BUTTON_COUNTER > 20 (This value depends on your app) Yes : Set LONG_FLAG Is BUTTON_COUNTER > 10 (This value depends on your app) Yes : Set SHORT_FLAG No : BUTTON_COUNTER = 0 Is LONG_FLAG set ? Yes : Set LONG_BUTTON_PRESSED_FLAG Else is SHORT_FLAG set ? Yes : Set SHORT_BUTTON_PRESSED_FLAG All you have to do in your main routine, is to look if SHORT_BUTTON_PRESSED_FLAG, or LONG_BUTTON_PRESSED_FLAG is set. You have to remember to reset the flags after use in your main routine. If you think this is a very long and cumbersome method, here are a few points to consider : 1) Your software never hangs in delay loops 2) The inputs are much better debounced than with just two samples. 3) You work with flags, so you don't have to worry about the debouncing - it happens by itself. 4) If you have multiple inputs, you can optimise the code to be shorter than other methods. 5) If you use this method to debounce keypads, you'll find double keypresses easy. -- Friendly Regards Tjaart van der Walt mailto:tjaart@wasp.co.za _____________________________________________________________ | Another sun-deprived R&D Engineer slaving away in a dungeon | | WASP International http://wasp.co.za | | GSM and GPS value-added applications | | Voice : +27-(0)11-622-8686 | Fax : +27-(0)11-622-8686 | |_____________________________________________________________|