Konstantinos Fane
/
Frequencymeasurement
Diff: main.cpp
- Revision:
- 5:faf243e88b50
- Parent:
- 4:ce121f9e6db5
--- a/main.cpp Tue Apr 20 12:35:43 2021 +0000 +++ b/main.cpp Thu Apr 22 16:40:49 2021 +0000 @@ -1,8 +1,4 @@ -/* - Created by Konstantinos Fane - copy this code to Mbed online simulator - to run the designed application -*/ +/* Written by Konstantinos Fane */ #include "mbed.h" #include <string> @@ -30,18 +26,14 @@ {"ms","ms","s"} }; -/* flag to hold which measurement mode to display - * 0 = Frequency Measurement - * 1 = Period Measurement - * 2 = Time interval Measurement +/* flag to hold which measurement mode to display, 0 = Frequency Measurement + * 1 = Period Measurement, 2 = Time interval Measurement * to match the index of message display arrays */ int dsp_mode = 0; const char* mode_msg [] = {"Frequency", "Period", "Time Interval"}; /* flag to hold which measurement range to display - * 0 = High Range - * 1 = Medium Range - * 2 = Low Range */ + * 0 = High Range, 1 = Medium Range, 2 = Low Range */ int dsp_range = 0; const char* range_msg [] = {"High Resolution", "Medium Resolution", "Low Resolution"}; @@ -51,8 +43,6 @@ 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 - InterruptIn squareIN(p17); //User interface measurement mode and range selection buttons @@ -60,13 +50,6 @@ InterruptIn range(p15); InterruptIn time_interval(p13); -/*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();*/ - //user defined functions to implement the user interface void user_interface_disp(); void mode_select(); @@ -75,30 +58,19 @@ void result_calculator(); //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 ui_disp; //generate the 1s interrupt, to measure frequency (direct frequency measurement) -Ticker result_calc; +Ticker result_calc; //generate 100us interrupt, to calculate a new result 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.attach_us(&counter1_isr,100); - + //Reciprocal frequency measurement, measures the number of rising edges every time they occur 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) - 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 @@ -107,73 +79,30 @@ range.rise(&range_select); time_interval.rise(&time_interval_type_select); - /*interrupt routine for multiplier increase/decrease user input - incr_freq.rise(&incr); - decr_freq.rise(&decr);*/ - - //start the timer t1 + //start the timer t1,t2 t1.start(); t2.start(); while(1) { } - - } -//dID NOT PROVIDE A GOOD MEASUREMENT OF TON void start_t1_isr() { period = t1.read_us(); t1.reset(); t2.reset(); - //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() -{ - //increment counter by 1 - count1 = count1 + 1; -} - -//ISR to calculate the period of incoming signal, every 1 second -void freq_calc_isr() -{ - //m_freq_hz = 1/period; - //m_freq_hz = multi * count1; - //count1 = 0; -} - -/*simulation only -void incr() -{ - multi = multi + 1500.5; -} - -void decr() -{ - multi = multi - 100.5; - if (multi<10.5) - { - multi = 0.5; - } -}*/ - void user_interface_disp() { - - //At startup just print a few things on the lcd and then enter a loop until the user does something lcd.cls(); //clear screen lcd.locate(1,1); lcd.printf("Mode: %s", mode_msg[dsp_mode]); @@ -197,24 +126,13 @@ else if(dsp_time_interval == 3) { lcd.printf("(F->R)"); - } - + } } - /*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(); //print the result - lcd.printf(*res,m_result,range_scale[dsp_mode][dsp_range]); - + lcd.printf(*res,m_result,range_scale[dsp_mode][dsp_range]); } void time_interval_type_select() @@ -257,12 +175,9 @@ void result_calculator() { - - //calculate period from frequency - //float m_period_s = 1/m_freq_hz; - float m_period_ms = period/1000; + //calculate frequency in Hz and period in ms + float m_period_ms = period/1000; //period calculated in us, divide by 1000 float m_freq_hz = 1/(m_period_ms/1000); - //m_t_interval_ms = ton/1000; if(dsp_mode == 0) //frequency measurement { @@ -306,12 +221,7 @@ *res="Out-of-range... '>100 Hz !'"; } - } - - /* '*res' assignment was moved inside the ifs, to that in case the current - nuber to be displayed is higher that acceptable upper limit, to delete - the displayed measurement, and show warning message*/ - + } } else if (dsp_mode == 1) //period measurement { @@ -396,7 +306,6 @@ } else //low resolution (2.000+-0.0001ms) { - //m_result = m_t_interval_ms/10e3; m_result = m_result/1000; if(m_result <= 2.001) { @@ -410,42 +319,3 @@ } } } - -/* - * Implemention of the vMy_Task1(), if 4 inputs are provided, - * vMy_Task2 is executed. - * -void vMy_Task1_isr() -{ - input_count = input_count + 1; - printf("Key pressed!: %d\n", input_count); - if(input_count == 4) - { - printf("4 inputs provided...\n"); - input_count = 0; - printf("Will move onto task2\n"); - - //call task 2 after 2 seconds to close the led, not the same as the - //ticker, where it is done periodically, timeout only does it once - task2.attach(&vMy_Task2, 2.0); - //call this to turn on the led, after it has been dictated to shut down after 2 secs - vMy_Task2(); - } -} - -void vMy_Task2() -{ - led1=!led1; - printf("Task 2 Running… LED1 = %d\n", led1.read()); -} - -void vMy_Task3() -{ - led2=1; - led3=0; - led4=0; - printf("Task 3 Running… LED2 = %d, LED3 = %d, LED4 = %d\n", led2.read(), led3.read(), led4.read()); - wait(0.01); - -} -*/