Based on stm32 F401 discovery version to realize button polling & interrupt to light up LED
/**
******************************************************************************
* @file Template/main.c
* @author MCD Application Team
* @version V1.0.0
* @date 11-September-2013
* @brief Main program body
******************************************************************************
* @attention
*
*
© COPYRIGHT 2013 STMicroelectronics *
* 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 is a while loop that monitors registers. The interrupt method is:
Interrupt triggered by external interrupt line 0
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET) //è·±£ê?·?2úéúá?EXTI Line?D??
{
// LED1 is?·′
STM_EVAL_LEDToggle(LED6);
EXTI_ClearITPendingBit(EXTI_Line0); //??3y?D??±ê????
}
}
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
Previous article:STM32 NVIC preemption priority and response priority
Next article:STM32 UART (receive, send data)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- [RVB2601 debug record] The function in the c file is not declared in the header file
- Shocked, how can two three-stage tubes make such a posture...
- Analyze several key technologies and application scenarios of the Internet of Things, and understand 5G in seconds
- [Raspberry Pi Pico Review] On-chip temperature detection
- STM32F407 simple vision board (recognition of QR codes, data matrix codes, apriltags, round and square shape detection)
- 【ST NUCLEO-G071RB Review】PWR
- There are many tricks for salary cuts and layoffs. What should you do if you encounter such a situation?
- [AB32VG1 development board review] TF card and file reading and writing applications
- How to measure power supply rejection ratio?
- 280049Solution to the problem of LaunchPad emulator not being able to connect