chad

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Revision:
14:ede0e7ed2745
Parent:
13:ad04937ca366
Child:
15:85616bc0e2ae
--- a/tasks.cpp	Tue Mar 07 22:32:06 2017 +0000
+++ b/tasks.cpp	Wed Mar 08 15:36:57 2017 +0000
@@ -1,38 +1,55 @@
 #include "main.h"
 
+////////////////////////////////////////////////////////////////////////////////
+// Subroutines
+//
+
+void PosEdge(void)
+{
+    // Sub will end when high, Timer will start.
+    while(FqIn == 0)
+    {        
+            wait_us(SampFreq);        
+    }
+}
+
+void NegEdge(void)
+{
+    // Sub will end when low, Timer will start.
+    while(FqIn == 1)
+    {
+        wait_us(SampFreq);
+    }
+}
+
 // Task 1: Measure input frequency
 void Task1(void)
 {
-    timer.reset();
+    //timer.reset();
     
-    // If the input signal is low, wait for a rising edge to start counting
     if (FqIn == 0)
     {
-        while(FqIn == 0)
+        PosEdge();          //Wait for Pos 
+        //timer.start(); // Start timer
+        while(FqIn == 1) // Keep counting as long as signal is high
+        {
             wait_us(SampFreq);
-        timer.start();      // Start timer
-        while(FqIn == 1)    // Keep counting as long as signal is high
-            wait_us(SampFreq);
+        }
     }
     
-    // If the input signal is high, wait for a falling edge to start counting
     else if (FqIn == 1)
     {
-        while(FqIn == 1)
+        NegEdge();          // Wait for Neg
+        //timer.start();      // Start timer
+        while(FqIn == 0)    // Keep counting as long as signal is high
+        {
             wait_us(SampFreq);
-        timer.start();      // Start timer
-        while(FqIn == 0)    // Keep counting as long as signal is high
-            wait_us(SampFreq);
+        }
     }
-        
-    timer.stop();               // Stop counting when signal changes
-    period = timer.read()*2; // Convert the time into a period
-    freq = 1000000/period;      // Convert the period into a frequency
-    
-    lcd->locate(1,0);
-    lcd->printf("F: %d", timer.read());
-    
-    return;
+
+    //timer.stop(); // Stop counting when signal changes
+    //period = timer.read_us()*2; // Convert the time into a period
+    freq = 1000000/period; // Convert the period into a frequency
 }
 
 
@@ -42,14 +59,10 @@
 //
 void Task2(void)
 {
-    if (DSIn == 1) 
-        switch_state = 1;
-    else if (DSIn == 0)
-        switch_state = 0;
-    else
-        switch_state = 0;
-       // switch_state = DSIn == 1 ? 1: 0;     
-    return;    
+    switch_state = DSIn == 1 ? 1: 0;     
+     
+    //lcd->locate(0,0);
+    //lcd->printf("S: %d", switch_state);
 }
 
 
@@ -62,7 +75,6 @@
     WD_pulse = 1;       // Pulse High
     wait_us(WD);        // Leave high for specified length
     WD_pulse = 0;
-    return;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -83,8 +95,7 @@
     
     A1_val = (A1_val / 3);
     A2_val = (A2_val / 3);
-    
-    return;
+
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -93,13 +104,16 @@
 // 
 void Task5(void)
 {
+    //T5.reset();
+    //T5.start();
+    
     lcd->cls();
     lcd->locate(0,0);
     //lcd->printf("F%d S%d% A1d% A2d",freq,switch_state,A1_val,A2_val); 
     lcd->printf("F%d",freq);
     //lcd->printf("test");
     
-    return;
+    //T5.stop();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -122,7 +136,6 @@
     lcd->printf("Error: %d", error_code);
     
     wait_us(100);
-    return;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -133,8 +146,7 @@
 {
     logcount++;
     fprintf(fp,"Log: %d,Freq: %dHz,Digital_In: %d,Analogue_1: %d,Analogue_2: %d\n",logcount,freq,switch_state,A1_val,A2_val); 
-    
-    return;
+
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -154,3 +166,6 @@
     }
 }
 */
+
+
+