chad
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
Diff: tasks.cpp
- Revision:
- 15:85616bc0e2ae
- Parent:
- 14:ede0e7ed2745
- Child:
- 16:bebcc7d24f3e
--- a/tasks.cpp Wed Mar 08 15:36:57 2017 +0000 +++ b/tasks.cpp Wed Mar 08 16:17:09 2017 +0000 @@ -1,8 +1,37 @@ #include "main.h" -//////////////////////////////////////////////////////////////////////////////// -// Subroutines -// +// 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); + } + } + + 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); + } + } + + 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: %d",freq); +} void PosEdge(void) { @@ -22,47 +51,17 @@ } } -// 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); - } - } - - 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); - } - } - - //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 -} - - //////////////////////////////////////////////////////////////////////////////// // // Read Digital input switch // void Task2(void) { - switch_state = DSIn == 1 ? 1: 0; - - //lcd->locate(0,0); - //lcd->printf("S: %d", switch_state); + //switch_state = DSIn == 1 ? 1: 0; + if(DSIn == 1) + switch_state = 1; + else if(DSIn == 0) + switch_state = 0; } @@ -86,15 +85,21 @@ { 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++) { - A1_val = A1_val + A1_val + (A1_in * 3.3); - A2_val = A2_val + A2_val + (A2_in * 3.3); + A1_val = A1_val + A1_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); } @@ -145,7 +150,7 @@ void Task7(void) { 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); + fprintf(fp,"Log: %d,Freq: %dHz,Digital_In: %d,Analogue_1: %f,Analogue_2: %f\n",logcount,freq,switch_state,A1_val,A2_val); }