一个不错的按钮检测程序

1 配置系统滴答定时器

/**************************************************************************
* º¯ÊýÃû  	: SystickInitialisation
* º¯ÊýÃèÊö    	: ÉèÖÃSystick¶¨Ê±Æ÷,ÖØ×°ÔØʱ¼äΪ20ms
* ÊäÈë²ÎÊý      : ÎÞ
* Êä³ö½á¹û      : ÎÞ
* ·µ»ØÖµ        : ÎÞ
**************************************************************************/
void SystickInitialisation(void)
{
	if (SysTick_Config(72000 * 20))	// ST3.5.0¿â°æ±¾ 20ms¶¨Ê±
	{ 
		/* Capture error */ 
		while (1);
	}
}
配置的时候系统函数已经配置好中断
2 设置中断标志
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval : None
  */
void SysTick_Handler(void)
{
	sign_20ms = 1;
}
3  按钮检测程序有一个标志代表按钮时间也就是自己可以设置长按和短按
<pre name="code" class="cpp">int main(void)
{
	vu16 KeyPortStatus = 0;
	/* ¶¨Òå°´¼üɨÃè״̬ö¾Ù±äÁ¿ */
    KeyScanState_Typedef KeyScanState;
	
   RCC_Configuration();	//ϵͳʱÖÓ³õʼ»¯
   GPIO_Configuration();//¶Ë¿Ú³õʼ»¯
	SystickInitialisation();
    GPIO_SetBits(GPIOB,GPIO_Pin_5);
	while(1)
	{
		#if 1
					if(sign_20ms == Bit_SET)
					{
						sign_20ms = 0;
            /* ¶ÁÈ¡IOµçƽ */
            KeyPortStatus = GPIO_ReadInputDataBit(KEYPORT,KEY0PIN) & 0x0001;
            /* ½øÈë״̬»úÁ÷³Ì */
            switch(KeyScanState)
            {
                /* ״̬1£ºÅжÏÓзñ°´¼ü°´Ï */	
            case KeyScanState_0:
                {
                    if(KeyPortStatus != 0x0001)
                    {
                        /* Óа´¼ü°´Ï£¬¸üÐÂ״̬±êÖ¾ */
                    	KeyScanState = KeyScanState_1;
                    }
                    break;
                }
                /* ״̬2£ºÅжÏÊÇ·ñ¶¶¶¯ */
            case KeyScanState_1:
                {
                    if(!KeyPortStatus)
                    {
                        /* ·Ç¶¶¶¯£¬È·ÈÏ°´¼ü°´Ï£¬Ö´ÐÐÏàÓ¦²Ù×÷ */
                        if(GPIO_ReadInputDataBit(KEYPORT, KEY0PIN) == 0)
                        {
//                            GPIO_WriteBit(LEDPORT, 
//                                          LED1PIN, 
//                                          (BitAction)(1 - GPIO_ReadOutputDataBit(LEDPORT, LED1PIN)));
														sign_keypress ++;
                        }
                        /* ¸üÐÂ״̬±êÖ¾ */
                        KeyScanState = KeyScanState_2;
                    }
                    else
                    {
                        /* ¶¶¶¯£¬È·ÈÏ°´¼üδ°´Ï£¬¸üÐÂ״̬±êÖ¾*/
                        KeyScanState = KeyScanState_0;
                    }
                    break;
                }
                /* ״̬3£ºËÉÊÖ¼ì²â */
            case KeyScanState_2:
                {
                    if(KeyPortStatus == 0x0001)
                    {
												/* ËÉÊÖ£¬¸üÐÂ״̬±êÖ¾ */
												KeyScanState = KeyScanState_0;
                    }
										else
										{
												KeyScanState = KeyScanState_1;
										}
                    break;
                }
            }
        }
				
				if(sign_keypress == 150)
				{
					sign_keypress = 0;
					GPIO_ResetBits(GPIOB,GPIO_Pin_5);
				}
				else
				{

				}
				#endif
		#if 0
			if(sign_20ms == Bit_SET)
			{
				if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET)
				{
					//LED ·¢¹â
					GPIO_ResetBits(GPIOB,GPIO_Pin_5);
				}
				else
				{
					 //LED ϨÃð
					 GPIO_SetBits(GPIOB,GPIO_Pin_5);
				}
		}
		#endif
	}
}


 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值