⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pulse_count.c

📁 CODE for embedded C ,hand coding version
💻 C
字号:
/*---------------------------------------------------------------------*-
  Pulse_Count.C(v1.00)
  -----------------------------------------------------------------------
  Count pulses from a mechanical switch or similar device 
  Response to falling edge of pulse : _
                                       |_
 -*-------------------------------------------------------------------*/

 #include "Main.H"
 #include "Port.H"
 #include "Bargraph.H"
 #include "Pulse_Count.H"

 //-------------------Private function prototype ----------------------
 void PULSE_COUNT_Check_Below_Threshold(const tByte);
 //------------------Public variable declarations ---------------------
 //The data to be displayed 
 extern tBargraph Data_G ;

 //------------------Public variable definition -----------------------
 //Set only after falling edge is detected 
 bit Falling_edge_G;

 //-----------Private variable definition ------------------------------
 //The results of successive tests of the pulse signal 
 //(Note: Can't have arrays of bits .. )

 static bit Test4 , Test3 ,Test2, Test1, Test0;
 static tByte Total_G = 0 ;
 static tWord Calls_G = 0;

 //---------Private constants -----------------------------------------
 //Allow changed of logic without hardware changes
 #define HI_LEVEL (0)
 #define LO_LEVEL (1)

 /*--------------------------------------------------------------------*-
  PULSE_COUNT_Init()
  Initialisation function for the switch library 
  -*--------------------------------------------------------------------*/

  void PULSE_COUNT_Init(void ){
   Sw_pin = 1 ; //Use this pin for input 
   //The tests (see Text)
   Test4 = LO_LEVEL;
   Test3 = LO_LEVEL;
   Test2 = LO_LEVEL;
   Test1 = LO_LEVEL;
   Test0 = LO_LEVEL;

  }

  /*--------------------------------------------------------------------*-
   PULSE_COUNT_Check_Below_Threshold()
   Checks to see if pulse count is below a specified 
   threshold value ,If it is , sounds an alarm
   -*--------------------------------------------------------------------*/

   void PULSE_COUNT_Check_Below_Threshold(const tByte THRESHOLD){
    if(Data_G < THRESHOLD){
	 Alarm_pin = 0 ;
	}else{
	 Alarm_pin = 1 ;
	}
   }
  /*------------------------------------------------------------------*-
    PULSE_COUNT_Update()
	This is the main switch function 
	It should be called every 30 ms
	(to allow for typical 20 ms  debounce time )

	-*----------------------------------------------------------------*/

	void PULSE_COUNT_Update(void){
	//Clear timer flag
	TF2  = 0 ;
	//Shuffle the test results 
	Test4 = Test3;
	Test3 = Test2;
	Test2 = Test1;
	Test1 = Test0;

	//Get latest test result 
	Test0 = Sw_pin ;
	//Required result 
	//Test4 = HI_LEVEL;
	//Test3 = HI_LEVEL;
	//Test1 = LO_LEVEL;
	//Test0 = LO_LEVEL;
    if ((Test4 == HI_LEVEL) && (Test3== HI_LEVEL) &&
	    (Test1 == LO_LEVEL) && (Test0== LO_LEVEL)){
	 //Falling edge detected 
	 Falling_edge_G = 1 ;
		}
	else {
	   //Default 
	   Falling_edge_G = 0 ;
	} 
	//Calculate average every 45 calls to this task 
	//- maximum count over this period is 9 pulses 
	//if (++Calls_G < 45 )
	//450 used here for test purposes (in simulator)
	//[Because there is a limit to how fast you can simulate pulses by hand...

	if(++Calls_G < 450){
	  Total_G += (int)Falling_edge_G;
	}
	else {
	  //update the display 
	  Data_G = Total_G; // Max is G 
	  Total_G = 0;
	  Calls_G = 0;
	  PULSE_COUNT_Check_Below_Threshold(3);
	  BARGRAPH_Update();
	}
	
	}

  /*-----------------------------------------------------------------*-
  ----------------END OF FILE -----------------------------------------
  -*------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -