This post was last edited by donatello1996 on 2018-1-18 23:53 External interrupts and timer interrupts are both very important and commonly used interrupts in microcontrollers. The purpose of external interrupts is to avoid system polling to detect changes in GPIO levels, which is particularly important when detecting key input and sensor status; while timer interrupts start a timing independent of the main loop, which is usually used to perform some tasks with high real-time requirements, such as detecting sensor readings. The STM32 HAL library is very user-friendly in the processing of interrupt service functions. Combined with CubeMX software, the initialization of all timer interrupts and external interrupts is simply a fool-proof operation. The main frequency and storage capacity of STM32 are both large, so the timer can be set in a very large range and with high precision. There is a blue user button on our L4+ board, which is connected to PC13 and VCC, so the parameters that need to be initialized are the EXTI15_10_IRQn interrupt line and PC13 - rising edge trigger - pull-down or floating input, external interrupt trigger print serial port information and flip the red LED light (PB14):
void EXTI15_10_IRQHandler() { if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != RESET) { printf("Press the blue button, enter the external interrupt, and flip the red LED light.\n"); HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_14); __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13); } }
复制代码
Configure the timer to count up mode, without frequency division, and load the value to 5000, that is, 0.5 seconds overflow. The timer interrupt triggers the printing of serial port information and flips the blue LED light (PB7): void TIM3_Init(int arr,int psc) { __HAL_RCC_TIM3_CLK_ENABLE(); TIM3_Handler.Instance=TIM3; TIM3_Handler.Init.Prescaler=psc; TIM3_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; TIM3_Handler.Init.Period=arr; TIM3_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&TIM3_Handler); HAL_TIM_Base_Start_IT(&TIM3_Handler); HAL_NVIC_SetPriority(TIM3_IRQn,1,2); HAL_NVIC_EnableIRQ(TIM3_IRQn); }[/code]
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim==(&TIM3_Handler)) { printf("Timer 3 is finished, turn on the green LED light." \n\n\n"); HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_7); } }