代码如下,设置中断监听PD4上的按钮按下事件,当按钮事件发生令LED闪烁(PD2), 但是当我用上位机给mcu串口发送信息,也会触发该中断从而引发LED闪烁.串口引脚为PD5,PD6,USB转串口的芯片为CH340E.
代码如下:
/********************************** (C) COPYRIGHT ******************************* * File Name : main.c * Author : WCH * Version : V1.0.0 * Date : 2022/08/08 * Description : Main program body. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. * SPDX-License-Identifier: Apache-2.0 *******************************************************************************/ #include "debug.h" void INIT() { INTERRUPT_INIT(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); GPIO_INIT(GPIOD,GPIO_Pin_2, GPIO_Mode_Out_PP); GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET); } void INTERRUPT_INIT(){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE); GPIO_InitTypeDef GPIO_InitStructure = {0}; EXTI_InitTypeDef EXTERNAL_EXTI_InitStructure = {0}; NVIC_InitTypeDef NVIC_InitStructure = {0}; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // pull down GPIO_Init(GPIOD, &GPIO_InitStructure); /* PD4 ----> EXTI_Line4 */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource4); EXTERNAL_EXTI_InitStructure.EXTI_Line = EXTI_Line4; EXTERNAL_EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTERNAL_EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTERNAL_EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTERNAL_EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI7_0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void EXTI7_0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); /********************************************************************* * @fn EXTI0_IRQHandler * * @brief This function handles EXTI0 Handler. * * @return none */ void EXTI7_0_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line4)!=RESET) { EXTI_ClearITPendingBit(EXTI_Line4); /* Clear Flag */ LED_Blink(); } } void LED_Blink(){ u8 i = 6; while(i--){ GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET); Delay_Ms(300); GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET); Delay_Ms(300); } } /********************************************************************* * @fn GPIO_INIT * * @brief Initializes GPIO by given params * * @return none */ void GPIO_INIT(GPIO_TypeDef *GPIOx, u16 PIN_NUM, GPIOMode_TypeDef IO_MODE) { GPIO_InitTypeDef GPIO_InitStructure = {0}; GPIO_InitStructure.GPIO_Pin = PIN_NUM; GPIO_InitStructure.GPIO_Mode = IO_MODE; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOx, &GPIO_InitStructure); } /********************************************************************* * @fn main * * @brief Main program. * * @return none */ int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); Delay_Init(); INIT(); while(1); }
热门产品 :
CH182: 以太网PHY收发器芯片