STM32 BUTTON方式(polling & interrupt)

Publisher:masphiaLatest update time:2017-09-16 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Based on stm32 F401 discovery version to realize button polling & interrupt to light up LED


  1. /** 

  2.   ****************************************************************************** 

  3.   * @file    Template/main.c  

  4.   * @author  MCD Application Team 

  5.   * @version V1.0.0 

  6.   * @date    11-September-2013 

  7.   * @brief   Main program body 

  8.   ****************************************************************************** 

  9.   * @attention 

  10.   * 

  11.   * 

    © COPYRIGHT 2013 STMicroelectronics

     

  12.   * 

  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 

  14.   * You may not use this file except in compliance with the License. 

  15.   * You may obtain a copy of the License at: 

  16.   * 

  17.   *        http://www.st.com/software_license_agreement_liberty_v2 

  18.   * 

  19.   * Unless required by applicable law or agreed to in writing, software  

  20.   * distributed under the License is distributed on an "AS IS" BASIS,  

  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

  22.   * See the License for the specific language governing permissions and 

  23.   * limitations under the License. 

  24.   * 

  25.   ****************************************************************************** 

  26.   */  

  27.   

  28.   

  29. /* Includes ------------------------------------------------------------------*/  

  30. #include "main.h"  

  31.   

  32. /** @addtogroup Template 

  33.   * @{ 

  34.   */  

  35.   

  36. /* Private typedef -----------------------------------------------------------*/  

  37. /* Private define ------------------------------------------------------------*/  

  38. #define USER_BUTTON_INTERRUPT  

  39. #define KEY_ON  0  

  40. #define KEY_OFF 1  

  41. /* Private macro -------------------------------------------------------------*/  

  42. /* Private variables ---------------------------------------------------------*/  

  43. /* Private function prototypes -----------------------------------------------*/  

  44. static void Delay(__IO uint32_t nCount);  

  45. uint8_t Key_Scan(GPIO_TypeDef* GPIOx,u16 GPIO_Pin);  

  46. /* Private functions ---------------------------------------------------------*/  

  47.   

  48.   

  49. /** 

  50.   * @brief  Main program 

  51.   * @param  None 

  52.   * @retval None 

  53.   */  

  54. int main(void)  

  55. {  

  56.   /*!< At this stage the microcontroller clock setting is already configured,  

  57.   this is done through SystemInit() function which is called from startup 

  58.   file (startup_stm32f401xx.s) before to branch to application main. 

  59.   To reconfigure the default setting of SystemInit() function, refer to 

  60.   system_stm32f4xx.c file 

  61.   */  

  62.   /* Add your application code here */  

  63.   /* Initialize LEDS */  

  64.     STM_EVAL_LEDInit(LED3);  

  65.     STM_EVAL_LEDInit(LED4);  

  66.     STM_EVAL_LEDInit(LED5);  

  67.     STM_EVAL_LEDInit(LED6);  

  68.       

  69. #ifdef USER_BUTTON_INTERRUPT  

  70.     /*Initialize the button interrupt*/  

  71.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  

  72.     STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);  

  73. #endif  

  74.    

  75. #ifdef UAER_BUTTON_POLLING  

  76.      /*Initialize the button normal*/  

  77.     STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);  

  78.     while(1)                              

  79.     {        

  80.     if( Key_Scan(GPIOA,GPIO_Pin_0) == KEY_ON  )  

  81.     {  

  82.             /*LED1·′×a*/  

  83.             STM_EVAL_LEDToggle(LED3);  

  84.     }     

  85.       }  

  86. #endif  

  87.       

  88. #ifdef LED_ON_OFF  

  89.     while (1)  

  90.     {  

  91.       STM_EVAL_LEDOn(LED3);  

  92.   

  93.       

  94.     /* Insert delay */  

  95.     Delay(0x3FFFFF);  

  96.       

  97.     /* PD13 to be toggled */  

  98.      STM_EVAL_LEDOn(LED4);  

  99.       

  100.     /* Insert delay */  

  101.     Delay(0x3FFFFF);  

  102.     

  103.     /* PD14 to be toggled */  

  104.      STM_EVAL_LEDOn(LED5);  

  105.       

  106.     /* Insert delay */  

  107.     Delay(0x3FFFFF);  

  108.       

  109.     /* PD15 to be toggled */  

  110.      STM_EVAL_LEDOn(LED6);  

  111.       

  112.     /* Insert delay */  

  113.     Delay(0x7FFFFF);  

  114.       

  115.     STM_EVAL_LEDOff(LED3);  

  116.     STM_EVAL_LEDOff(LED4);  

  117.     STM_EVAL_LEDOff(LED5);  

  118.     STM_EVAL_LEDOff(LED6);  

  119.     //GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);  

  120.       

  121.     /* Insert delay */  

  122.     Delay(0xFFFFFF);  

  123.   }  

  124. #endif   

  125.   /* Infinite loop */  

  126.   while (1)  

  127.   {  

  128.   }  

  129. }  

  130.   

  131.   

  132.   

  133.   

  134. #ifdef  USE_FULL_ASSERT  

  135.   

  136. /** 

  137.   * @brief  Reports the name of the source file and the source line number 

  138.   *         where the assert_param error has occurred. 

  139.   * @param  file: pointer to the source file name 

  140.   * @param  line: assert_param error line source number 

  141.   * @retval None 

  142.   */  

  143. void assert_failed(uint8_t* file, uint32_t line)  

  144. {   

  145.   /* User can add his own implementation to report the file name and line number, 

  146.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  

  147.   

  148.   /* Infinite loop */  

  149.   while (1)  

  150.   {  

  151.   }  

  152. }  

  153. #endif  

  154.   

  155. /** 

  156.   * @} 

  157.   */   

  158. /** 

  159.   * @brief  Delay Function. 

  160.   * @param  nCount:specifies the Delay time length. 

  161.   * @retval None 

  162.   */  

  163. void Delay(__IO uint32_t nCount)  

  164. {  

  165.   while(nCount--)  

  166.   {  

  167.   }  

  168. }  

  169. /** 

  170.   * @brief ?ì2aê?·?óD°′?ü°′?? 

  171.   * @param   ??ì?μ????úoí???ú?? 

  172.   *     @arg GPIOx: x?éò?ê?£¨A...G£?  

  173.   *     @arg GPIO_PIN ?éò?ê?GPIO_PIN_x£¨x?éò?ê?1...16£? 

  174.   * @retval °′?üμ?×′ì? 

  175.   *     @arg KEY_ON:°′?ü°′?? 

  176.   *     @arg KEY_OFF:°′?ü??°′?? 

  177.   */  

  178. uint8_t Key_Scan(GPIO_TypeDef* GPIOx,u16 GPIO_Pin)  

  179. {             

  180.     /*?ì2aê?·?óD°′?ü°′?? */  

  181.     if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )   

  182.     {        

  183.         /*?óê±????*/  

  184.         Delay(10000);         

  185.         if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )    

  186.         {      

  187.             /*μè′y°′?üêí·? */  

  188.             while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);     

  189.             return  KEY_ON;    

  190.         }  

  191.         else  

  192.             return KEY_OFF;  

  193.     }  

  194.     else  

  195.         return KEY_OFF;  

  196. }  

  197.   

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


Polling is a while loop that monitors registers. The interrupt method is:


Interrupt triggered by external interrupt line 0


  1. void EXTI0_IRQHandler(void)  

  2. {  

  3.     if(EXTI_GetITStatus(EXTI_Line0) != RESET) //è·±£ê?·?2úéúá?EXTI Line?D??  

  4.     {  

  5.         // LED1 is?·′          

  6.         STM_EVAL_LEDToggle(LED6);  

  7.         EXTI_ClearITPendingBit(EXTI_Line0); //??3y?D??±ê????  

  8.     }    

  9. }  


Because the USER button is the PA0 pin, the interrupt line is EXIT0


The whole project is connected as follows:

STM32 button lights up LED


Keywords:STM32 Reference address:STM32 BUTTON方式(polling & interrupt)

Previous article:STM32 NVIC preemption priority and response priority
Next article:STM32 UART (receive, send data)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号