2949 views|0 replies

156

Posts

1

Resources
The OP
 

【NUCLEO-L4R5ZI Review】External Interrupt & Timer Interrupt [Copy link]

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):
  1. void EXTI15_10_IRQHandler_Config() { __HAL_RCC_GPIOC_CLK_ENABLE(); GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = GPIO_PIN_13; HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); HAL_NVIC_SetPriority(EXTI15_10_IRQn,2,0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); }
复制代码
  1. 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]
  1. 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); } }
复制代码
Effect picture: Project file: 工程文件.zip (1.65 MB, downloads: 44)



This post is from stm32/stm8
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list