Embedded software Assessment 2

Dependencies:   MCP23017 SDFileSystem USBDevice WattBob_TextLCD mbed

FreqMesure.h

Committer:
muaiyd
Date:
2014-03-05
Revision:
23:ffd758e50c3a
Parent:
22:644d53f1f291

File content as of revision 23:ffd758e50c3a:

/***************************************************************************************
Measure the frequency of a 3.3v square wave signal once every second. The frequency range
is 500Hz to 1000Hz with 50% duty cycle). In this code the on complet cycle is peiod is 
masured after detcting the edge also a timeout timer is seting to detect open loop.
*****************************************************************************************/

void FreqMsur(){
    FreqMsurPin = ! (FreqMsurPin);   //Pin 23
    FreqTimeOut.start();
    // Ignore first part of the signal to detect the edge
    if(!FrequencyIn){ 
        while(!FrequencyIn){
            if (FreqTimeOut.read_ms() > 3)
                break;
        }
    }
    else{
        while(FrequencyIn){
            if (FreqTimeOut.read_ms() > 3)
                break;
        }
    }
    //This is the begining of the edge start the timer to masure
    FreqMsurT.start();
    if (FreqTimeOut.read_ms() > 3){
           FreqTimeOutHpn=1;
    }
    else{
            FreqTimeOutHpn=0;
    }
    FreqTimeOut.stop(); 
    FreqTimeOut.reset();
    if(!FreqTimeOutHpn){
        if(!FrequencyIn){
            while(!FrequencyIn){
            }
            while(FrequencyIn){
            }
        }
        else{
            while(FrequencyIn){
            }
            while(!FrequencyIn){
            }
        }    
        FreqMsurT.stop();
        float Period=FreqMsurT.read_us();
        Freq=1/Period*1000000.0+0.5;  //Adding 0.5 to round the frequency to nearst int
    }
    else{
        Freq=0xffff; 
    }                
    FreqTimeOut.stop();
    FreqMsurT.reset();
    //FreqMsurPin = ! (FreqMsurPin);
}