![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded software Assessment 2
Dependencies: MCP23017 SDFileSystem USBDevice WattBob_TextLCD mbed
Diff: FreqMesure.h
- Revision:
- 20:00a9a95ef083
- Child:
- 22:644d53f1f291
diff -r 304b17087d06 -r 00a9a95ef083 FreqMesure.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FreqMesure.h Tue Mar 04 18:42:38 2014 +0000 @@ -0,0 +1,55 @@ +/*************************************************************************************** +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() > 4) + break; + } + } + else{ + while(FrequencyIn){ + if (FreqTimeOut.read_ms() > 4) + break; + } + } + //This is the begining of the edge start the timer to masure + FreqMsurT.start(); + if (FreqTimeOut.read_ms() > 4){ + 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(); +}