revised code

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers frequency_detector.cpp Source File

frequency_detector.cpp

Go to the documentation of this file.
00001 /**----------------------------------------------------------------------------
00002  *
00003  *            \file frequency_detector.cpp
00004 --                                                                           --
00005 --              ECEN 5803 Mastering Embedded System Architecture             --
00006 --                  Project 1 Module 4                                       --
00007 --                Microcontroller Firmware                                   --
00008 --                      frequency_detector.cpp                                           --
00009 --                                                                           --
00010 -------------------------------------------------------------------------------
00011 --
00012 --  Designed for:  University of Colorado at Boulder
00013 --                
00014 --                
00015 --  Designed by:  Tim Scherr
00016 --  Revised by:  Naved Maududi and Bryan Cisneros 
00017 -- 
00018 -- Version: 2.1
00019 -- Date of current revision:  2017-09-25   
00020 -- Target Microcontroller: Freescale MKL25ZVMT4 
00021 -- Tools used:  ARM mbed compiler
00022 --              ARM mbed SDK
00023 --              Freescale FRDM-KL25Z Freedom Board
00024 --               
00025 --               
00026    Functional Description:  
00027    This file contains code that takes in quasi-sine wave ADC inputs from the flow meter
00028      From the ADC inputs, it calculates the frequency of that sine wave. The frequency is 
00029      important in determining the flow rate and velocity for the flowmeter that are outputed
00030      to the display. 
00031 -- 
00032 --      Copyright (c) 2015 Tim Scherr  All rights reserved.
00033 */
00034 
00035 
00036 #include "shared.h" 
00037 
00038 
00039 #ifdef __cplusplus 
00040 extern "C" {
00041 #endif
00042 /**********************/   
00043 /*   Definitions     */
00044 /**********************/
00045 
00046 extern double zero_detect_counter = 0; // initialize the value to zero
00047 extern double sample_count =0; // intialize sample count to zero
00048 extern double frequency_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
00049 //extern double test_value = 0;
00050 extern UCHAR frequency_buffer_count = 0;
00051 extern UCHAR reset_counter_flag = 0;
00052 extern UCHAR current_bool_value =1; // trigger for zero detection within the code
00053 extern UCHAR last_bool_value = 1; // trigger for zero detection within the code
00054 extern double SAMPLES_SECOND = 0.0001;
00055 
00056 extern uint16_t freq_value = 0;
00057 
00058     
00059 #ifdef __cplusplus
00060 }
00061 #endif
00062 
00063 // read in ADC from the professor and store it within this function (ADC() will store a value within 
00064 //will write code to keep going through this buffer, need to be able to store value to this function fast enough
00065 //uint16_t ADC_buffer;
00066 //UCHAR size_ADC_array = 10;
00067 void frequency_detect(void)
00068 {  int32_t i;
00069     
00070   // if(reset_counter_flag == 1)
00071  //        {
00072  //      zero_detect_counter = 0; // reset the value of counter each 800us has passed, want to attain the moving average(fed from timer0.cpp)
00073   //       reset_counter_flag=0; // set reset flag back to 0
00074  //   }
00075  
00076 
00077 
00078       if(ADC_vortex_frequency_input <=(0)) 
00079           {
00080              current_bool_value =0; // negative values set to 0
00081           }
00082              else
00083              {
00084              current_bool_value =1; // positive value set to 1
00085        }
00086              
00087         if((current_bool_value != last_bool_value))
00088         {
00089    last_bool_value = current_bool_value;
00090    zero_detect_counter = zero_detect_counter+1;
00091   
00092      if ((frequency_buffer_count<9) && (zero_detect_counter>=2))
00093      {
00094          frequency_buffer[frequency_buffer_count]  = ((zero_detect_counter)/((sample_count)*(SAMPLES_SECOND)))/10;
00095              frequency_buffer_count = frequency_buffer_count+1;
00096              zero_detect_counter = 0;
00097      }
00098     else 
00099         {
00100             frequency_buffer_count = 0;
00101      }
00102    
00103     }
00104     
00105   if((zero_detect_counter == 0) & (reset_counter_flag == 0))
00106     {
00107       sample_count = 1;
00108   }
00109     else if(zero_detect_counter>0)
00110     {
00111     sample_count++; // only evaluated at the beginning of code during resets
00112   }
00113     else {
00114         sample_count = 1; // needed to not divide by zero
00115     }
00116 
00117         double sum_frequency_buffer= 0;
00118 
00119         for(i = 0; i < 10; ++i)
00120     {
00121         sum_frequency_buffer += frequency_buffer[i];
00122     }
00123         
00124         if ((sum_frequency_buffer/10)>3000)
00125         {
00126             freq_value = 0;
00127         }
00128     else
00129         {
00130            freq_value = sum_frequency_buffer/10;
00131         }
00132         
00133 }
00134 
00135 
00136