A sleep (or deepSleep) example with button interrupt wakeup

Dependencies:   mbed

Committer:
jose_23991
Date:
Mon Sep 08 17:41:39 2014 +0000
Revision:
0:4d80b159d444
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jose_23991 0:4d80b159d444 1 #include "mbed.h"
jose_23991 0:4d80b159d444 2
jose_23991 0:4d80b159d444 3 InterruptIn event_button(USER_BUTTON); // Create the button event object
jose_23991 0:4d80b159d444 4 volatile bool go_to_sleep = false; // Volatile variable to make the system sleep
jose_23991 0:4d80b159d444 5
jose_23991 0:4d80b159d444 6 void ISR_pressed() // ISR for the button press
jose_23991 0:4d80b159d444 7 {
jose_23991 0:4d80b159d444 8 printf("Button pressed\n"); // Show that the button has pressed
jose_23991 0:4d80b159d444 9 go_to_sleep = !go_to_sleep; // Toogle the sleep state
jose_23991 0:4d80b159d444 10 event_button.disable_irq(); // Disable the interrupt request
jose_23991 0:4d80b159d444 11 }
jose_23991 0:4d80b159d444 12
jose_23991 0:4d80b159d444 13 int main()
jose_23991 0:4d80b159d444 14 {
jose_23991 0:4d80b159d444 15 int i = 0;
jose_23991 0:4d80b159d444 16
jose_23991 0:4d80b159d444 17 event_button.mode(PullUp); // Setup the internall pull-up resistor
jose_23991 0:4d80b159d444 18 event_button.fall(&ISR_pressed); // Set the ISR associated to event fall (push the button)
jose_23991 0:4d80b159d444 19
jose_23991 0:4d80b159d444 20 while(1)
jose_23991 0:4d80b159d444 21 {
jose_23991 0:4d80b159d444 22 if(go_to_sleep)
jose_23991 0:4d80b159d444 23 {
jose_23991 0:4d80b159d444 24 printf("%d: Entering sleep (press user button to resume)\n", i);
jose_23991 0:4d80b159d444 25
jose_23991 0:4d80b159d444 26 event_button.enable_irq(); // Enable the interrupt request
jose_23991 0:4d80b159d444 27 //sleep(); // Enter Low Power Mode
jose_23991 0:4d80b159d444 28 deepsleep(); // Enter Low Power Mode (deep)
jose_23991 0:4d80b159d444 29 wait_ms(200); // Wait 200ms for debounce
jose_23991 0:4d80b159d444 30 event_button.enable_irq(); // Enable the interrupt request
jose_23991 0:4d80b159d444 31 }
jose_23991 0:4d80b159d444 32 else
jose_23991 0:4d80b159d444 33 {
jose_23991 0:4d80b159d444 34 printf("%d: Running\n", i); // Show the counter
jose_23991 0:4d80b159d444 35 }
jose_23991 0:4d80b159d444 36 i++;
jose_23991 0:4d80b159d444 37 }
jose_23991 0:4d80b159d444 38 }