Bryan and Naved Debub monitor

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 #ifdef __cplusplus 
00039 extern "C" {
00040 #endif
00041 /**********************/   
00042 /*   Definitions     */
00043 /**********************/
00044 
00045 extern uint16_t zero_detect_counter = 0; // initialize the value to zero
00046 extern UCHAR sample_count =0; // intialize sample count to zero
00047 extern uint32_t frequency_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
00048 extern UCHAR frequency_buffer_count = 0;
00049 extern UCHAR reset_counter_flag = 0;
00050 extern UCHAR current_bool_value =1; // trigger for zero detection within the code
00051 extern UCHAR last_bool_value = 1; // trigger for zero detection within the code
00052 
00053     
00054 #ifdef __cplusplus
00055 }
00056 #endif
00057 
00058 // read in ADC from the professor and store it within this function (ADC() will store a value within 
00059 //will write code to keep going through this buffer, need to be able to store value to this function fast enough
00060 //uint16_t ADC_buffer;
00061 //UCHAR size_ADC_array = 10;
00062 void frequency_detect(void)
00063 {  int32_t i;
00064     
00065    if(reset_counter_flag==1)
00066          {
00067        zero_detect_counter = 0; // reset the value of counter each 800us has passed, want to attain the moving average(fed from timer0.cpp)
00068          reset_counter_flag=0; // set reset flag back to 0
00069     }
00070 
00071       if(ADC_vortex_frequency_input<=0) 
00072           {
00073              current_bool_value =0; // negative values set to 0
00074           }
00075              else
00076              {
00077              current_bool_value =1; // positive value set to 1
00078        }
00079              
00080         if((current_bool_value != last_bool_value))
00081         {
00082    last_bool_value = current_bool_value;
00083    zero_detect_counter = zero_detect_counter+1;
00084             
00085      if ((frequency_buffer_count<10) && (zero_detect_counter>=2))
00086      {
00087          frequency_buffer[frequency_buffer_count]  = ((zero_detect_counter-1)/((sample_count)*(SAMPLES_SECOND)))/2;
00088              frequency_buffer_count = frequency_buffer_count+1;
00089      }
00090     else 
00091         {
00092             frequency_buffer_count = 1;
00093     }
00094    
00095     }
00096     
00097   if((zero_detect_counter == 0) & (reset_counter_flag == 0))
00098     {
00099       sample_count = 1;
00100   }
00101     else if(zero_detect_counter>0)
00102     {
00103     sample_count++; // only evaluated at the beginning of code during resets
00104   }
00105     else {
00106         sample_count = 1; // needed to not divide by zero
00107     }
00108 
00109         uint16_t sum_frequency_buffer= 0;
00110 
00111         for(i = 1; i < 10; ++i)
00112     {
00113         sum_frequency_buffer += frequency_buffer[i];
00114     }
00115         
00116         if ((sum_frequency_buffer/9)>3000)
00117         {
00118             freq_value = 0;
00119         }
00120     else
00121         {
00122            freq_value = sum_frequency_buffer/9;
00123         }
00124 
00125 }
00126 
00127 
00128