External interruption
- In STM32, each IO pin can be used as an external interrupt input pin.
- However, a number of slices can not configure the same interrupt line to receive IO state ( rise, drop, speed, etc. ), so, how to make the break and a large number of IO pins to pair it?
In the configuration shown above broken corresponding IO pin, the number of interrupt lines greatly reduced.
General configuration of external interrupt
1, since it is configured IO foot line, foot clock that IO is the first need to configure.
__HAL_RCC_GPIOA_CLK_ENABLE (); // open GPIOA clock __HAL_RCC_GPIOC_CLK_ENABLE (); // open GPIOC clock __HAL_RCC_GPIOH_CLK_ENABLE (); // open clock GPIOH
2. Once you've configured IO foot of the clock, then the state is required to configure IO pin. (Up, pull-down, speed, etc.)
IO pin is configured state, you need to use HAL_GPIO_Init (GPIOA, & GPIO_Initure); this function
GPIO_Initure.Pin = GPIO_PIN_0; // PA0 GPIO_Initure.Mode = GPIO_MODE_IT_RISING; // GPIO_Initure.Pull = GPIO_PULLDOWN; // 下拉 GPIO_Initure.Speed = GPIO_SPEED_HIGH; // 高速 HAL_GPIO_Init (GPIOA and GPIO_Initure);
3, after the work-related IO pin configuration, the configuration can be interrupted, to sort out. From the first priority can interrupt enable and interrupt start.
HAL_NVIC_EnableIRQ(EXTI0_IRQn); // HAL_NVIC_SetPriority(EXTI0_IRQn,2,0); // HAL_NVIC_EnableIRQ(EXTI2_IRQn); // HAL_NVIC_SetPriority(EXTI2_IRQn,2,1); // HAL_NVIC_EnableIRQ(EXTI3_IRQn); // HAL_NVIC_SetPriority(EXTI3_IRQn,2,2); //
4, needs to be interrupted, then it needs external interrupt function to handle the IO pin outages, STM32 provides a common external interrupt handler. HAL_GPIO_EXTI_IRQHandler (GPIO_PIN_0)
void EXTI0_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); } void EXTI2_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2); }
5, calls the interrupt handler is completed, after obtaining the interrupt source, the need for further external interrupt callback function to process the data. void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin)
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { delay_ms(100); switch(GPIO_Pin) { case GPIO_PIN_0: //WAKE UP if(WK_UP == 1) LED1=!LED1; LED0=!LED1; break; // case GPIO_PIN_13: //KEY2 if(KEY2 == 0) { LED0=!LED0; } break; // case GPIO_PIN_2: //KEY1 if(KEY1 == 0) { LED1=!LED1; } break; // case GPIO_PIN_3: //KEY0 if(KEY0 == 0) { LED0=!LED0; LED1=!LED1; } break; // } }
After several functions written in the header file EXTI.C in. Main.c function can be called in.
int main ( void ) { HAL_Init (); // initialize HAL library Stm32_Clock_Init ( 360 , 25 , 2 , . 8 ); // set the clock, 180MHz delay_init ( 180 [ ); // initialize delay function uart_init ( 115200 ); // initialization The USART LED_Init (); // initialize the LED KEY_Init (); // initialize key EXTI_Init (); // external interrupt initialization while(1) { printf("ok\r\n"); delay_ms(1000); } }
2019-09-25