Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
frequency_detector.cpp@2:64a34ae90bb1, 2019-10-05 (annotated)
- Committer:
- nmaududi
- Date:
- Sat Oct 05 21:17:17 2019 +0000
- Revision:
- 2:64a34ae90bb1
- Parent:
- 1:9fa7cc80f1a7
revised version for module 4;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nmaududi | 0:4fb921928934 | 1 | /**---------------------------------------------------------------------------- |
| nmaududi | 0:4fb921928934 | 2 | * |
| nmaududi | 0:4fb921928934 | 3 | * \file frequency_detector.cpp |
| nmaududi | 0:4fb921928934 | 4 | -- -- |
| nmaududi | 0:4fb921928934 | 5 | -- ECEN 5803 Mastering Embedded System Architecture -- |
| nmaududi | 0:4fb921928934 | 6 | -- Project 1 Module 4 -- |
| nmaududi | 0:4fb921928934 | 7 | -- Microcontroller Firmware -- |
| nmaududi | 0:4fb921928934 | 8 | -- frequency_detector.cpp -- |
| nmaududi | 0:4fb921928934 | 9 | -- -- |
| nmaududi | 0:4fb921928934 | 10 | ------------------------------------------------------------------------------- |
| nmaududi | 0:4fb921928934 | 11 | -- |
| nmaududi | 0:4fb921928934 | 12 | -- Designed for: University of Colorado at Boulder |
| nmaududi | 0:4fb921928934 | 13 | -- |
| nmaududi | 0:4fb921928934 | 14 | -- |
| nmaududi | 0:4fb921928934 | 15 | -- Designed by: Tim Scherr |
| nmaududi | 0:4fb921928934 | 16 | -- Revised by: Naved Maududi and Bryan Cisneros |
| nmaududi | 0:4fb921928934 | 17 | -- |
| nmaududi | 0:4fb921928934 | 18 | -- Version: 2.1 |
| nmaududi | 0:4fb921928934 | 19 | -- Date of current revision: 2017-09-25 |
| nmaududi | 0:4fb921928934 | 20 | -- Target Microcontroller: Freescale MKL25ZVMT4 |
| nmaududi | 0:4fb921928934 | 21 | -- Tools used: ARM mbed compiler |
| nmaududi | 0:4fb921928934 | 22 | -- ARM mbed SDK |
| nmaududi | 0:4fb921928934 | 23 | -- Freescale FRDM-KL25Z Freedom Board |
| nmaududi | 0:4fb921928934 | 24 | -- |
| nmaududi | 0:4fb921928934 | 25 | -- |
| nmaududi | 0:4fb921928934 | 26 | Functional Description: |
| nmaududi | 0:4fb921928934 | 27 | This file contains code that takes in quasi-sine wave ADC inputs from the flow meter |
| nmaududi | 0:4fb921928934 | 28 | From the ADC inputs, it calculates the frequency of that sine wave. The frequency is |
| nmaududi | 0:4fb921928934 | 29 | important in determining the flow rate and velocity for the flowmeter that are outputed |
| nmaududi | 0:4fb921928934 | 30 | to the display. |
| nmaududi | 0:4fb921928934 | 31 | -- |
| nmaududi | 0:4fb921928934 | 32 | -- Copyright (c) 2015 Tim Scherr All rights reserved. |
| nmaududi | 0:4fb921928934 | 33 | */ |
| nmaududi | 0:4fb921928934 | 34 | |
| nmaududi | 0:4fb921928934 | 35 | |
| nmaududi | 0:4fb921928934 | 36 | #include "shared.h" |
| nmaududi | 0:4fb921928934 | 37 | |
| nmaududi | 2:64a34ae90bb1 | 38 | |
| nmaududi | 0:4fb921928934 | 39 | #ifdef __cplusplus |
| nmaududi | 0:4fb921928934 | 40 | extern "C" { |
| nmaududi | 0:4fb921928934 | 41 | #endif |
| nmaududi | 0:4fb921928934 | 42 | /**********************/ |
| nmaududi | 0:4fb921928934 | 43 | /* Definitions */ |
| nmaududi | 0:4fb921928934 | 44 | /**********************/ |
| nmaududi | 0:4fb921928934 | 45 | |
| nmaududi | 2:64a34ae90bb1 | 46 | extern double zero_detect_counter = 0; // initialize the value to zero |
| nmaududi | 2:64a34ae90bb1 | 47 | extern double sample_count =0; // intialize sample count to zero |
| nmaududi | 2:64a34ae90bb1 | 48 | extern double frequency_buffer[10] = {0,0,0,0,0,0,0,0,0,0}; |
| nmaududi | 2:64a34ae90bb1 | 49 | //extern double test_value = 0; |
| nmaududi | 0:4fb921928934 | 50 | extern UCHAR frequency_buffer_count = 0; |
| nmaududi | 0:4fb921928934 | 51 | extern UCHAR reset_counter_flag = 0; |
| nmaududi | 0:4fb921928934 | 52 | extern UCHAR current_bool_value =1; // trigger for zero detection within the code |
| nmaududi | 0:4fb921928934 | 53 | extern UCHAR last_bool_value = 1; // trigger for zero detection within the code |
| nmaududi | 2:64a34ae90bb1 | 54 | extern double SAMPLES_SECOND = 0.0001; |
| nmaududi | 0:4fb921928934 | 55 | |
| nmaududi | 2:64a34ae90bb1 | 56 | extern uint16_t freq_value = 0; |
| bcis93 | 1:9fa7cc80f1a7 | 57 | |
| nmaududi | 0:4fb921928934 | 58 | |
| nmaududi | 0:4fb921928934 | 59 | #ifdef __cplusplus |
| nmaududi | 0:4fb921928934 | 60 | } |
| nmaududi | 0:4fb921928934 | 61 | #endif |
| nmaududi | 0:4fb921928934 | 62 | |
| nmaududi | 0:4fb921928934 | 63 | // read in ADC from the professor and store it within this function (ADC() will store a value within |
| nmaududi | 0:4fb921928934 | 64 | //will write code to keep going through this buffer, need to be able to store value to this function fast enough |
| nmaududi | 0:4fb921928934 | 65 | //uint16_t ADC_buffer; |
| nmaududi | 0:4fb921928934 | 66 | //UCHAR size_ADC_array = 10; |
| nmaududi | 0:4fb921928934 | 67 | void frequency_detect(void) |
| nmaududi | 0:4fb921928934 | 68 | { int32_t i; |
| nmaududi | 0:4fb921928934 | 69 | |
| nmaududi | 2:64a34ae90bb1 | 70 | // if(reset_counter_flag == 1) |
| nmaududi | 2:64a34ae90bb1 | 71 | // { |
| nmaududi | 2:64a34ae90bb1 | 72 | // zero_detect_counter = 0; // reset the value of counter each 800us has passed, want to attain the moving average(fed from timer0.cpp) |
| nmaududi | 2:64a34ae90bb1 | 73 | // reset_counter_flag=0; // set reset flag back to 0 |
| nmaududi | 2:64a34ae90bb1 | 74 | // } |
| nmaududi | 2:64a34ae90bb1 | 75 | |
| nmaududi | 0:4fb921928934 | 76 | |
| nmaududi | 2:64a34ae90bb1 | 77 | |
| nmaududi | 2:64a34ae90bb1 | 78 | if(ADC_vortex_frequency_input <=(0)) |
| nmaududi | 0:4fb921928934 | 79 | { |
| nmaududi | 0:4fb921928934 | 80 | current_bool_value =0; // negative values set to 0 |
| nmaududi | 0:4fb921928934 | 81 | } |
| nmaududi | 0:4fb921928934 | 82 | else |
| nmaududi | 0:4fb921928934 | 83 | { |
| nmaududi | 0:4fb921928934 | 84 | current_bool_value =1; // positive value set to 1 |
| nmaududi | 0:4fb921928934 | 85 | } |
| nmaududi | 0:4fb921928934 | 86 | |
| nmaududi | 0:4fb921928934 | 87 | if((current_bool_value != last_bool_value)) |
| nmaududi | 0:4fb921928934 | 88 | { |
| nmaududi | 0:4fb921928934 | 89 | last_bool_value = current_bool_value; |
| nmaududi | 0:4fb921928934 | 90 | zero_detect_counter = zero_detect_counter+1; |
| nmaududi | 2:64a34ae90bb1 | 91 | |
| nmaududi | 2:64a34ae90bb1 | 92 | if ((frequency_buffer_count<9) && (zero_detect_counter>=2)) |
| nmaududi | 0:4fb921928934 | 93 | { |
| nmaududi | 2:64a34ae90bb1 | 94 | frequency_buffer[frequency_buffer_count] = ((zero_detect_counter)/((sample_count)*(SAMPLES_SECOND)))/10; |
| nmaududi | 0:4fb921928934 | 95 | frequency_buffer_count = frequency_buffer_count+1; |
| nmaududi | 2:64a34ae90bb1 | 96 | zero_detect_counter = 0; |
| nmaududi | 0:4fb921928934 | 97 | } |
| nmaududi | 0:4fb921928934 | 98 | else |
| nmaududi | 0:4fb921928934 | 99 | { |
| nmaududi | 2:64a34ae90bb1 | 100 | frequency_buffer_count = 0; |
| nmaududi | 2:64a34ae90bb1 | 101 | } |
| nmaududi | 0:4fb921928934 | 102 | |
| nmaududi | 0:4fb921928934 | 103 | } |
| nmaududi | 0:4fb921928934 | 104 | |
| nmaududi | 0:4fb921928934 | 105 | if((zero_detect_counter == 0) & (reset_counter_flag == 0)) |
| nmaududi | 0:4fb921928934 | 106 | { |
| nmaududi | 0:4fb921928934 | 107 | sample_count = 1; |
| nmaududi | 0:4fb921928934 | 108 | } |
| nmaududi | 0:4fb921928934 | 109 | else if(zero_detect_counter>0) |
| nmaududi | 0:4fb921928934 | 110 | { |
| nmaududi | 0:4fb921928934 | 111 | sample_count++; // only evaluated at the beginning of code during resets |
| nmaududi | 0:4fb921928934 | 112 | } |
| nmaududi | 0:4fb921928934 | 113 | else { |
| nmaududi | 0:4fb921928934 | 114 | sample_count = 1; // needed to not divide by zero |
| nmaududi | 0:4fb921928934 | 115 | } |
| nmaududi | 0:4fb921928934 | 116 | |
| nmaududi | 2:64a34ae90bb1 | 117 | double sum_frequency_buffer= 0; |
| nmaududi | 0:4fb921928934 | 118 | |
| nmaududi | 2:64a34ae90bb1 | 119 | for(i = 0; i < 10; ++i) |
| nmaududi | 0:4fb921928934 | 120 | { |
| nmaududi | 0:4fb921928934 | 121 | sum_frequency_buffer += frequency_buffer[i]; |
| nmaududi | 0:4fb921928934 | 122 | } |
| nmaududi | 0:4fb921928934 | 123 | |
| nmaududi | 2:64a34ae90bb1 | 124 | if ((sum_frequency_buffer/10)>3000) |
| nmaududi | 0:4fb921928934 | 125 | { |
| nmaududi | 0:4fb921928934 | 126 | freq_value = 0; |
| nmaududi | 0:4fb921928934 | 127 | } |
| nmaududi | 0:4fb921928934 | 128 | else |
| nmaududi | 0:4fb921928934 | 129 | { |
| nmaududi | 2:64a34ae90bb1 | 130 | freq_value = sum_frequency_buffer/10; |
| nmaududi | 0:4fb921928934 | 131 | } |
| nmaududi | 2:64a34ae90bb1 | 132 | |
| nmaududi | 0:4fb921928934 | 133 | } |
| nmaududi | 0:4fb921928934 | 134 | |
| nmaududi | 0:4fb921928934 | 135 | |
| nmaududi | 0:4fb921928934 | 136 |