Nicholas Outram
/
Task522Solution
Task 5.2.2 Solution
main.cpp@1:69d2dd81cd48, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:55:53 2017 +0000
- Revision:
- 1:69d2dd81cd48
- Parent:
- 0:4e6cbb427cd7
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:4e6cbb427cd7 | 1 | #include "mbed.h" |
noutram | 0:4e6cbb427cd7 | 2 | |
noutram | 0:4e6cbb427cd7 | 3 | //Global objects |
noutram | 0:4e6cbb427cd7 | 4 | DigitalOut onboardLed(LED1); |
noutram | 0:4e6cbb427cd7 | 5 | DigitalOut redLED(D7); |
noutram | 0:4e6cbb427cd7 | 6 | DigitalOut yellowLED(D6); |
noutram | 0:4e6cbb427cd7 | 7 | DigitalOut greenLED(D5); |
noutram | 0:4e6cbb427cd7 | 8 | |
noutram | 0:4e6cbb427cd7 | 9 | //Function prototypes (ISR) |
noutram | 0:4e6cbb427cd7 | 10 | void doRedPWM(); |
noutram | 0:4e6cbb427cd7 | 11 | void doYellowPWM(); |
noutram | 0:4e6cbb427cd7 | 12 | void doGreenPWM(); |
noutram | 0:4e6cbb427cd7 | 13 | |
noutram | 0:4e6cbb427cd7 | 14 | //One-shot timers |
noutram | 0:4e6cbb427cd7 | 15 | Timeout tRed; |
noutram | 0:4e6cbb427cd7 | 16 | Timeout tYellow; |
noutram | 0:4e6cbb427cd7 | 17 | Timeout tGreen; |
noutram | 0:4e6cbb427cd7 | 18 | |
noutram | 0:4e6cbb427cd7 | 19 | //Interrupt Service Routine Flags |
noutram | 0:4e6cbb427cd7 | 20 | volatile int redISRFlag = 0; |
noutram | 0:4e6cbb427cd7 | 21 | volatile int yellowISRFlag = 0; |
noutram | 0:4e6cbb427cd7 | 22 | volatile int greenISRFlag = 0; |
noutram | 0:4e6cbb427cd7 | 23 | |
noutram | 0:4e6cbb427cd7 | 24 | //ON and OFF times |
noutram | 0:4e6cbb427cd7 | 25 | // RED 9:1 |
noutram | 0:4e6cbb427cd7 | 26 | float TRedON = 0.009; |
noutram | 0:4e6cbb427cd7 | 27 | float TRedOFF = 0.001; |
noutram | 0:4e6cbb427cd7 | 28 | // YELLOW 1:1 |
noutram | 0:4e6cbb427cd7 | 29 | float TYellowON = 0.001; |
noutram | 0:4e6cbb427cd7 | 30 | float TYellowOFF = 0.001; |
noutram | 0:4e6cbb427cd7 | 31 | // GREEN 1:9 - nearly off |
noutram | 0:4e6cbb427cd7 | 32 | float TGreenON = 0.001; |
noutram | 0:4e6cbb427cd7 | 33 | float TGreenOFF = 0.009; |
noutram | 0:4e6cbb427cd7 | 34 | |
noutram | 0:4e6cbb427cd7 | 35 | int main() { |
noutram | 0:4e6cbb427cd7 | 36 | //Initialise the LEDs |
noutram | 0:4e6cbb427cd7 | 37 | redLED = 0; |
noutram | 0:4e6cbb427cd7 | 38 | yellowLED = 0; |
noutram | 0:4e6cbb427cd7 | 39 | greenLED = 0; |
noutram | 0:4e6cbb427cd7 | 40 | |
noutram | 0:4e6cbb427cd7 | 41 | |
noutram | 0:4e6cbb427cd7 | 42 | //Initialise timers (oneshot) |
noutram | 0:4e6cbb427cd7 | 43 | tRed.attach(doRedPWM, TRedOFF); |
noutram | 0:4e6cbb427cd7 | 44 | tYellow.attach(doYellowPWM, TYellowOFF); |
noutram | 0:4e6cbb427cd7 | 45 | tGreen.attach(doGreenPWM, TGreenOFF); |
noutram | 0:4e6cbb427cd7 | 46 | |
noutram | 0:4e6cbb427cd7 | 47 | while (1) { |
noutram | 0:4e6cbb427cd7 | 48 | //Sleep and wait for an interrupt |
noutram | 0:4e6cbb427cd7 | 49 | sleep(); |
noutram | 0:4e6cbb427cd7 | 50 | |
noutram | 0:4e6cbb427cd7 | 51 | //Chech which timer(s) went off |
noutram | 0:4e6cbb427cd7 | 52 | if (redISRFlag == 1) { |
noutram | 0:4e6cbb427cd7 | 53 | redISRFlag = 0; //Reset ISR flag |
noutram | 0:4e6cbb427cd7 | 54 | float t = (redLED==0) ? TRedOFF : TRedON; |
noutram | 0:4e6cbb427cd7 | 55 | tRed.attach(doRedPWM, t); //Reset timer |
noutram | 0:4e6cbb427cd7 | 56 | } |
noutram | 0:4e6cbb427cd7 | 57 | if (yellowISRFlag == 1) { |
noutram | 0:4e6cbb427cd7 | 58 | yellowISRFlag = 0; |
noutram | 0:4e6cbb427cd7 | 59 | float t = (yellowLED==0) ? TYellowOFF : TYellowON; |
noutram | 0:4e6cbb427cd7 | 60 | tYellow.attach(doYellowPWM, t); |
noutram | 0:4e6cbb427cd7 | 61 | } |
noutram | 0:4e6cbb427cd7 | 62 | if (greenISRFlag == 1) { |
noutram | 0:4e6cbb427cd7 | 63 | greenISRFlag = 0; |
noutram | 0:4e6cbb427cd7 | 64 | float t = (greenLED==0) ? TGreenOFF : TGreenON; |
noutram | 0:4e6cbb427cd7 | 65 | tGreen.attach(doGreenPWM, t); |
noutram | 0:4e6cbb427cd7 | 66 | } |
noutram | 0:4e6cbb427cd7 | 67 | |
noutram | 0:4e6cbb427cd7 | 68 | } |
noutram | 0:4e6cbb427cd7 | 69 | } |
noutram | 0:4e6cbb427cd7 | 70 | |
noutram | 0:4e6cbb427cd7 | 71 | void doRedPWM() |
noutram | 0:4e6cbb427cd7 | 72 | { |
noutram | 0:4e6cbb427cd7 | 73 | //Toggle LED |
noutram | 0:4e6cbb427cd7 | 74 | redLED = !redLED; |
noutram | 0:4e6cbb427cd7 | 75 | //Flag that interrupt has fired |
noutram | 0:4e6cbb427cd7 | 76 | redISRFlag = 1; |
noutram | 0:4e6cbb427cd7 | 77 | |
noutram | 0:4e6cbb427cd7 | 78 | } |
noutram | 0:4e6cbb427cd7 | 79 | void doYellowPWM() |
noutram | 0:4e6cbb427cd7 | 80 | { |
noutram | 0:4e6cbb427cd7 | 81 | yellowLED = !yellowLED; |
noutram | 0:4e6cbb427cd7 | 82 | yellowISRFlag = 1; |
noutram | 0:4e6cbb427cd7 | 83 | } |
noutram | 0:4e6cbb427cd7 | 84 | void doGreenPWM() |
noutram | 0:4e6cbb427cd7 | 85 | { |
noutram | 0:4e6cbb427cd7 | 86 | greenLED = !greenLED; |
noutram | 0:4e6cbb427cd7 | 87 | greenISRFlag = 1; |
noutram | 0:4e6cbb427cd7 | 88 | } |