Konstantinos Fane
/
Frequencymeasurement
embed simulator
Diff: main.cpp
- Revision:
- 3:e3e73c36a565
- Parent:
- 0:167e85b50250
- Child:
- 4:ce121f9e6db5
--- a/main.cpp Mon Apr 19 22:45:43 2021 +0000 +++ b/main.cpp Tue Apr 20 12:05:58 2021 +0000 @@ -13,13 +13,16 @@ //dummy measurement values float m_freq_hz; // = 120730.546423; //*10^6 Hz -float m_t_interval_ms = 988.056475; +float m_t_interval_ms; //= 988.056475; //result to be displayed float m_result; //variables used for frequency, period and time interval measruements float count1 = 0.0; +float ton = 0.0; +float toff = 0.0; +float period = 0.0; const char* range_scale [3][3] = { {"MHz","KHz","Hz"}, @@ -44,6 +47,9 @@ int dsp_time_interval = 0; //0=R-R, 1=F-F, 2=R-F, 3=F-R +//result string +const char* res[] = {""}; + //Interrupt to be used in actual implementation to read the input square wave signal //InterruptIn squareIN(p#) //# = the pin number where the actual signal would be conected to @@ -51,15 +57,15 @@ //User interface measurement mode and range selection buttons InterruptIn mode(p12); -InterruptIn range(p13); -InterruptIn time_interval(p14); +InterruptIn range(p15); +InterruptIn time_interval(p13); -//For simulation purposes only, multiplier for the count1, to achieve higher simulated frequencies +/*For simulation purposes only, multiplier for the count1, to achieve higher simulated frequencies InterruptIn incr_freq(p15); InterruptIn decr_freq(p16); float multi = 1.0; void incr(); -void decr(); +void decr();*/ //user defined functions to implement the user interface void user_interface_disp(); @@ -71,42 +77,69 @@ //interrupt service routines void counter1_isr(); void freq_calc_isr(); +void start_t1_isr(); +void start_t2_isr(); //Timeout task2; Ticker tick1; //generate a tick -Ticker freq1; //generate the 1s interrupt, to measure frequency (direct frequency measurement) +Ticker ui_disp; //generate the 1s interrupt, to measure frequency (direct frequency measurement) +Ticker result_calc; +Timer t1; +Timer t2; int main() { //ticker, where it is done periodically, timeout only does it once //Direct frequency measurement, measures the number of rising edges every time they occur - squareIN.rise(&counter1_isr); - //squareIN.fall(&) + //squareIN.attach_us(&counter1_isr,100); + squareIN.rise(&start_t1_isr); + squareIN.fall(&start_t2_isr); //For simulation purposes only //tick1.attach_us(&counter1_isr, 1); //generate a tick every 100 us = 0.0001s 1us = 100000 s (period) - freq1.attach(&freq_calc_isr, 1); //measure the numbed of ticks per 1 second - + ui_disp.attach(&user_interface_disp, 1); //update the screen every 1 second + result_calc.attach_us(&result_calculator,100); //calculate a new measurement result every 100us //interrupt routine for mode and range selection user input mode.rise(&mode_select); range.rise(&range_select); time_interval.rise(&time_interval_type_select); - //interrupt routine for multiplier increase/decrease user input + /*interrupt routine for multiplier increase/decrease user input incr_freq.rise(&incr); - decr_freq.rise(&decr); + decr_freq.rise(&decr);*/ + + //start the timer t1 + t1.start(); while(1) { - user_interface_disp(); - wait_ms(0.001); + //user_interface_disp(); //didnt refresh properly when tested on the mbed board + //wait(0.1); } } +//dID NOT PROVIDE A GOOD MEASUREMENT OF TON +void start_t1_isr() +{ + period = t1.read_us(); + t1.reset(); + t2.start(); + //t2.stop(); + //toff=t2.read(); +} + +void start_t2_isr() +{ + //t2.start(); + ton = t2.read_us(); + t2.reset(); + //period = ton+toff; +} + //ISR to count the occurences of incoming signal rising edges void counter1_isr() { @@ -117,12 +150,12 @@ //ISR to calculate the period of incoming signal, every 1 second void freq_calc_isr() { - //m_freq_hz = count1; - m_freq_hz = multi * count1; - count1 = 0; + //m_freq_hz = 1/period; + //m_freq_hz = multi * count1; + //count1 = 0; } -//simulation only +/*simulation only void incr() { multi = multi + 1500.5; @@ -135,7 +168,7 @@ { multi = 0.5; } -} +}*/ void user_interface_disp() { @@ -167,18 +200,20 @@ } } - else + /*else { //simulation only - to display the current multiplier lcd.locate(78,1); lcd.printf("(x%1.1f)",multi); - } + }*/ lcd.locate(1,10); lcd.printf("Range: %s", range_msg[dsp_range]); lcd.locate(1,20); //call the result calculator function - result_calculator(); + //result_calculator(); + //print the result + lcd.printf(*res,m_result,range_scale[dsp_mode][dsp_range]); } @@ -224,8 +259,10 @@ { //calculate period from frequency - float m_period_s = 1/m_freq_hz; - const char* res[] = {""}; + //float m_period_s = 1/m_freq_hz; + float m_period_ms = period/1000; + float m_freq_hz = 1/(m_period_ms/1000); + m_t_interval_ms = ton/1000; if(dsp_mode == 0) //frequency measurement { @@ -280,7 +317,7 @@ { if(dsp_range == 0) //High resolution (2.000 +- 0.0001 ms) { - m_result = m_period_s*1000; + m_result = m_period_ms; if(m_result <= 2.0001) { //assigns the decimal point precision for each range @@ -293,7 +330,7 @@ } else if(dsp_range == 1) //medium resolution (20.00 +- 0.01 ms) { - m_result = m_period_s*1000; + m_result = m_period_ms; if(m_result <= 20.01) { //assigns the decimal point precision for each range @@ -306,7 +343,7 @@ } else //low resolution (2.000 +- 0.001 s) { - m_result = m_period_s; + m_result = m_period_ms/1000; if(m_result <= 2.001) { //assigns the decimal point precision for each range @@ -359,11 +396,7 @@ *res="Out-of-range... '>2.000 sec'"; } } - } - - //print the result - lcd.printf(*res,m_result,range_scale[dsp_mode][dsp_range]); - + } } /*