rfsesf

Dependencies:   mbed C12832

Committer:
kwstasfane1
Date:
Fri Apr 23 12:40:28 2021 +0000
Revision:
2:250c8d4f4201
Parent:
1:d37c71b35a5a
dfs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kwstasfane1 0:b94498823546 1 /*
kwstasfane1 0:b94498823546 2 Created by Konstantinos Fane
kwstasfane1 0:b94498823546 3 copy this code to Mbed online simulator
kwstasfane1 0:b94498823546 4 to run the designed application
kwstasfane1 0:b94498823546 5 */
kwstasfane1 0:b94498823546 6
kwstasfane1 0:b94498823546 7 #include "mbed.h"
kwstasfane1 0:b94498823546 8 #include <string>
kwstasfane1 0:b94498823546 9 #include "C12832.h"
kwstasfane1 0:b94498823546 10
kwstasfane1 0:b94498823546 11 // LCD display
kwstasfane1 0:b94498823546 12 C12832 lcd(p5, p7, p6, p8, p11);
kwstasfane1 0:b94498823546 13
kwstasfane1 1:d37c71b35a5a 14 //variables used for frequency, period and time interval measruements
kwstasfane1 2:250c8d4f4201 15 float m_freq_hz;
kwstasfane1 2:250c8d4f4201 16 float m_t_interval_ms;
kwstasfane1 1:d37c71b35a5a 17 float period = 0.0;
kwstasfane1 1:d37c71b35a5a 18 float count1 = 0.0;
kwstasfane1 0:b94498823546 19
kwstasfane1 0:b94498823546 20 //result to be displayed
kwstasfane1 0:b94498823546 21 float m_result;
kwstasfane1 0:b94498823546 22
kwstasfane1 0:b94498823546 23 const char* range_scale [3][3] = {
kwstasfane1 0:b94498823546 24 {"MHz","KHz","Hz"},
kwstasfane1 0:b94498823546 25 {"ms","ms","s"},
kwstasfane1 0:b94498823546 26 {"ms","ms","s"}
kwstasfane1 0:b94498823546 27 };
kwstasfane1 0:b94498823546 28
kwstasfane1 1:d37c71b35a5a 29 /* flag to hold which measurement mode to display, 0 = Frequency Measurement,
kwstasfane1 1:d37c71b35a5a 30 * 1 = Period Measurement, 2 = Time interval Measurement
kwstasfane1 0:b94498823546 31 * to match the index of message display arrays */
kwstasfane1 0:b94498823546 32 int dsp_mode = 0;
kwstasfane1 0:b94498823546 33 const char* mode_msg [] = {"Frequency", "Period", "Time Interval"};
kwstasfane1 0:b94498823546 34
kwstasfane1 0:b94498823546 35 /* flag to hold which measurement range to display
kwstasfane1 1:d37c71b35a5a 36 * 0 = High Range, 1 = Medium Range, 2 = Low Range */
kwstasfane1 0:b94498823546 37 int dsp_range = 0;
kwstasfane1 0:b94498823546 38 const char* range_msg [] = {"High Resolution", "Medium Resolution", "Low Resolution"};
kwstasfane1 0:b94498823546 39
kwstasfane1 0:b94498823546 40 int dsp_time_interval = 0; //0=R-R, 1=F-F, 2=R-F, 3=F-R
kwstasfane1 0:b94498823546 41
kwstasfane1 0:b94498823546 42 //result string
kwstasfane1 0:b94498823546 43 const char* res[] = {""};
kwstasfane1 0:b94498823546 44
kwstasfane1 0:b94498823546 45 //Interrupt to be used in actual implementation to read the input square wave signal
kwstasfane1 0:b94498823546 46 InterruptIn squareIN(p17);
kwstasfane1 0:b94498823546 47
kwstasfane1 0:b94498823546 48 //User interface measurement mode and range selection buttons
kwstasfane1 0:b94498823546 49 InterruptIn mode(p12);
kwstasfane1 0:b94498823546 50 InterruptIn range(p15);
kwstasfane1 0:b94498823546 51 InterruptIn time_interval(p13);
kwstasfane1 0:b94498823546 52
kwstasfane1 1:d37c71b35a5a 53 //interrupt service routines
kwstasfane1 1:d37c71b35a5a 54 void counter1_isr();
kwstasfane1 1:d37c71b35a5a 55 void freq_calc_isr();
kwstasfane1 0:b94498823546 56 void mode_select();
kwstasfane1 0:b94498823546 57 void range_select();
kwstasfane1 0:b94498823546 58 void time_interval_type_select();
kwstasfane1 0:b94498823546 59
kwstasfane1 0:b94498823546 60 //Timeout task2;
kwstasfane1 0:b94498823546 61 Ticker freq_meas; //generate a tick
kwstasfane1 0:b94498823546 62 Ticker ui_disp; //generate the 1s interrupt, to measure frequency (direct frequency measurement)
kwstasfane1 1:d37c71b35a5a 63
kwstasfane1 1:d37c71b35a5a 64 //user defined functions to implement the user interface and result calculation
kwstasfane1 1:d37c71b35a5a 65 void user_interface_disp();
kwstasfane1 1:d37c71b35a5a 66 void result_calculator();
kwstasfane1 1:d37c71b35a5a 67
kwstasfane1 0:b94498823546 68 int main()
kwstasfane1 0:b94498823546 69 {
kwstasfane1 0:b94498823546 70
kwstasfane1 0:b94498823546 71 //ticker, where it is done periodically, timeout only does it once
kwstasfane1 0:b94498823546 72 //Direct frequency measurement, measures the number of rising edges every time they occur
kwstasfane1 0:b94498823546 73 squareIN.rise(&counter1_isr); //maximum frequency related to interrupt operating frequency
kwstasfane1 0:b94498823546 74 freq_meas.attach(&freq_calc_isr,1); //obtain number of sums per second
kwstasfane1 0:b94498823546 75
kwstasfane1 1:d37c71b35a5a 76 ui_disp.attach(&user_interface_disp, 1); //update the screen every 1 second
kwstasfane1 0:b94498823546 77
kwstasfane1 0:b94498823546 78 //interrupt routine for mode and range selection user input
kwstasfane1 0:b94498823546 79 mode.rise(&mode_select);
kwstasfane1 0:b94498823546 80 range.rise(&range_select);
kwstasfane1 0:b94498823546 81 time_interval.rise(&time_interval_type_select);
kwstasfane1 0:b94498823546 82
kwstasfane1 0:b94498823546 83 while(1)
kwstasfane1 0:b94498823546 84 {
kwstasfane1 1:d37c71b35a5a 85 wait_ms(0.1);
kwstasfane1 0:b94498823546 86 }
kwstasfane1 0:b94498823546 87
kwstasfane1 0:b94498823546 88
kwstasfane1 0:b94498823546 89 }
kwstasfane1 0:b94498823546 90
kwstasfane1 0:b94498823546 91 //ISR to count the occurences of incoming signal rising edges
kwstasfane1 0:b94498823546 92 void counter1_isr()
kwstasfane1 0:b94498823546 93 {
kwstasfane1 0:b94498823546 94 //increment counter by 1
kwstasfane1 0:b94498823546 95 count1 = count1 + 1.0;
kwstasfane1 0:b94498823546 96 }
kwstasfane1 0:b94498823546 97
kwstasfane1 0:b94498823546 98 //ISR to calculate the period of incoming signal, every 1 second
kwstasfane1 0:b94498823546 99 void freq_calc_isr()
kwstasfane1 0:b94498823546 100 {
kwstasfane1 0:b94498823546 101 m_freq_hz = count1;
kwstasfane1 0:b94498823546 102 count1 = 0;
kwstasfane1 1:d37c71b35a5a 103 //call the result calculator function, to calculate the required period and time interval results
kwstasfane1 1:d37c71b35a5a 104 result_calculator();
kwstasfane1 0:b94498823546 105 }
kwstasfane1 0:b94498823546 106
kwstasfane1 0:b94498823546 107 void user_interface_disp()
kwstasfane1 0:b94498823546 108 {
kwstasfane1 0:b94498823546 109
kwstasfane1 0:b94498823546 110 lcd.cls(); //clear screen
kwstasfane1 0:b94498823546 111 lcd.locate(1,1);
kwstasfane1 0:b94498823546 112 lcd.printf("Mode: %s", mode_msg[dsp_mode]);
kwstasfane1 0:b94498823546 113
kwstasfane1 0:b94498823546 114 //display the type of time interval measurement if the mode is selected
kwstasfane1 0:b94498823546 115 if(dsp_mode ==2)
kwstasfane1 0:b94498823546 116 {
kwstasfane1 0:b94498823546 117 lcd.locate(90,1);
kwstasfane1 0:b94498823546 118 if(dsp_time_interval == 0)
kwstasfane1 0:b94498823546 119 {
kwstasfane1 0:b94498823546 120 lcd.printf("(R->R)");
kwstasfane1 0:b94498823546 121 }
kwstasfane1 0:b94498823546 122 else if(dsp_time_interval == 1)
kwstasfane1 0:b94498823546 123 {
kwstasfane1 0:b94498823546 124 lcd.printf("(F->F)");
kwstasfane1 0:b94498823546 125 }
kwstasfane1 0:b94498823546 126 else if(dsp_time_interval == 2)
kwstasfane1 0:b94498823546 127 {
kwstasfane1 0:b94498823546 128 lcd.printf("(R->F)");
kwstasfane1 0:b94498823546 129 }
kwstasfane1 0:b94498823546 130 else if(dsp_time_interval == 3)
kwstasfane1 0:b94498823546 131 {
kwstasfane1 0:b94498823546 132 lcd.printf("(F->R)");
kwstasfane1 0:b94498823546 133 }
kwstasfane1 0:b94498823546 134
kwstasfane1 0:b94498823546 135 }
kwstasfane1 0:b94498823546 136 lcd.locate(1,10);
kwstasfane1 0:b94498823546 137 lcd.printf("Range: %s", range_msg[dsp_range]);
kwstasfane1 0:b94498823546 138 lcd.locate(1,20);
kwstasfane1 0:b94498823546 139 //print the result
kwstasfane1 0:b94498823546 140 lcd.printf(*res,m_result,range_scale[dsp_mode][dsp_range]);
kwstasfane1 0:b94498823546 141
kwstasfane1 0:b94498823546 142 }
kwstasfane1 0:b94498823546 143
kwstasfane1 0:b94498823546 144 void time_interval_type_select()
kwstasfane1 0:b94498823546 145 {
kwstasfane1 0:b94498823546 146 if (dsp_time_interval <3)
kwstasfane1 0:b94498823546 147 {
kwstasfane1 0:b94498823546 148 dsp_time_interval++;
kwstasfane1 0:b94498823546 149 }
kwstasfane1 0:b94498823546 150 else
kwstasfane1 0:b94498823546 151 {
kwstasfane1 0:b94498823546 152 dsp_time_interval = 0;
kwstasfane1 0:b94498823546 153 }
kwstasfane1 0:b94498823546 154 }
kwstasfane1 0:b94498823546 155
kwstasfane1 0:b94498823546 156 //Interrupt service routine to handle the measurement mode selection
kwstasfane1 0:b94498823546 157 void mode_select()
kwstasfane1 0:b94498823546 158 {
kwstasfane1 0:b94498823546 159 if (dsp_mode < 2)
kwstasfane1 0:b94498823546 160 {
kwstasfane1 0:b94498823546 161 dsp_mode++;
kwstasfane1 0:b94498823546 162 }
kwstasfane1 0:b94498823546 163 else
kwstasfane1 0:b94498823546 164 {
kwstasfane1 0:b94498823546 165 dsp_mode = 0;
kwstasfane1 0:b94498823546 166 }
kwstasfane1 0:b94498823546 167 }
kwstasfane1 0:b94498823546 168
kwstasfane1 0:b94498823546 169 //Interrupt service routine to handle the measurement range selection
kwstasfane1 0:b94498823546 170 void range_select()
kwstasfane1 0:b94498823546 171 {
kwstasfane1 0:b94498823546 172 if (dsp_range < 2)
kwstasfane1 0:b94498823546 173 {
kwstasfane1 0:b94498823546 174 dsp_range++;
kwstasfane1 0:b94498823546 175 }
kwstasfane1 0:b94498823546 176 else
kwstasfane1 0:b94498823546 177 {
kwstasfane1 0:b94498823546 178 dsp_range = 0;
kwstasfane1 0:b94498823546 179 }
kwstasfane1 0:b94498823546 180 }
kwstasfane1 0:b94498823546 181
kwstasfane1 0:b94498823546 182 void result_calculator()
kwstasfane1 0:b94498823546 183 {
kwstasfane1 0:b94498823546 184
kwstasfane1 0:b94498823546 185 //calculate period from frequency
kwstasfane1 0:b94498823546 186 float m_period_ms = (1/m_freq_hz)*1000; //times by 1000 to convert to ms
kwstasfane1 0:b94498823546 187
kwstasfane1 0:b94498823546 188 if(dsp_mode == 0) //frequency measurement
kwstasfane1 0:b94498823546 189 {
kwstasfane1 0:b94498823546 190 if(dsp_range == 0) //High resolution (MHz)
kwstasfane1 0:b94498823546 191 {
kwstasfane1 0:b94498823546 192 m_result = m_freq_hz/1000000;
kwstasfane1 0:b94498823546 193 if(m_result <= 10.001)
kwstasfane1 0:b94498823546 194 {
kwstasfane1 0:b94498823546 195 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 196 *res="%3.3f %s";
kwstasfane1 0:b94498823546 197 }
kwstasfane1 0:b94498823546 198 else
kwstasfane1 0:b94498823546 199 {
kwstasfane1 0:b94498823546 200 *res="Out-of-range... '>10 MHz !'";
kwstasfane1 0:b94498823546 201 }
kwstasfane1 0:b94498823546 202
kwstasfane1 0:b94498823546 203 }
kwstasfane1 0:b94498823546 204 else if(dsp_range == 1) //medium resolution (KHz)
kwstasfane1 0:b94498823546 205 {
kwstasfane1 0:b94498823546 206 m_result = m_freq_hz/1000;
kwstasfane1 0:b94498823546 207 if(m_result <= 100.001)
kwstasfane1 0:b94498823546 208 {
kwstasfane1 0:b94498823546 209 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 210 *res="%3.3f %s";
kwstasfane1 0:b94498823546 211 }
kwstasfane1 0:b94498823546 212 else
kwstasfane1 0:b94498823546 213 {
kwstasfane1 0:b94498823546 214 *res="Out-of-range... '>100 KHz !'";
kwstasfane1 0:b94498823546 215 }
kwstasfane1 0:b94498823546 216 }
kwstasfane1 0:b94498823546 217 else //low resolution (Hz)
kwstasfane1 0:b94498823546 218 {
kwstasfane1 0:b94498823546 219 m_result = m_freq_hz;
kwstasfane1 0:b94498823546 220 if(m_result <= 100.001)
kwstasfane1 0:b94498823546 221 {
kwstasfane1 0:b94498823546 222 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 223 *res="%3.3f %s";
kwstasfane1 0:b94498823546 224 }
kwstasfane1 0:b94498823546 225 else
kwstasfane1 0:b94498823546 226 {
kwstasfane1 0:b94498823546 227 *res="Out-of-range... '>100 Hz !'";
kwstasfane1 0:b94498823546 228 }
kwstasfane1 0:b94498823546 229
kwstasfane1 0:b94498823546 230 }
kwstasfane1 0:b94498823546 231
kwstasfane1 0:b94498823546 232 /* '*res' assignment was moved inside the ifs, to that in case the current
kwstasfane1 0:b94498823546 233 nuber to be displayed is higher that acceptable upper limit, to delete
kwstasfane1 0:b94498823546 234 the displayed measurement, and show warning message*/
kwstasfane1 0:b94498823546 235
kwstasfane1 0:b94498823546 236 }
kwstasfane1 0:b94498823546 237 else if (dsp_mode == 1) //period measurement
kwstasfane1 0:b94498823546 238 {
kwstasfane1 0:b94498823546 239 if(dsp_range == 0) //High resolution (2.000 +- 0.0001 ms)
kwstasfane1 0:b94498823546 240 {
kwstasfane1 0:b94498823546 241 m_result = m_period_ms;
kwstasfane1 0:b94498823546 242 if(m_result <= 2.0001)
kwstasfane1 0:b94498823546 243 {
kwstasfane1 0:b94498823546 244 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 245 *res="%1.4f %s";
kwstasfane1 0:b94498823546 246 }
kwstasfane1 0:b94498823546 247 else
kwstasfane1 0:b94498823546 248 {
kwstasfane1 0:b94498823546 249 *res="Out-of-range... '>2.000 ms !'";
kwstasfane1 0:b94498823546 250 }
kwstasfane1 0:b94498823546 251 }
kwstasfane1 0:b94498823546 252 else if(dsp_range == 1) //medium resolution (20.00 +- 0.01 ms)
kwstasfane1 0:b94498823546 253 {
kwstasfane1 0:b94498823546 254 m_result = m_period_ms;
kwstasfane1 0:b94498823546 255 if(m_result <= 20.01)
kwstasfane1 0:b94498823546 256 {
kwstasfane1 0:b94498823546 257 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 258 *res="%2.2f %s";
kwstasfane1 0:b94498823546 259 }
kwstasfane1 0:b94498823546 260 else
kwstasfane1 0:b94498823546 261 {
kwstasfane1 0:b94498823546 262 *res="Out-of-range... '>20.00 ms !'";
kwstasfane1 0:b94498823546 263 }
kwstasfane1 0:b94498823546 264 }
kwstasfane1 0:b94498823546 265 else //low resolution (2.000 +- 0.001 s)
kwstasfane1 0:b94498823546 266 {
kwstasfane1 0:b94498823546 267 m_result = m_period_ms/1000;
kwstasfane1 0:b94498823546 268 if(m_result <= 2.001)
kwstasfane1 0:b94498823546 269 {
kwstasfane1 0:b94498823546 270 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 271 *res="%1.3f %s";
kwstasfane1 0:b94498823546 272 }
kwstasfane1 0:b94498823546 273 else
kwstasfane1 0:b94498823546 274 {
kwstasfane1 0:b94498823546 275 *res="Out-of-range... '>2.000 sec'";
kwstasfane1 0:b94498823546 276 }
kwstasfane1 0:b94498823546 277 }
kwstasfane1 0:b94498823546 278 }
kwstasfane1 0:b94498823546 279 else // "(dsp_mode == 2)" time interval measurement
kwstasfane1 0:b94498823546 280 {
kwstasfane1 0:b94498823546 281 if(dsp_time_interval == 0 || dsp_time_interval == 1) //rising->rising, falling -> falling
kwstasfane1 0:b94498823546 282 {
kwstasfane1 0:b94498823546 283 m_result = m_period_ms;
kwstasfane1 0:b94498823546 284 }
kwstasfane1 0:b94498823546 285 else if(dsp_time_interval == 2 || dsp_time_interval == 3) //rising->falling, falling->rising
kwstasfane1 0:b94498823546 286 {
kwstasfane1 0:b94498823546 287 m_result = m_period_ms/2; //based on the assumption that the square wave has 50% DUTY CYCLE
kwstasfane1 0:b94498823546 288 }
kwstasfane1 0:b94498823546 289
kwstasfane1 0:b94498823546 290 if(dsp_range == 0) //High resolution (2.0000 +- 0.0001 ms)
kwstasfane1 0:b94498823546 291 {
kwstasfane1 0:b94498823546 292 if(m_result <= 2.0001)
kwstasfane1 0:b94498823546 293 {
kwstasfane1 0:b94498823546 294 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 295 *res="%1.4f %s";
kwstasfane1 0:b94498823546 296 }
kwstasfane1 0:b94498823546 297 else
kwstasfane1 0:b94498823546 298 {
kwstasfane1 0:b94498823546 299 *res="Out-of-range... '>2.000 ms !'";
kwstasfane1 0:b94498823546 300 }
kwstasfane1 0:b94498823546 301 }
kwstasfane1 0:b94498823546 302 else if(dsp_range == 1) //medium resolution (20.00 +- 0.01 ms)
kwstasfane1 0:b94498823546 303 {
kwstasfane1 0:b94498823546 304 if(m_result <= 20.01)
kwstasfane1 0:b94498823546 305 {
kwstasfane1 0:b94498823546 306 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 307 *res="%2.2f %s";
kwstasfane1 0:b94498823546 308 }
kwstasfane1 0:b94498823546 309 else
kwstasfane1 0:b94498823546 310 {
kwstasfane1 0:b94498823546 311 *res="Out-of-range... '>20.00 ms !'";
kwstasfane1 0:b94498823546 312 }
kwstasfane1 0:b94498823546 313 }
kwstasfane1 0:b94498823546 314 else //low resolution (2.000+-0.0001ms)
kwstasfane1 0:b94498823546 315 {
kwstasfane1 0:b94498823546 316 //m_result = m_t_interval_ms/10e3;
kwstasfane1 0:b94498823546 317 m_result = m_result/1000;
kwstasfane1 0:b94498823546 318 if(m_result <= 2.001)
kwstasfane1 0:b94498823546 319 {
kwstasfane1 0:b94498823546 320 //assigns the decimal point precision for each range
kwstasfane1 0:b94498823546 321 *res="%1.3f %s";
kwstasfane1 0:b94498823546 322 }
kwstasfane1 0:b94498823546 323 else
kwstasfane1 0:b94498823546 324 {
kwstasfane1 0:b94498823546 325 *res="Out-of-range... '>2.000 sec'";
kwstasfane1 0:b94498823546 326 }
kwstasfane1 0:b94498823546 327 }
kwstasfane1 0:b94498823546 328 }
kwstasfane1 0:b94498823546 329 }