Lab version
Fork of Task327 by
main.cpp@0:bf0e4c6e1f4d, 2016-03-09 (annotated)
- Committer:
- noutram
- Date:
- Wed Mar 09 12:47:16 2016 +0000
- Revision:
- 0:bf0e4c6e1f4d
- Child:
- 2:23bd19542152
Lab version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:bf0e4c6e1f4d | 1 | #include "mbed.h" |
noutram | 0:bf0e4c6e1f4d | 2 | |
noutram | 0:bf0e4c6e1f4d | 3 | void sw1TimeOutHandler(); |
noutram | 0:bf0e4c6e1f4d | 4 | void sw1RisingEdge(); |
noutram | 0:bf0e4c6e1f4d | 5 | void sw1FallingEdge(); |
noutram | 0:bf0e4c6e1f4d | 6 | |
noutram | 0:bf0e4c6e1f4d | 7 | |
noutram | 0:bf0e4c6e1f4d | 8 | #define EDGE_RISEN 1 |
noutram | 0:bf0e4c6e1f4d | 9 | #define EDGE_FALLEN 0 |
noutram | 0:bf0e4c6e1f4d | 10 | |
noutram | 0:bf0e4c6e1f4d | 11 | //Global Objects |
noutram | 0:bf0e4c6e1f4d | 12 | DigitalOut red_led(D7); |
noutram | 0:bf0e4c6e1f4d | 13 | DigitalOut green_led(D5); |
noutram | 0:bf0e4c6e1f4d | 14 | InterruptIn sw1(D4); |
noutram | 0:bf0e4c6e1f4d | 15 | InterruptIn sw2(D3); |
noutram | 0:bf0e4c6e1f4d | 16 | |
noutram | 0:bf0e4c6e1f4d | 17 | Timeout sw1TimeOut; //Used to prevent switch bounce |
noutram | 0:bf0e4c6e1f4d | 18 | int sw1State = EDGE_FALLEN; //Initial state for switch 1 |
noutram | 0:bf0e4c6e1f4d | 19 | |
noutram | 0:bf0e4c6e1f4d | 20 | //Interrupt service routine for handling the timeout |
noutram | 0:bf0e4c6e1f4d | 21 | void sw1TimeOutHandler() { |
noutram | 0:bf0e4c6e1f4d | 22 | sw1TimeOut.detach(); //Stop the timeout counter firing |
noutram | 0:bf0e4c6e1f4d | 23 | |
noutram | 0:bf0e4c6e1f4d | 24 | //Which event does this follow? |
noutram | 0:bf0e4c6e1f4d | 25 | switch (sw1State) { |
noutram | 0:bf0e4c6e1f4d | 26 | case EDGE_RISEN: |
noutram | 0:bf0e4c6e1f4d | 27 | sw1.fall(&sw1FallingEdge); //Now wait for a falling edge |
noutram | 0:bf0e4c6e1f4d | 28 | break; |
noutram | 0:bf0e4c6e1f4d | 29 | case EDGE_FALLEN: |
noutram | 0:bf0e4c6e1f4d | 30 | sw1.rise(&sw1RisingEdge); //Now wait for a rising edge |
noutram | 0:bf0e4c6e1f4d | 31 | break; |
noutram | 0:bf0e4c6e1f4d | 32 | } //end switch |
noutram | 0:bf0e4c6e1f4d | 33 | } |
noutram | 0:bf0e4c6e1f4d | 34 | |
noutram | 0:bf0e4c6e1f4d | 35 | //Interrupt service routine for a rising edge (press) |
noutram | 0:bf0e4c6e1f4d | 36 | void sw1RisingEdge() { |
noutram | 0:bf0e4c6e1f4d | 37 | sw1.rise(NULL); //Disable detecting more rising edges |
noutram | 0:bf0e4c6e1f4d | 38 | sw1State = EDGE_RISEN; //Flag state |
noutram | 0:bf0e4c6e1f4d | 39 | sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout timer |
noutram | 0:bf0e4c6e1f4d | 40 | } |
noutram | 0:bf0e4c6e1f4d | 41 | |
noutram | 0:bf0e4c6e1f4d | 42 | //Interrupt service routive for SW1 falling edge (release) |
noutram | 0:bf0e4c6e1f4d | 43 | void sw1FallingEdge() { |
noutram | 0:bf0e4c6e1f4d | 44 | sw1.fall(NULL); //Disable this interrupt |
noutram | 0:bf0e4c6e1f4d | 45 | red_led = !red_led; //Toggle LED |
noutram | 0:bf0e4c6e1f4d | 46 | sw1State = EDGE_FALLEN; //Flag state |
noutram | 0:bf0e4c6e1f4d | 47 | sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout counter |
noutram | 0:bf0e4c6e1f4d | 48 | } |
noutram | 0:bf0e4c6e1f4d | 49 | |
noutram | 0:bf0e4c6e1f4d | 50 | //Main - only has to initialise and sleep |
noutram | 0:bf0e4c6e1f4d | 51 | int main() { |
noutram | 0:bf0e4c6e1f4d | 52 | //Initial logging message |
noutram | 0:bf0e4c6e1f4d | 53 | puts("START"); |
noutram | 0:bf0e4c6e1f4d | 54 | |
noutram | 0:bf0e4c6e1f4d | 55 | //Initial state |
noutram | 0:bf0e4c6e1f4d | 56 | red_led = 0; //Set RED LED to OFF |
noutram | 0:bf0e4c6e1f4d | 57 | green_led = 1; |
noutram | 0:bf0e4c6e1f4d | 58 | |
noutram | 0:bf0e4c6e1f4d | 59 | //Configure interrupts, wait for first rising edge |
noutram | 0:bf0e4c6e1f4d | 60 | sw1.rise(&sw1RisingEdge); |
noutram | 0:bf0e4c6e1f4d | 61 | |
noutram | 0:bf0e4c6e1f4d | 62 | //Main Polling Loop |
noutram | 0:bf0e4c6e1f4d | 63 | while (true) { |
noutram | 0:bf0e4c6e1f4d | 64 | |
noutram | 0:bf0e4c6e1f4d | 65 | //Put CPU back to sleep |
noutram | 0:bf0e4c6e1f4d | 66 | sleep(); |
noutram | 0:bf0e4c6e1f4d | 67 | |
noutram | 0:bf0e4c6e1f4d | 68 | //You can ONLY reach this point if an ISR wakes the CPU |
noutram | 0:bf0e4c6e1f4d | 69 | puts("ISR just woke the MPU"); |
noutram | 0:bf0e4c6e1f4d | 70 | |
noutram | 0:bf0e4c6e1f4d | 71 | } //end while |
noutram | 0:bf0e4c6e1f4d | 72 | |
noutram | 0:bf0e4c6e1f4d | 73 | } |