/ Freq_Meter
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Freq_Meter_Code.cpp Source File

Freq_Meter_Code.cpp

00001 /*
00002    LCD RS pin to digital pin 5
00003    LCD Enable pin to digital pin 4
00004    LCD D4 pin to digital pin 0
00005    LCD D5 pin to digital pin 1
00006    LCD D6 pin to digital pin 2
00007    LCD D7 pin to digital pin 3
00008    LCD R/W pin to ground
00009    LCD VSS pin to ground
00010    LCD VCC pin to 3.3V
00011    100K pot:
00012    ends to 3.3V and ground
00013    wiper to LCD VO pin (pin 3)
00014 */
00015 
00016 /*
00017  * Max freq. mbed board can measure (accurately) = 50kHz
00018  * circuit contains freq. divider to scale freq. down by 4
00019  * Thus max theoretical measurable freq = 200kHz
00020  * Duty cycle measurement before freq. divider
00021  * Duty cycle for high an low pulse times of >10us only
00022  */
00023 /*********************************************************************/
00024 #include <string>
00025 #include <Pulse.h>
00026 #include "mbed.h"
00027 #include "TextLCD.h"
00028 
00029 TextLCD lcd(D5, D4, D0, D1, D2, D3);//RS, E, D4, D5, D6, D7
00030 
00031 /*********************************************************************/
00032 
00033 Case 1:
00034 
00035 lcd.printf("Frequency:");
00036 
00037 string freqpin("D14");
00038 
00039 double tHi; //to store avg time high
00040 
00041 double tLo; //to store avg time low
00042 
00043 float period;
00044 
00045 float freq;
00046 
00047 tHi = highTime(freqpin); //call function to compute avg high time
00048 
00049 tLo = lowTime(freqpin); //call function to compute avg low time
00050 
00051 period = period(tHi, tLo); //call function to compute period
00052 
00053 duty = dutyCycle(tHi, period); //function to give duty cycle
00054 
00055 freq = frequency(period); //function to give freq
00056 
00057 if (freq < 1000) {
00058 
00059     lcd.locate(0,1);
00060     
00061     lcd.printf("%.2f Hz", freq);
00062     }
00063 else if (1000 <= freq <= 200000){
00064     
00065     freq = freq / 1000;
00066     
00067     lcd.locate(0,1);
00068 
00069     lcd.printf("%.2f kHz", freq);
00070     }
00071 else {
00072     
00073 /*********************************************************************/
00074 case 2:
00075 
00076 string dutypin("D13");
00077 
00078 double tHi; //to store avg time high
00079 
00080 double tLo; //to store avg time low
00081 
00082 float period;
00083 
00084 double duty;
00085 
00086 tHi = highTime(dutypin); //call function to compute avg high time
00087 
00088 tLo = lowTime(dutypin); //call function to compute avg low time
00089 
00090 period = period(tHi, tLo); //call function to compute period
00091 
00092 duty = dutyCycle(tHi, period); //function to give duty cycle
00093 
00094 /*********************************************************************/
00095 
00096 double highTime(string x)
00097 {
00098     PulseInOut InputPin(x);
00099     double highTimes[5]; //array to store times when input is high
00100     double result = 0; //to store avg time high
00101 
00102     for (int i = 0; i <= 5; i++) {
00103 
00104         highTimes[i] = int read_high_us(); //measure hi pulse x5
00105 
00106     }
00107     for (int j = 0; j <= 5; j++) {
00108 
00109         result = result + highTimes[j];
00110     }
00111     result = result / 5; //compute avg
00112     return result;
00113 }
00114 
00115 double lowTime(string x)
00116 {
00117     PulseInOut InputPin(x);
00118     double lowTimes[5]; //array to store times when input is high
00119     double result = 0; //to store avg time high
00120 
00121     for (int i = 0; i <= 5; i++) {
00122 
00123         lowTimes[i] = int read_low_us(); //measure lo pulse x5
00124 
00125     }
00126     for (int j = 0; j <= 5; j++) {
00127 
00128         result = result + lowTimes[j];
00129     }
00130     result  = result / 5; //compute avg
00131     return result;
00132 
00133 }
00134 
00135 float period(double x, double y)
00136 {
00137     float result;
00138     result = x + y;
00139     return result;
00140 }
00141 
00142 double dutyCycle(double x, float y)
00143 {
00144 
00145     double result;
00146     result = (x / y) * 100;
00147     return result;
00148 }
00149 
00150 float frequency(float x)
00151 {
00152     float result;
00153     result = (1000000 / x) * 4;
00154     return result;
00155 }