Task 5.2.2 Solution

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
noutram
Date:
Thu Sep 24 12:31:29 2015 +0000
Commit message:
Initial version 24-09-2015

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 24 12:31:29 2015 +0000
@@ -0,0 +1,88 @@
+#include "mbed.h"
+
+//Global objects
+DigitalOut onboardLed(LED1);
+DigitalOut redLED(D7);
+DigitalOut yellowLED(D6);
+DigitalOut greenLED(D5);
+
+//Function prototypes (ISR)
+void doRedPWM();
+void doYellowPWM();
+void doGreenPWM();
+
+//One-shot timers
+Timeout tRed;
+Timeout tYellow;
+Timeout tGreen;
+
+//Interrupt Service Routine Flags
+volatile int redISRFlag = 0;
+volatile int yellowISRFlag = 0;
+volatile int greenISRFlag = 0;
+
+//ON and OFF times
+// RED 9:1
+float TRedON =  0.009;
+float TRedOFF = 0.001;
+// YELLOW 1:1
+float TYellowON =  0.001; 
+float TYellowOFF = 0.001;
+// GREEN 1:9 - nearly off
+float TGreenON =  0.001;
+float TGreenOFF = 0.009;
+
+int main() {
+    //Initialise the LEDs
+    redLED = 0;
+    yellowLED = 0;
+    greenLED = 0;
+
+    
+    //Initialise timers (oneshot)
+    tRed.attach(doRedPWM, TRedOFF);
+    tYellow.attach(doYellowPWM, TYellowOFF);
+    tGreen.attach(doGreenPWM, TGreenOFF);
+    
+    while (1) {
+        //Sleep and wait for an interrupt
+        sleep();
+        
+        //Chech which timer(s) went off
+        if (redISRFlag == 1) {
+            redISRFlag = 0; //Reset ISR flag
+            float t = (redLED==0) ? TRedOFF : TRedON;
+            tRed.attach(doRedPWM, t);  //Reset timer
+        }
+        if (yellowISRFlag == 1) {
+            yellowISRFlag = 0;
+            float t = (yellowLED==0) ? TYellowOFF : TYellowON;
+            tYellow.attach(doYellowPWM, t);  
+        }
+        if (greenISRFlag == 1) {
+            greenISRFlag = 0;
+            float t = (greenLED==0) ? TGreenOFF : TGreenON;
+            tGreen.attach(doGreenPWM, t);  
+        }                
+
+    }
+}
+
+void doRedPWM()
+{
+    //Toggle LED
+    redLED = !redLED;
+    //Flag that interrupt has fired
+    redISRFlag = 1;
+    
+}
+void doYellowPWM()
+{
+    yellowLED = !yellowLED;
+    yellowISRFlag = 1;
+}
+void doGreenPWM()
+{
+    greenLED = !greenLED;
+    greenISRFlag = 1; 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 24 12:31:29 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file