![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded software Assessment 2
Dependencies: MCP23017 SDFileSystem USBDevice WattBob_TextLCD mbed
FreqMesure.h@20:00a9a95ef083, 2014-03-04 (annotated)
- Committer:
- muaiyd
- Date:
- Tue Mar 04 18:42:38 2014 +0000
- Revision:
- 20:00a9a95ef083
- Child:
- 22:644d53f1f291
I finish it !!!!!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
muaiyd | 20:00a9a95ef083 | 1 | /*************************************************************************************** |
muaiyd | 20:00a9a95ef083 | 2 | Measure the frequency of a 3.3v square wave signal once every second. The frequency range |
muaiyd | 20:00a9a95ef083 | 3 | is 500Hz to 1000Hz with 50% duty cycle). In this code the on complet cycle is peiod is |
muaiyd | 20:00a9a95ef083 | 4 | masured after detcting the edge also a timeout timer is seting to detect open loop. |
muaiyd | 20:00a9a95ef083 | 5 | *****************************************************************************************/ |
muaiyd | 20:00a9a95ef083 | 6 | |
muaiyd | 20:00a9a95ef083 | 7 | void FreqMsur(){ |
muaiyd | 20:00a9a95ef083 | 8 | FreqMsurPin = ! (FreqMsurPin); //Pin 23 |
muaiyd | 20:00a9a95ef083 | 9 | FreqTimeOut.start(); |
muaiyd | 20:00a9a95ef083 | 10 | // Ignore first part of the signal to detect the edge |
muaiyd | 20:00a9a95ef083 | 11 | if(!FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 12 | while(!FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 13 | if (FreqTimeOut.read_ms() > 4) |
muaiyd | 20:00a9a95ef083 | 14 | break; |
muaiyd | 20:00a9a95ef083 | 15 | } |
muaiyd | 20:00a9a95ef083 | 16 | } |
muaiyd | 20:00a9a95ef083 | 17 | else{ |
muaiyd | 20:00a9a95ef083 | 18 | while(FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 19 | if (FreqTimeOut.read_ms() > 4) |
muaiyd | 20:00a9a95ef083 | 20 | break; |
muaiyd | 20:00a9a95ef083 | 21 | } |
muaiyd | 20:00a9a95ef083 | 22 | } |
muaiyd | 20:00a9a95ef083 | 23 | //This is the begining of the edge start the timer to masure |
muaiyd | 20:00a9a95ef083 | 24 | FreqMsurT.start(); |
muaiyd | 20:00a9a95ef083 | 25 | if (FreqTimeOut.read_ms() > 4){ |
muaiyd | 20:00a9a95ef083 | 26 | FreqTimeOutHpn=1; |
muaiyd | 20:00a9a95ef083 | 27 | } |
muaiyd | 20:00a9a95ef083 | 28 | else{ |
muaiyd | 20:00a9a95ef083 | 29 | FreqTimeOutHpn=0; |
muaiyd | 20:00a9a95ef083 | 30 | } |
muaiyd | 20:00a9a95ef083 | 31 | FreqTimeOut.stop(); |
muaiyd | 20:00a9a95ef083 | 32 | FreqTimeOut.reset(); |
muaiyd | 20:00a9a95ef083 | 33 | if(!FreqTimeOutHpn){ |
muaiyd | 20:00a9a95ef083 | 34 | if(!FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 35 | while(!FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 36 | } |
muaiyd | 20:00a9a95ef083 | 37 | while(FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 38 | } |
muaiyd | 20:00a9a95ef083 | 39 | } |
muaiyd | 20:00a9a95ef083 | 40 | else{ |
muaiyd | 20:00a9a95ef083 | 41 | while(FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 42 | } |
muaiyd | 20:00a9a95ef083 | 43 | while(!FrequencyIn){ |
muaiyd | 20:00a9a95ef083 | 44 | } |
muaiyd | 20:00a9a95ef083 | 45 | } |
muaiyd | 20:00a9a95ef083 | 46 | FreqMsurT.stop(); |
muaiyd | 20:00a9a95ef083 | 47 | float Period=FreqMsurT.read_us(); |
muaiyd | 20:00a9a95ef083 | 48 | Freq=1/Period*1000000.0+0.5; //Adding 0.5 to round the frequency to nearst int |
muaiyd | 20:00a9a95ef083 | 49 | } |
muaiyd | 20:00a9a95ef083 | 50 | else{ |
muaiyd | 20:00a9a95ef083 | 51 | Freq=0xffff; |
muaiyd | 20:00a9a95ef083 | 52 | } |
muaiyd | 20:00a9a95ef083 | 53 | FreqTimeOut.stop(); |
muaiyd | 20:00a9a95ef083 | 54 | FreqMsurT.reset(); |
muaiyd | 20:00a9a95ef083 | 55 | } |