STM32 BUTTON方式(polling & interrupt)

来源:互联网 发布:易语言攻击集群源码 编辑:程序博客网 时间:2024/06/05 02:46

基于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_ON0#define KEY_OFF1/* 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; }elsereturn KEY_OFF;}elsereturn 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


0 0
原创粉丝点击