Lab version

Fork of Task327 by Nicholas Outram

Committer:
noutram
Date:
Wed Oct 11 16:52:31 2017 +0000
Revision:
2:23bd19542152
Parent:
0:bf0e4c6e1f4d
Child:
3:384a8f9726ac
Demo for the F429

Who changed what in which revision?

UserRevisionLine numberNew 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 #define EDGE_RISEN 1
noutram 0:bf0e4c6e1f4d 8 #define EDGE_FALLEN 0
noutram 0:bf0e4c6e1f4d 9
noutram 0:bf0e4c6e1f4d 10 //Global Objects
noutram 2:23bd19542152 11 DigitalOut led(LED1);
noutram 2:23bd19542152 12 InterruptIn sw1(USER_BUTTON);
noutram 0:bf0e4c6e1f4d 13 Timeout sw1TimeOut; //Used to prevent switch bounce
noutram 0:bf0e4c6e1f4d 14 int sw1State = EDGE_FALLEN; //Initial state for switch 1
noutram 0:bf0e4c6e1f4d 15
noutram 0:bf0e4c6e1f4d 16 //Interrupt service routine for handling the timeout
noutram 0:bf0e4c6e1f4d 17 void sw1TimeOutHandler() {
noutram 0:bf0e4c6e1f4d 18 //Which event does this follow?
noutram 0:bf0e4c6e1f4d 19 switch (sw1State) {
noutram 0:bf0e4c6e1f4d 20 case EDGE_RISEN:
noutram 0:bf0e4c6e1f4d 21 sw1.fall(&sw1FallingEdge); //Now wait for a falling edge
noutram 0:bf0e4c6e1f4d 22 break;
noutram 0:bf0e4c6e1f4d 23 case EDGE_FALLEN:
noutram 0:bf0e4c6e1f4d 24 sw1.rise(&sw1RisingEdge); //Now wait for a rising edge
noutram 0:bf0e4c6e1f4d 25 break;
noutram 0:bf0e4c6e1f4d 26 } //end switch
noutram 0:bf0e4c6e1f4d 27 }
noutram 0:bf0e4c6e1f4d 28
noutram 0:bf0e4c6e1f4d 29 //Interrupt service routine for a rising edge (press)
noutram 0:bf0e4c6e1f4d 30 void sw1RisingEdge() {
noutram 0:bf0e4c6e1f4d 31 sw1.rise(NULL); //Disable detecting more rising edges
noutram 0:bf0e4c6e1f4d 32 sw1State = EDGE_RISEN; //Flag state
noutram 0:bf0e4c6e1f4d 33 }
noutram 0:bf0e4c6e1f4d 34
noutram 0:bf0e4c6e1f4d 35 //Interrupt service routive for SW1 falling edge (release)
noutram 0:bf0e4c6e1f4d 36 void sw1FallingEdge() {
noutram 0:bf0e4c6e1f4d 37 sw1.fall(NULL); //Disable this interrupt
noutram 2:23bd19542152 38 led = !led; //Toggle LED
noutram 0:bf0e4c6e1f4d 39 sw1State = EDGE_FALLEN; //Flag state
noutram 0:bf0e4c6e1f4d 40 }
noutram 0:bf0e4c6e1f4d 41
noutram 0:bf0e4c6e1f4d 42 //Main - only has to initialise and sleep
noutram 0:bf0e4c6e1f4d 43 int main() {
noutram 0:bf0e4c6e1f4d 44 //Initial logging message
noutram 0:bf0e4c6e1f4d 45 puts("START");
noutram 0:bf0e4c6e1f4d 46
noutram 0:bf0e4c6e1f4d 47 //Configure interrupts, wait for first rising edge
noutram 0:bf0e4c6e1f4d 48 sw1.rise(&sw1RisingEdge);
noutram 0:bf0e4c6e1f4d 49
noutram 0:bf0e4c6e1f4d 50 //Main Polling Loop
noutram 0:bf0e4c6e1f4d 51 while (true) {
noutram 2:23bd19542152 52 //Set LED to OFF
noutram 2:23bd19542152 53 led = 0;
noutram 0:bf0e4c6e1f4d 54
noutram 0:bf0e4c6e1f4d 55 //Put CPU back to sleep
noutram 2:23bd19542152 56 sleep(); //woken by rising edge
noutram 2:23bd19542152 57 puts("Rising");
noutram 2:23bd19542152 58 wait(0.2);
noutram 2:23bd19542152 59 sw1TimeOutHandler();
noutram 2:23bd19542152 60 sleep(); //Falling edge
noutram 2:23bd19542152 61 puts("Falling");
noutram 2:23bd19542152 62 wait(0.2);
noutram 2:23bd19542152 63 sw1TimeOutHandler();
noutram 0:bf0e4c6e1f4d 64
noutram 0:bf0e4c6e1f4d 65 //You can ONLY reach this point if an ISR wakes the CPU
noutram 2:23bd19542152 66 for (int n=0; n<10; n++) {
noutram 2:23bd19542152 67 led = !led;
noutram 2:23bd19542152 68 wait(0.1);
noutram 2:23bd19542152 69 }
noutram 0:bf0e4c6e1f4d 70
noutram 0:bf0e4c6e1f4d 71 } //end while
noutram 0:bf0e4c6e1f4d 72
noutram 0:bf0e4c6e1f4d 73 }