switch_wait.c

来自「CODE for embedded C ,hand coding version」· C语言 代码 · 共 64 行

C
64
字号
/*--------------------------------------------------------------*-
 Switch_Wait.C(v1.00)
 Author:06_Digital_Media
 All Right Reserved
 -----------------------------------------------------------------
 Simple libray for debouncing a switch input

 Note : Duration of function is highly variable!

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

 #include "Main.H"
 #include "Port.H"
 #include "Switch_wait.H"
 #include "Delay_Loop.H"


 /*--------------------------------------------------------------*-
 SWITCH_Init()
 Initialization function for the switch library

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

 void SWITCH_Init(void){
  Switch_pin = 1; // use this pin for input

 }

 /*--------------------------------------------------------------*-
  SWITCH_Get_Input()
  Reads  and debounces  a mechanical switch as follows :
  1. If switch is not pressed ,return SWITCH_NOT_PRESSED
  2. If switch is pressed ,wait for DEBOUNCE_PERIOD (in ms)
     a. If switch is not pressed ,return SWITCH_NOT_PRESSED
	 b. If switch is pressed,wait (indefinitely) for  switch to be released 
	 then return SWITCH_PRESSED
  See Switch_Wait.H for details of return values

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

  bit SWITCH_Get_Input(const tByte DEBOUNCE_PERIOD){
   bit Return_value = SWITCH_NOT_PRESSED;
   if(Switch_pin == 0){
    //Switch is pressed
	//Debounce -just wait...
	DELAY_LOOP_Wait(DEBOUNCE_PERIOD);

	//Check switch again
	if(Switch_pin == 0){
	 //Wait until the switch is released
	 while(Switch_pin == 0);
	 Return_value = SWITCH_PRESSED;
	}
   
	}
     //Now (finally) return switch value
	  return Return_value;
 }

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

⌨️ 快捷键说明

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