这里使用的外部中断函数宏定义板子上的LED和按键的引脚
- #define LED_GPIO_PORT CW_GPIOB
- #define LED_GPIO_PINS GPIO_PIN_8 | GPIO_PIN_9
- #define KEY_GPIO_PORT CW_GPIOA
- #define KEY_GPIO_PINS GPIO_PIN_1 | GPIO_PIN_2
使能时钟,并配置端口和中断
- __RCC_GPIOA_CLK_ENABLE(); // 使能GPIO的配置时钟
- __RCC_GPIOB_CLK_ENABLE();
- GPIO_InitStruct.IT = GPIO_IT_RISING | GPIO_IT_FALLING;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pins = KEY_GPIO_PINS;
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
- GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStruct);
- GPIO_InitStruct.IT = GPIO_IT_NONE;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pins = LED_GPIO_PINS;
- GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
- //清除PA00中断标志并使能NVIC
- GPIOA_INTFLAG_CLR(bv1| bv2);
- NVIC_EnableIRQ(GPIOA_IRQn);
添加中断函数
- void GPIOA_IRQHandler(void)
- {
- /* USER CODE BEGIN */
- GPIOA_IRQHandlerCallback();
- /* USER CODE END */
- }
响应代码
- void GPIOA_IRQHandlerCallback(void)
- {
- if (CW_GPIOA->ISR_f.PIN1)
- {
- GPIOA_INTFLAG_CLR(bv1);
- PB09_TOG();
- }
- if (CW_GPIOA->ISR_f.PIN2)
- {
- GPIOA_INTFLAG_CLR(bv2);
- PB08_TOG();
- }
- }
|