I would recommend using a switch state latch bit and simple logic to filter out all but a "new press" or "new release" state without having to wait for the opposite state. You didn't mention if you're using active-low or active-high switch signals so I'll provide (C code) examples for both using parallel switch state logic (below).
Code:
/*
* K8LH parallel switch state logic
*
* sample active lo switches with a "new press" filter or
* sample active hi switches with a "new release" filter
*
* swnew ___---___---___---___ sample switches
* swold ____---___---___---__ switch state latch
* swnew ___-__-__-__-__-__-__ changes, press or release
* swnew ___-_____-_____-_____ filter for desired state
* flags ___------______------ toggle flag bits for main
*
*/
swnew = ~portb; // sample active lo switches
swnew ^= swold; // changes, press or release
swold ^= swnew; // update switch state latch
swnew &= swold; // filter for desired state
flags ^= swnew; // toggle flag bits for main
Code:
/*
* K8LH parallel switch state logic
*
* sample active hi switches with a "new press" filter or
* sample active lo switches with a "new release" filter
*
* swnew ___---___---___---___ sample switches
* swold ____---___---___---__ switch state latch
* swnew ___-__-__-__-__-__-__ changes, press or release
* swnew ___-_____-_____-_____ filter for desired state
* flags ___------______------ toggle flag bits for main
*
*/
swnew = portb; // sample active lo switches
swnew ^= swold; // changes, press or release
swold ^= swnew; // update switch state latch
swnew &= swold; // filter for desired state
flags ^= swnew; // toggle flag bits for main
Execute this code in an ISR or in a main program loop at some "debounce" interval, typically 10 to 20 milliseconds. Test "flags" bit 4 in the main program for a "new press" or "new release", whichever you're using, for the switch connected to RB4. Clear the flag bit after using it.
To emulate a toggle switch, where you press a switch to toggle its flag from on-to-off or from off-to-on, simply test the flag bit for that switch without clearing the flag afterwards.
The switch state latch variable (swold) represents the real time (plus debounce interval) debounced state of the switches on portb and can be used for selective "repeat" key operation, if desired.
Good luck with your project and Happy Holidays to everyone.
Cheerful regards, Mike
Bookmarks