Demonstration solution of the switch-problem using interrupts

Fork of Task330_ISR by University of Plymouth - Stages 1, 2 and 3

Committer:
noutram
Date:
Wed Sep 18 10:52:31 2019 +0000
Revision:
2:88f3c172e566
Parent:
1:de866e4249b0
2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:2a35dbda8863 1 #include "mbed.h"
noutram 0:2a35dbda8863 2 #include "SwitchManager.hpp"
noutram 0:2a35dbda8863 3
noutram 0:2a35dbda8863 4 #define N 1000000
noutram 0:2a35dbda8863 5 #define RELEASED 0
noutram 0:2a35dbda8863 6 #define PRESSED 1
noutram 0:2a35dbda8863 7
noutram 0:2a35dbda8863 8 //Function prototypes
noutram 0:2a35dbda8863 9 void isr1();
noutram 0:2a35dbda8863 10 void isr2();
noutram 0:2a35dbda8863 11 void isrTick();
noutram 0:2a35dbda8863 12
noutram 0:2a35dbda8863 13 //Hardware objects
noutram 0:2a35dbda8863 14 DigitalOut red_led(PE_15); //CountUp is in its critical section
noutram 0:2a35dbda8863 15 DigitalOut yellow_led(PB_10); //CountDown is in its critical section
noutram 0:2a35dbda8863 16 DigitalOut green_led(PB_11); //counter != 0
noutram 0:2a35dbda8863 17 DigitalOut onboardLED(LED1);
noutram 0:2a35dbda8863 18
noutram 0:2a35dbda8863 19 InterruptIn sw1(PE_12);
noutram 0:2a35dbda8863 20 InterruptIn sw2(PE_14);
noutram 0:2a35dbda8863 21 Ticker yellowTick;
noutram 0:2a35dbda8863 22
noutram 0:2a35dbda8863 23 void toggleYellowISR() {
noutram 0:2a35dbda8863 24 yellow_led = !yellow_led;
noutram 0:2a35dbda8863 25 }
noutram 0:2a35dbda8863 26
noutram 0:2a35dbda8863 27 int main() {
noutram 0:2a35dbda8863 28
noutram 0:2a35dbda8863 29 //Light up
noutram 0:2a35dbda8863 30 red_led = 1;
noutram 0:2a35dbda8863 31 yellow_led = 1;
noutram 0:2a35dbda8863 32 green_led = 1;
noutram 0:2a35dbda8863 33 onboardLED = 0;
noutram 0:2a35dbda8863 34
noutram 0:2a35dbda8863 35 //Interrupt controlled red led using sw1
noutram 0:2a35dbda8863 36 SwitchManager sm1(sw1, red_led);
noutram 0:2a35dbda8863 37
noutram 0:2a35dbda8863 38 //Interrupt controlled red led using sw2
noutram 0:2a35dbda8863 39 SwitchManager sm2(sw2, green_led);
noutram 0:2a35dbda8863 40
noutram 0:2a35dbda8863 41 //Simple ticker
noutram 0:2a35dbda8863 42 yellowTick.attach(&toggleYellowISR, 0.5);
noutram 0:2a35dbda8863 43
noutram 0:2a35dbda8863 44 //Now loop forever
noutram 0:2a35dbda8863 45 while(1) {
noutram 0:2a35dbda8863 46 sleep();
noutram 2:88f3c172e566 47 yellow_led = !yellow_led;
noutram 0:2a35dbda8863 48 };
noutram 0:2a35dbda8863 49 }