Lab version

Fork of Task326 by Nicholas Outram

Committer:
noutram
Date:
Thu Sep 12 14:47:03 2019 +0000
Revision:
2:a5a574cd9474
Parent:
0:57de85b71119
2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:57de85b71119 1 #include "mbed.h"
noutram 0:57de85b71119 2
noutram 2:a5a574cd9474 3 #ifdef TARGET_NUCLEO_F429ZI
noutram 2:a5a574cd9474 4 //Uncomment the following line to use onboard LEDs and Switch (F429ZI only)
noutram 2:a5a574cd9474 5 //#define USEONBOARD
noutram 2:a5a574cd9474 6 #endif
noutram 2:a5a574cd9474 7
noutram 0:57de85b71119 8 //A slight improvement - however, the pressing of the switch still registers
noutram 0:57de85b71119 9 //spurious falling edges!
noutram 0:57de85b71119 10
noutram 0:57de85b71119 11 void sw1TimeOutHandler();
noutram 0:57de85b71119 12 void sw1FallingEdge();
noutram 0:57de85b71119 13
noutram 0:57de85b71119 14
noutram 0:57de85b71119 15 #define EDGE_RISEN 1
noutram 0:57de85b71119 16 #define EDGE_FALLEN 0
noutram 0:57de85b71119 17
noutram 0:57de85b71119 18 //Global Objects
noutram 2:a5a574cd9474 19 #ifdef USEONBOARD
noutram 2:a5a574cd9474 20 DigitalOut red_led(LED3);
noutram 2:a5a574cd9474 21 DigitalOut green_led(LED1);
noutram 2:a5a574cd9474 22 InterruptIn sw1(USER_BUTTON);
noutram 2:a5a574cd9474 23 #else
noutram 0:57de85b71119 24 DigitalOut red_led(D7);
noutram 0:57de85b71119 25 DigitalOut green_led(D5);
noutram 0:57de85b71119 26 InterruptIn sw1(D4);
noutram 2:a5a574cd9474 27 #endif
noutram 0:57de85b71119 28
noutram 0:57de85b71119 29 Timeout sw1TimeOut; //Used to prevent switch bounce
noutram 2:a5a574cd9474 30 bool timerFired = false;
noutram 2:a5a574cd9474 31 bool switchPressed = false;
noutram 0:57de85b71119 32
noutram 0:57de85b71119 33 //Interrupt service routine for handling the timeout
noutram 0:57de85b71119 34 void sw1TimeOutHandler() {
noutram 2:a5a574cd9474 35 timerFired = true;
noutram 0:57de85b71119 36 sw1TimeOut.detach(); //Stop the timeout counter firing
noutram 0:57de85b71119 37 sw1.fall(&sw1FallingEdge); //Now wait for a falling edge
noutram 0:57de85b71119 38 }
noutram 0:57de85b71119 39
noutram 0:57de85b71119 40 //Interrupt service routive for SW1 falling edge (release)
noutram 0:57de85b71119 41 void sw1FallingEdge() {
noutram 2:a5a574cd9474 42 switchPressed = true;
noutram 0:57de85b71119 43 sw1.fall(NULL); //Disable this interrupt
noutram 0:57de85b71119 44 red_led = !red_led; //Toggle LED
noutram 0:57de85b71119 45 sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout counter
noutram 0:57de85b71119 46 }
noutram 0:57de85b71119 47
noutram 0:57de85b71119 48 //Main - only has to initialise and sleep
noutram 0:57de85b71119 49 int main() {
noutram 0:57de85b71119 50
noutram 0:57de85b71119 51 //Initial state
noutram 0:57de85b71119 52 red_led = 0; //Set RED LED to OFF
noutram 0:57de85b71119 53 green_led = 1;
noutram 0:57de85b71119 54
noutram 0:57de85b71119 55 //Configure interrupts, wait for first rising edge
noutram 0:57de85b71119 56 sw1.fall(&sw1FallingEdge);
noutram 0:57de85b71119 57
noutram 0:57de85b71119 58 //Main Polling Loop
noutram 0:57de85b71119 59 while (true) {
noutram 0:57de85b71119 60
noutram 0:57de85b71119 61 //Put CPU back to sleep
noutram 0:57de85b71119 62 sleep();
noutram 0:57de85b71119 63
noutram 2:a5a574cd9474 64 //You can ONLY reach this point if an ISR wakes the CPU - now there are two
noutram 2:a5a574cd9474 65 if (timerFired == true) {
noutram 2:a5a574cd9474 66 timerFired = false; //Reset flag
noutram 2:a5a574cd9474 67 }
noutram 2:a5a574cd9474 68 if (switchPressed == true) {
noutram 2:a5a574cd9474 69 switchPressed = false; //Reset flag
noutram 2:a5a574cd9474 70 green_led = !green_led;
noutram 2:a5a574cd9474 71 }
noutram 0:57de85b71119 72 } //end while
noutram 0:57de85b71119 73
noutram 0:57de85b71119 74 }