Hello,
On the subject of debounce, use the forum search engine below - I recall this has been discussed on previous occasions. As Witztronics has suggested, within the interrupt service routine (ISR), disable further KBI interrupts for that pin, carry out the action associated with the keypress, and
clear the interrupt flag. If the required action is too lengthy, it is probably better to set a user flag within the ISR, and to poll for the flag within the main loop.
Now the debounce part. Typically, you will now need to wait 10 - 50 milliseconds before re-enabling the KBI interrupt, ready for the next keypress. While you could enter a simple software timing loop, this would prevent anything else from occurring during the wait period (unless you were to poll for specific events during this period).
Probably a better way is to setup a TPM module so that overflow interrupts will periodically occur. You can then configure your debounce delay to be an integral number of overflow periods. You will need a byte size counter (global) variable for each switch contact, to handle the debounce timing. Within the KBI ISR, set the counter to the required delay value. Then, within the TPM overflow ISR do the following:
- Test whether the counter is non-zero.
- If so, decrement the counter.
- Now, again test whether the counter has reached zero.
- If so, re-enable the KBI interrupt.
Using this method, the debounce will be transparent to the main loop.
Whether you need to interrupt on a rising or a falling edge will be determined by the connection configuration for the pushbutton. Assuming that the contact closes when the button is pressed, if the switch is connected between the input pin and ground, you will require falling edge setup. If the switch is returned to Vdd instead of ground, the rising edge would be applicable.
Regards,
Mac
Message Edited by bigmac on
2009-02-16 11:50 AM