University of Plymouth - Stages 1, 2 and 3
/
Task325-FZ429
Demo of ISR waking MCU
main.cpp@5:c11d12dab1e2, 2019-09-12 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 12 13:59:51 2019 +0000
- Revision:
- 5:c11d12dab1e2
- Parent:
- 4:684525c93d74
Fixed bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:24ccc6d0c391 | 1 | #include "mbed.h" |
noutram | 0:24ccc6d0c391 | 2 | |
noutram | 0:24ccc6d0c391 | 3 | // This uses INTERRUPTS to detect a falling edge of the switch input |
noutram | 0:24ccc6d0c391 | 4 | // However, pressing and releasing the switch can result in spurious falling edges |
noutram | 0:24ccc6d0c391 | 5 | // which trigger the routine |
noutram | 0:24ccc6d0c391 | 6 | |
noutram | 4:684525c93d74 | 7 | //Uncomment this if you want to use the onboard LEDs and Blue Switch on a F429ZI |
noutram | 5:c11d12dab1e2 | 8 | //#define USEONBOARD |
noutram | 4:684525c93d74 | 9 | |
noutram | 0:24ccc6d0c391 | 10 | //Declare functions |
noutram | 0:24ccc6d0c391 | 11 | void sw1FallingEdge(); |
noutram | 0:24ccc6d0c391 | 12 | |
noutram | 0:24ccc6d0c391 | 13 | //Global Objects |
noutram | 4:684525c93d74 | 14 | #ifdef USEONBOARD |
noutram | 3:52ced27ad588 | 15 | DigitalOut red_led(LED3); |
noutram | 3:52ced27ad588 | 16 | DigitalOut green_led(LED1); |
noutram | 4:684525c93d74 | 17 | InterruptIn sw1(USER_BUTTON); |
noutram | 4:684525c93d74 | 18 | #else |
noutram | 4:684525c93d74 | 19 | DigitalOut red_led(D7); |
noutram | 4:684525c93d74 | 20 | DigitalOut green_led(D5); |
noutram | 4:684525c93d74 | 21 | InterruptIn sw1(D4); |
noutram | 4:684525c93d74 | 22 | #endif |
noutram | 0:24ccc6d0c391 | 23 | |
noutram | 0:24ccc6d0c391 | 24 | //Interrupt service routine for a rising edge (press) |
noutram | 0:24ccc6d0c391 | 25 | void sw1FallingEdge() { |
noutram | 0:24ccc6d0c391 | 26 | red_led = !red_led; //Toggle the LED |
noutram | 0:24ccc6d0c391 | 27 | } |
noutram | 0:24ccc6d0c391 | 28 | |
noutram | 4:684525c93d74 | 29 | Serial pc(USBTX, USBRX); |
noutram | 4:684525c93d74 | 30 | |
noutram | 0:24ccc6d0c391 | 31 | //Main - only has to initialise and sleep |
noutram | 0:24ccc6d0c391 | 32 | int main() { |
noutram | 4:684525c93d74 | 33 | |
noutram | 4:684525c93d74 | 34 | pc.set_blocking(true); |
noutram | 0:24ccc6d0c391 | 35 | red_led = 0; |
noutram | 0:24ccc6d0c391 | 36 | green_led = 1; |
noutram | 0:24ccc6d0c391 | 37 | |
noutram | 0:24ccc6d0c391 | 38 | //Configure interrupts, wait for first rising edge |
noutram | 0:24ccc6d0c391 | 39 | sw1.fall(&sw1FallingEdge); |
noutram | 0:24ccc6d0c391 | 40 | |
noutram | 0:24ccc6d0c391 | 41 | //Main Polling Loop |
noutram | 0:24ccc6d0c391 | 42 | while (true) { |
noutram | 0:24ccc6d0c391 | 43 | |
noutram | 0:24ccc6d0c391 | 44 | //Put CPU back to sleep |
noutram | 0:24ccc6d0c391 | 45 | sleep(); |
noutram | 0:24ccc6d0c391 | 46 | |
noutram | 0:24ccc6d0c391 | 47 | //You can ONLY reach this point if an ISR wakes the CPU |
noutram | 4:684525c93d74 | 48 | green_led = !green_led; |
noutram | 4:684525c93d74 | 49 | |
noutram | 4:684525c93d74 | 50 | //Any use of the serial port will produce a series of interrupts |
noutram | 4:684525c93d74 | 51 | //pc.puts("Ping!\n\r"); |
noutram | 0:24ccc6d0c391 | 52 | |
noutram | 0:24ccc6d0c391 | 53 | } //end while |
noutram | 0:24ccc6d0c391 | 54 | |
noutram | 0:24ccc6d0c391 | 55 | } |