It's not all that hard.
Strip out all other code, and work on a basic test... (blinking LED!)
Instability may be due to switch bounce or too much happening within the ISR.
ISR code should be as short and sweet as possible.
Test your state flags and do further (time consuming) work in your main()...
(CPU cycles spent in main() are 'free' - you will just get interrupted again if something else changes )
// ================================
void main() {
- Make sure the idle state of your inputs are pulled as expected
- Set up your IO pins.
- Clear any stray interrupt source flags (in case!)
- Enable interrupt modes
-- things can start happening now...
// ----------------
while(1) {
-- main stuff
-- test your private flags and call your action functions
-- reset your private flags or whatever is needed to maintain your operating state
}
// ----------------
}
// ================================
void interrupt isr {
- Test which interrupt source fired the ISR...
- if (RBIF) {
-- test those specific I/O bits that are your sensor-input pins
(minimise the code executed within the ISR - e.g. only set some flags etc) - }
- Clear the ISR source flag & return to caller.
RBIF = 0;
}