STM32 BUTTON方式(polling & interrupt)

基于stm32 F401 discovery版实现button polling & interrupt点亮LED

/**
  ******************************************************************************
  * @file    Template/main.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    11-September-2013
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define USER_BUTTON_INTERRUPT
#define KEY_ON	0
#define KEY_OFF	1
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void Delay(__IO uint32_t nCount);
uint8_t Key_Scan(GPIO_TypeDef* GPIOx,u16 GPIO_Pin);
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f401xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f4xx.c file
  */
  /* Add your application code here */
  /* Initialize LEDS */
    STM_EVAL_LEDInit(LED3);
    STM_EVAL_LEDInit(LED4);
    STM_EVAL_LEDInit(LED5);
    STM_EVAL_LEDInit(LED6);
    
#ifdef USER_BUTTON_INTERRUPT
    /*Initialize the button interrupt*/
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
#endif
 
#ifdef UAER_BUTTON_POLLING
     /*Initialize the button normal*/
    STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
    while(1)                            
    {	   
	if( Key_Scan(GPIOA,GPIO_Pin_0) == KEY_ON  )
	{
            /*LED1·′×a*/
            STM_EVAL_LEDToggle(LED3);
	}   
      }
#endif
    
#ifdef LED_ON_OFF
    while (1)
    {
      STM_EVAL_LEDOn(LED3);

	
    /* Insert delay */
    Delay(0x3FFFFF);
    
    /* PD13 to be toggled */
     STM_EVAL_LEDOn(LED4);
    
    /* Insert delay */
    Delay(0x3FFFFF);
  
    /* PD14 to be toggled */
     STM_EVAL_LEDOn(LED5);
    
    /* Insert delay */
    Delay(0x3FFFFF);
    
    /* PD15 to be toggled */
     STM_EVAL_LEDOn(LED6);
    
    /* Insert delay */
    Delay(0x7FFFFF);
    
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOff(LED4);
    STM_EVAL_LEDOff(LED5);
    STM_EVAL_LEDOff(LED6);
    //GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
    
    /* Insert delay */
    Delay(0xFFFFFF);
  }
#endif 
  /* Infinite loop */
  while (1)
  {
  }
}




#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */ 
/**
  * @brief  Delay Function.
  * @param  nCount:specifies the Delay time length.
  * @retval None
  */
void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}
/**
  * @brief   ?ì2aê?·?óD°′?ü°′??
  * @param   ??ì?μ????úoí???ú??
  *		@arg GPIOx: x?éò?ê?£¨A...G£? 
  *		@arg GPIO_PIN ?éò?ê?GPIO_PIN_x£¨x?éò?ê?1...16£?
  * @retval  °′?üμ?×′ì?
  *		@arg KEY_ON:°′?ü°′??
  *		@arg KEY_OFF:°′?ü??°′??
  */
uint8_t Key_Scan(GPIO_TypeDef* GPIOx,u16 GPIO_Pin)
{			
	/*?ì2aê?·?óD°′?ü°′?? */
	if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON ) 
	{	   
		/*?óê±????*/
		Delay(10000);		
		if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )  
		{	 
			/*μè′y°′?üêí·? */
			while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);   
			return 	KEY_ON;	 
		}
		else
			return KEY_OFF;
	}
	else
		return KEY_OFF;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

polling是一直while循环监测寄存器,interrupt方式是:

通过外部中断线0出发中断

void EXTI0_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line0) != RESET) //è·±£ê?·?2úéúá?EXTI Line?D??
	{
		// LED1 è?·′		
		STM_EVAL_LEDToggle(LED6);
		EXTI_ClearITPendingBit(EXTI_Line0);     //??3y?D??±ê????
	}  
}

因为USER button是PA0 pin,所以中断线是EXIT0

整个工程如连接:

STM32 button 点亮LED


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wireless_Link

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值