chad

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Revision:
17:bc25d5f47bab
Parent:
16:bebcc7d24f3e
--- a/tasks.cpp	Fri Mar 10 18:50:37 2017 +0000
+++ b/tasks.cpp	Mon Mar 13 14:51:55 2017 +0000
@@ -1,192 +1,243 @@
 #include "main.h"
 
-// Task 1: Measure input frequency
+////////////////////////////////////////////////////////////////////////////////
+//
+// Task 1: Measure input frequency 
+// 24ms max
+//
 void Task1(void)
 {
+    //T5.reset();
+    //T5.start();
+    
+    timer.reset();                  // Timer used to time input frequency pulses
+    
     //  Wait for Posedge
-    if (Fq == 0){
-        while (Fq == 0){
-            wait_us (1);
-            }
-        //  Posedge detected, log start time
-        if (Fq == 1){
+    if (Fq == 0){                   // Case for starting on low pulse
+        while (Fq == 0){            // Wait for high pulse 
+            wait_us (1);            
+        } 
+        if (Fq == 1){               // Start timer on high pulse
             timer.start ();
         }
-            
-        //  Wait for Negedge
-        while (Fq == 1){
-            wait_us (1);
+        while (Fq == 1){            // Wait for low pulse
+            wait_us (1);            
         }
-        //  Negedge detected, log end time
-        if (Fq == 0){
+        if (Fq == 0){               // Stop timer on low pulse
             timer.stop ();
         }
     }
     
-    else if (Fq == 1){
-        
-        while (Fq == 1){
+    else if (Fq == 1){              // Case for starting on high pulse
+        while (Fq == 1){            // Wait for low pulse
             wait_us (1);
-            }
-        //  Posedge detected, log start time
-        if (Fq == 0){
+        }
+        if (Fq == 0){               // Start timer on low pulse
             timer.start ();
         }
-            
-        //  Wait for Negedge
-        while (Fq == 0){
-            wait_us (1);
+        while (Fq == 0){            // Wait for high pulse
+            wait_us (1);            
         }
-        //  Negedge detected, log end time
-        if (Fq == 1){
+        if (Fq == 1){               // Stop timer on high pulse
             timer.stop ();
         }
     }
-    
-    //timer.stop(); // Stop counting when signal changes*/
-
-    lcd->locate(0,0);
-    lcd->printf("F: %.0f",(1/(2*timer.read())));
-    
-    timer.reset();
-}
 
-void PosEdge(void)
-{
-    // Sub will end when high, Timer will start.
-    while(FqIn == 0) {
-        wait_us(SampFreq);
-    }
-}
+    freq = (1/(2*timer.read()));    // Calc frequency from timer
+    
+    //T5.stop();
+    
+    //lcd->locate(0,0);
+    //lcd->printf("F: %4.0f",freq);
 
-void NegEdge(void)
-{
-    // Sub will end when low, Timer will start.
-    while(FqIn == 1) {
-        wait_us(SampFreq);
-    }
+    //lcd->locate(1,0);
+    //lcd->printf("%f",T5.read());
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Read Digital input switch
+// Read Digital input switch        
+// 3us max
 //
 void Task2(void)
 {
-    //switch_state = DSIn == 1 ? 1: 0;
-    if(DSIn == 1)
+    //T5.reset();
+    //T5.start();
+
+    if(DS == 1)                     // If switch 1 is high set switch_state register high
         switch_state = 1;
-    else if(DSIn == 0)
+    else if(DS == 0)                // If switch 1 is low set switch_state register low 
         switch_state = 0;
+        
+    //T5.stop();
+    
+    //lcd->locate(0,0);
+    //lcd->printf("Sw1: %d",switch_state);
+    
+    //lcd->locate(1,0);
+    //lcd->printf("%f",(1000*T5.read()));
 }
 
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Output watchdog timer pulse
+// Output watchdog timer pulse          6.1ms max
 //
 void Task3(void)
 {
-    WD_pulse = 1;       // Pulse High
+    //T5.reset();
+    //T5.start();
+    
+    WD_pulse = 1;       // WatchDog pulse High
     wait_us(WD);        // Leave high for specified length
-    WD_pulse = 0;
+    WD_pulse = 0;       // WatchDog pulse low
+    
+    //T5.stop();
+    
+    //lcd->locate(1,0);
+    //lcd->printf("%f",T5.read());
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Read and filter 2 analogue inputs
+// Read and filter 2 analogue inputs           
 // 3.3V max
+// 55us
 //
 void Task4(void)
 {
-    A1_val = 0;
-    A2_val = 0;
+    //T5.reset();
+    //T5.start();
+    
+    A1_val = 0;         // Reset analogue 1 variable
+    A2_val = 0;         // Reset analogue 2 variable
 
-    A1_in = (A1_in * 3.3);
-    A2_in = (A2_in * 3.3);
+    A1_In = (A1_ * 3.3);            //  Analogue input changed from 0->1 to 0-> 3.3
+    A2_In = (A2_ * 3.3);
 
-    for(int i=0; i<3; i++) {
-        A1_val = A1_val + A1_in;
-        A2_val = A2_val + A2_in;
+    for(int i=0; i<4; i++) {        // Take three readings for each input 
+        A1_val = A1_val + A1_In;
+        A2_val = A2_val + A2_In;
     }
 
-    A1_val = (A1_val / 3);
-    A2_val = (A2_val / 3);
+    A1_val = (A1_val / 4);          // Final analogue 1 input. Average taken of 3 readings
+    A2_val = (A2_val / 4);          // Final analogue 2 input. Average taken of 3 readings
 
+    //T5.stop();
+    
     //lcd->locate(0,0);
     //lcd->printf("A1:%1.2f A2:%1.2f",A1_val,A2_val);
-
+    
+    //lcd->locate(1,0);
+    //lcd->printf("%f",T5.read());
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Display Frequency, digital and filterd analogue values on LCD
+// Display Frequency, digital and filterd analogue values on LCD 
 //
-void Task5(void)
+void Task5a(void)                   // 25.5 ms  
 {
-    T5.reset();
-    T5.start();
+    T5.reset();                     // Reset timer for task 5
+    T5.start();                     // Start task 5 timer
+                 
+    lcd->locate(0,0);               // First character set to top left
+    // Print freqency, Switch 1 and Error code to LCD
+    lcd->printf("%4.f %d %d",freq,switch_state,error_code);     
+        
+    T5.stop();                      // Stop task 5 timer
 
-    lcd->cls();
-    lcd->locate(0,0);
-    lcd->printf("F:%d S:%d",freq,switch_state);
-    lcd->locate(1,0);
-    lcd->printf("A1:%1.1f A2:%1.1f E%d",A1_val,A2_val,error_code);
+    lcd->locate(1,9);               // Printing time to complete task 5 (ms)
+    lcd->printf("%.1f",(T5.read()*1000));
+}
 
-    //lcd->printf("F%d",freq);
-    //lcd->printf("test");
+void Task5b(void)                   // 22.7 ms
+{
+    T5.reset();                     // Reset timer for task 5
+    T5.start();                     // Start task 5 timer
+    
+    lcd->locate(1,0);               // First character set to bottom left
+    // Print Analogue values to LCD
+    lcd->printf("%1.1f %1.1f",A1_val,A2_val);
 
-    T5.stop();
+    T5.stop();                      // Stop task 5 timer
 
-    lcd->locate(0,8);
-    lcd->printf(":%.1f",(T5.read()*1000));
+    lcd->locate(1,9);               // Printing time to complete task 5 (ms)
+    lcd->printf("%.1f",(T5.read()*1000));
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Error check
-// If switch_1 is ON & (average_analogue_in_1 > average_analogue_in_2) error code 3
+// Error check          
+// 3us 
+// 
 // Else error code 0
 //
 void Task6(void)
 {
-    if((A1_val > A2_val))
+    //T5.reset();
+    //T5.start();
+    
+    // If switch_1 is ON & (average_analogue_in_1 > average_analogue_in_2) error code 3
+    if(switch_state == 1 && (A1_val > A2_val))  
         error_code = 3;
-    else
+    else                    // If conditions not met error code is 0
         error_code = 0;
 
-
+    //T5.stop();
 
-    //lcd->cls();
+    //lcd->locate(0,0);
+    //lcd->printf("Error: %d", error_code);
+    
     //lcd->locate(1,0);
-    //lcd->printf("Error: %d", error_code);
-
-    wait_us(100);
+    //lcd->printf("%f",T5.read());
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
 // Log frequency, digital and filtered analogue values to uSD
+// 21ms
 //
 void Task7(void)
 {
-    logcount++;
-    fprintf(fp,"Log: %d,Freq: %fHz,Digital_In: %d,Analogue_1: %f,Analogue_2: %f\n",logcount,freq,switch_state,A1_val,A2_val);
-
+    //T5.reset();
+    //T5.start();
+    
+    logcount++;             // Increment logcount
+    // Saving logcount, frequency, Switch 1 and analogue input values to .txt 
+    fprintf(fp,"%d,%fHz,%d,%fV,%fV\n",logcount,freq,switch_state,A1_val,A2_val);
+    
+    //T5.stop();
+    
+    //lcd->locate(1,0);
+    //lcd->printf("%f",T5.read());
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
 // Shutdown on switch
+// 3us when DS_s == 0
+// N/A when DS_s == 1
 //
 void Task8()
 {
-    if(DS_sIn == 1) {
-        ticker.detach();
-        fprintf(fp, "\nTask 5 took %dms to complete\n", T5.read_ms());
-        fprintf(fp, "\nCyclic Executive stopped\n");
-        fclose(fp);
-    } else {
+    //T5.reset();
+    //T5.start();
+    
+    if(DS_s == 1) {         // If shutdown switch is high
+        ticker.detach();    // Stop ticker
+        
+        fprintf(fp, "\nCyclic Executive stopped\n");    // Show uSD detach
+        fclose(fp);                                     // Detach uSD
+        
+        //T5.stop();
+        
+        //lcd->locate(1,0);
+        //lcd->printf("%f",T5.read());
+        
+    } 
+    else {
+        //T5.stop();
     }
 }