revised code

Dependencies:   mbed

Committer:
bcis93
Date:
Fri Oct 04 22:03:30 2019 +0000
Revision:
1:9fa7cc80f1a7
Parent:
0:4fb921928934
Child:
2:64a34ae90bb1
compiled;

Who changed what in which revision?

UserRevisionLine numberNew 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 0:4fb921928934 38 #ifdef __cplusplus
nmaududi 0:4fb921928934 39 extern "C" {
nmaududi 0:4fb921928934 40 #endif
nmaududi 0:4fb921928934 41 /**********************/
nmaududi 0:4fb921928934 42 /* Definitions */
nmaududi 0:4fb921928934 43 /**********************/
nmaududi 0:4fb921928934 44
nmaududi 0:4fb921928934 45 extern uint16_t zero_detect_counter = 0; // initialize the value to zero
nmaududi 0:4fb921928934 46 extern UCHAR sample_count =0; // intialize sample count to zero
nmaududi 0:4fb921928934 47 extern uint32_t frequency_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
nmaududi 0:4fb921928934 48 extern UCHAR frequency_buffer_count = 0;
nmaududi 0:4fb921928934 49 extern UCHAR reset_counter_flag = 0;
nmaududi 0:4fb921928934 50 extern UCHAR current_bool_value =1; // trigger for zero detection within the code
nmaududi 0:4fb921928934 51 extern UCHAR last_bool_value = 1; // trigger for zero detection within the code
nmaududi 0:4fb921928934 52
bcis93 1:9fa7cc80f1a7 53 uint16_t freq_value = 0;
bcis93 1:9fa7cc80f1a7 54
nmaududi 0:4fb921928934 55
nmaududi 0:4fb921928934 56 #ifdef __cplusplus
nmaududi 0:4fb921928934 57 }
nmaududi 0:4fb921928934 58 #endif
nmaududi 0:4fb921928934 59
nmaududi 0:4fb921928934 60 // read in ADC from the professor and store it within this function (ADC() will store a value within
nmaududi 0:4fb921928934 61 //will write code to keep going through this buffer, need to be able to store value to this function fast enough
nmaududi 0:4fb921928934 62 //uint16_t ADC_buffer;
nmaududi 0:4fb921928934 63 //UCHAR size_ADC_array = 10;
nmaududi 0:4fb921928934 64 void frequency_detect(void)
nmaududi 0:4fb921928934 65 { int32_t i;
nmaududi 0:4fb921928934 66
nmaududi 0:4fb921928934 67 if(reset_counter_flag==1)
nmaududi 0:4fb921928934 68 {
nmaududi 0:4fb921928934 69 zero_detect_counter = 0; // reset the value of counter each 800us has passed, want to attain the moving average(fed from timer0.cpp)
nmaududi 0:4fb921928934 70 reset_counter_flag=0; // set reset flag back to 0
nmaududi 0:4fb921928934 71 }
nmaududi 0:4fb921928934 72
nmaududi 0:4fb921928934 73 if(ADC_vortex_frequency_input<=0)
nmaududi 0:4fb921928934 74 {
nmaududi 0:4fb921928934 75 current_bool_value =0; // negative values set to 0
nmaududi 0:4fb921928934 76 }
nmaududi 0:4fb921928934 77 else
nmaududi 0:4fb921928934 78 {
nmaududi 0:4fb921928934 79 current_bool_value =1; // positive value set to 1
nmaududi 0:4fb921928934 80 }
nmaududi 0:4fb921928934 81
nmaududi 0:4fb921928934 82 if((current_bool_value != last_bool_value))
nmaududi 0:4fb921928934 83 {
nmaududi 0:4fb921928934 84 last_bool_value = current_bool_value;
nmaududi 0:4fb921928934 85 zero_detect_counter = zero_detect_counter+1;
nmaududi 0:4fb921928934 86
nmaududi 0:4fb921928934 87 if ((frequency_buffer_count<10) && (zero_detect_counter>=2))
nmaududi 0:4fb921928934 88 {
nmaududi 0:4fb921928934 89 frequency_buffer[frequency_buffer_count] = ((zero_detect_counter-1)/((sample_count)*(SAMPLES_SECOND)))/2;
nmaududi 0:4fb921928934 90 frequency_buffer_count = frequency_buffer_count+1;
nmaududi 0:4fb921928934 91 }
nmaududi 0:4fb921928934 92 else
nmaududi 0:4fb921928934 93 {
nmaududi 0:4fb921928934 94 frequency_buffer_count = 1;
nmaududi 0:4fb921928934 95 }
nmaududi 0:4fb921928934 96
nmaududi 0:4fb921928934 97 }
nmaududi 0:4fb921928934 98
nmaududi 0:4fb921928934 99 if((zero_detect_counter == 0) & (reset_counter_flag == 0))
nmaududi 0:4fb921928934 100 {
nmaududi 0:4fb921928934 101 sample_count = 1;
nmaududi 0:4fb921928934 102 }
nmaududi 0:4fb921928934 103 else if(zero_detect_counter>0)
nmaududi 0:4fb921928934 104 {
nmaududi 0:4fb921928934 105 sample_count++; // only evaluated at the beginning of code during resets
nmaududi 0:4fb921928934 106 }
nmaududi 0:4fb921928934 107 else {
nmaududi 0:4fb921928934 108 sample_count = 1; // needed to not divide by zero
nmaududi 0:4fb921928934 109 }
nmaududi 0:4fb921928934 110
nmaududi 0:4fb921928934 111 uint16_t sum_frequency_buffer= 0;
nmaududi 0:4fb921928934 112
nmaududi 0:4fb921928934 113 for(i = 1; i < 10; ++i)
nmaududi 0:4fb921928934 114 {
nmaududi 0:4fb921928934 115 sum_frequency_buffer += frequency_buffer[i];
nmaududi 0:4fb921928934 116 }
nmaududi 0:4fb921928934 117
nmaududi 0:4fb921928934 118 if ((sum_frequency_buffer/9)>3000)
nmaududi 0:4fb921928934 119 {
nmaududi 0:4fb921928934 120 freq_value = 0;
nmaududi 0:4fb921928934 121 }
nmaududi 0:4fb921928934 122 else
nmaududi 0:4fb921928934 123 {
nmaududi 0:4fb921928934 124 freq_value = sum_frequency_buffer/9;
nmaududi 0:4fb921928934 125 }
nmaududi 0:4fb921928934 126
nmaududi 0:4fb921928934 127 }
nmaududi 0:4fb921928934 128
nmaududi 0:4fb921928934 129
nmaududi 0:4fb921928934 130