This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SW debounce button and sleep

Hi all , 

1)

I need debounce a button with i use an interrupt .  Debounce button in "normal" program is easy , check change status on button, then __delay_cycles(); and check status again . I dont know how make this debouncing delay on button which call interrupt vector.


for imagine 

button press

delay

check if still press

make interrupt 

2)

How i go sleep MSP and wake up mcu using ONE button ? Get sleep is easy, i set on bit sr register to LMP mode , thats ok, but how i wake up from LMP same button ? 

Thank you very much

  • Martin Krchnak said:

    1) I need debounce a button with i use an interrupt .  Debounce button in "normal" program is easy , check change status on button, then __delay_cycles(); and check status again . I dont know how make this debouncing delay on button which call interrupt vector.


    for imagine 

    button press
    delay
    check if still press
    make interrupt 

    I would suggest you change your approach a bit.  Use the interrupt to capture the initial edge.  Within the interrupt service routine, set a flag to indicate that an initial button press has been detected.  When you exit the interrupt service routine, be sure to keep the CPU awake by ensuring you do not go back into a Low Power Mode.

    In your main processing loop, you check this flag and kick off a delay timer, perhaps the __delay_cycles() if you prefer.  After this is completed, poll the GPIO bit associated with the button press again to ensure it is still "pressed" and then set a different flag indicating a confirmed button press.  Your applicaiton code can then take action on this "confirmed button press".

     

    Martin Krchnak said:

    2) How i go sleep MSP and wake up mcu using ONE button ? Get sleep is easy, i set on bit sr register to LMP mode , thats ok, but how i wake up from LMP same button ? 

    You can setup the GPIO to interrupt the CPU and this action will wake up the CPU to service the interrupt service routine.  As I mentioned above, within the interrupt service routine, you will need to execute an instruction to indicate not to go back to the original Low Power Mode upon existing the ISR.

  • It is the same in ISR, but the delay has to be done by using interrupts too isntead of busy-waiting.

    So in the port ISR, start a timer that triggers an interrupt after the requried delay. In the timer ISR, then check the port state again and signal the result to main.

    To enter/exit slewep mode, you can use the BIC_SR_ON_EXIT and BIS_SR_ON_EXIT intrinsics (exact syntax see compiler manual). inside the ISR. It will enter (or exit) sleep mode when the ISR returns.

    Do not directly set the LMP bits inside an ISR. It will freeze the MSP while still inside the ISR. Worst case, you'll end up with the stack filled with anless ISR calls that never returned until the stack overflows into your variables. Or it will never wake up again.

**Attention** This is a public forum