chad

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

tasks.cpp

Committer:
f_legge
Date:
2017-03-13
Revision:
17:bc25d5f47bab
Parent:
16:bebcc7d24f3e

File content as of revision 17:bc25d5f47bab:

#include "main.h"

////////////////////////////////////////////////////////////////////////////////
//
// 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){                   // 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 ();
        }
        while (Fq == 1){            // Wait for low pulse
            wait_us (1);            
        }
        if (Fq == 0){               // Stop timer on low pulse
            timer.stop ();
        }
    }
    
    else if (Fq == 1){              // Case for starting on high pulse
        while (Fq == 1){            // Wait for low pulse
            wait_us (1);
        }
        if (Fq == 0){               // Start timer on low pulse
            timer.start ();
        }
        while (Fq == 0){            // Wait for high pulse
            wait_us (1);            
        }
        if (Fq == 1){               // Stop timer on high pulse
            timer.stop ();
        }
    }

    freq = (1/(2*timer.read()));    // Calc frequency from timer
    
    //T5.stop();
    
    //lcd->locate(0,0);
    //lcd->printf("F: %4.0f",freq);

    //lcd->locate(1,0);
    //lcd->printf("%f",T5.read());
}

////////////////////////////////////////////////////////////////////////////////
//
// Read Digital input switch        
// 3us max
//
void Task2(void)
{
    //T5.reset();
    //T5.start();

    if(DS == 1)                     // If switch 1 is high set switch_state register high
        switch_state = 1;
    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          6.1ms max
//
void Task3(void)
{
    //T5.reset();
    //T5.start();
    
    WD_pulse = 1;       // WatchDog pulse High
    wait_us(WD);        // Leave high for specified length
    WD_pulse = 0;       // WatchDog pulse low
    
    //T5.stop();
    
    //lcd->locate(1,0);
    //lcd->printf("%f",T5.read());
}

////////////////////////////////////////////////////////////////////////////////
//
// Read and filter 2 analogue inputs           
// 3.3V max
// 55us
//
void Task4(void)
{
    //T5.reset();
    //T5.start();
    
    A1_val = 0;         // Reset analogue 1 variable
    A2_val = 0;         // Reset analogue 2 variable

    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<4; i++) {        // Take three readings for each input 
        A1_val = A1_val + A1_In;
        A2_val = A2_val + A2_In;
    }

    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 
//
void Task5a(void)                   // 25.5 ms  
{
    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->locate(1,9);               // Printing time to complete task 5 (ms)
    lcd->printf("%.1f",(T5.read()*1000));
}

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();                      // Stop task 5 timer

    lcd->locate(1,9);               // Printing time to complete task 5 (ms)
    lcd->printf("%.1f",(T5.read()*1000));
}

////////////////////////////////////////////////////////////////////////////////
//
// Error check          
// 3us 
// 
// Else error code 0
//
void Task6(void)
{
    //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                    // If conditions not met error code is 0
        error_code = 0;

    //T5.stop();

    //lcd->locate(0,0);
    //lcd->printf("Error: %d", error_code);
    
    //lcd->locate(1,0);
    //lcd->printf("%f",T5.read());
}

////////////////////////////////////////////////////////////////////////////////
//
// Log frequency, digital and filtered analogue values to uSD
// 21ms
//
void Task7(void)
{
    //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()
{
    //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();
    }
}