chad

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Revision:
16:bebcc7d24f3e
Parent:
15:85616bc0e2ae
Child:
17:bc25d5f47bab
--- a/tasks.cpp	Wed Mar 08 16:17:09 2017 +0000
+++ b/tasks.cpp	Fri Mar 10 18:50:37 2017 +0000
@@ -3,50 +3,66 @@
 // Task 1: Measure input frequency
 void Task1(void)
 {
-    timer.reset();
-    
-    if (FqIn == 0)
-    {
-        PosEdge();          //Wait for Pos 
-        timer.start(); // Start timer
-        while(FqIn == 1) // Keep counting as long as signal is high
-        {
-            wait_us(SampFreq);
+    //  Wait for Posedge
+    if (Fq == 0){
+        while (Fq == 0){
+            wait_us (1);
+            }
+        //  Posedge detected, log start time
+        if (Fq == 1){
+            timer.start ();
+        }
+            
+        //  Wait for Negedge
+        while (Fq == 1){
+            wait_us (1);
+        }
+        //  Negedge detected, log end time
+        if (Fq == 0){
+            timer.stop ();
         }
     }
     
-    else if (FqIn == 1)
-    {
-        NegEdge();          // Wait for Neg
-        timer.start();      // Start timer
-        while(FqIn == 0)    // Keep counting as long as signal is high
-        {
-            wait_us(SampFreq);
+    else if (Fq == 1){
+        
+        while (Fq == 1){
+            wait_us (1);
+            }
+        //  Posedge detected, log start time
+        if (Fq == 0){
+            timer.start ();
+        }
+            
+        //  Wait for Negedge
+        while (Fq == 0){
+            wait_us (1);
+        }
+        //  Negedge detected, log end time
+        if (Fq == 1){
+            timer.stop ();
         }
     }
+    
+    //timer.stop(); // Stop counting when signal changes*/
 
-    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
+    lcd->locate(0,0);
+    lcd->printf("F: %.0f",(1/(2*timer.read())));
     
-    lcd->locate(0,0);
-    lcd->printf("F: %d",freq);
+    timer.reset();
 }
 
 void PosEdge(void)
 {
     // Sub will end when high, Timer will start.
-    while(FqIn == 0)
-    {        
-            wait_us(SampFreq);        
+    while(FqIn == 0) {
+        wait_us(SampFreq);
     }
 }
 
 void NegEdge(void)
 {
     // Sub will end when low, Timer will start.
-    while(FqIn == 1)
-    {
+    while(FqIn == 1) {
         wait_us(SampFreq);
     }
 }
@@ -57,7 +73,7 @@
 //
 void Task2(void)
 {
-    //switch_state = DSIn == 1 ? 1: 0;     
+    //switch_state = DSIn == 1 ? 1: 0;
     if(DSIn == 1)
         switch_state = 1;
     else if(DSIn == 0)
@@ -80,45 +96,50 @@
 //
 // Read and filter 2 analogue inputs
 // 3.3V max
-// 
+//
 void Task4(void)
-{ 
+{
     A1_val = 0;
     A2_val = 0;
-    
+
     A1_in = (A1_in * 3.3);
     A2_in = (A2_in * 3.3);
-    
-    for(int i=0; i<3; i++)
-    {
+
+    for(int i=0; i<3; i++) {
         A1_val = A1_val + A1_in;
-        A2_val = A2_val + A2_in; 
+        A2_val = A2_val + A2_in;
     }
-    
+
     A1_val = (A1_val / 3);
     A2_val = (A2_val / 3);
-    
-    lcd->locate(0,0);
-    lcd->printf("A1:%1.2f A2:%1.2f",A1_val,A2_val);
+
+    //lcd->locate(0,0);
+    //lcd->printf("A1:%1.2f A2:%1.2f",A1_val,A2_val);
 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Display Frequency, digital and filterd analogue values on LCD 
-// 
+// Display Frequency, digital and filterd analogue values on LCD
+//
 void Task5(void)
 {
-    //T5.reset();
-    //T5.start();
-    
+    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("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->printf("F%d",freq);
     //lcd->printf("test");
-    
-    //T5.stop();
+
+    T5.stop();
+
+    lcd->locate(0,8);
+    lcd->printf(":%.1f",(T5.read()*1000));
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -126,51 +147,49 @@
 // Error check
 // If switch_1 is ON & (average_analogue_in_1 > average_analogue_in_2) error code 3
 // Else error code 0
-// 
+//
 void Task6(void)
-{/*
-    if(switch_state == 1 && (A1_val > A2_val))
+{
+    if((A1_val > A2_val))
         error_code = 3;
     else
-        error_code = 0;*/
-    
-    error_code++;
-        
+        error_code = 0;
+
+
+
     //lcd->cls();
-    lcd->locate(1,0);
-    lcd->printf("Error: %d", error_code);
-    
+    //lcd->locate(1,0);
+    //lcd->printf("Error: %d", error_code);
+
     wait_us(100);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
 // Log frequency, digital and filtered analogue values to uSD
-// 
+//
 void Task7(void)
 {
     logcount++;
-    fprintf(fp,"Log: %d,Freq: %dHz,Digital_In: %d,Analogue_1: %f,Analogue_2: %f\n",logcount,freq,switch_state,A1_val,A2_val); 
+    fprintf(fp,"Log: %d,Freq: %fHz,Digital_In: %d,Analogue_1: %f,Analogue_2: %f\n",logcount,freq,switch_state,A1_val,A2_val);
 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Shutdown on switch 
-// 
-/*void Task8()
+// Shutdown on switch
+//
+void Task8()
 {
-    if(DS_sIn == 1)
-    {
-        tick.detach();
+    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{
+    } else {
     }
 }
-*/
 
 
 
+