代碼如下,設(shè)置中斷監(jiān)聽PD4上的按鈕按下事件,當(dāng)按鈕事件發(fā)生令LED閃爍(PD2), 但是當(dāng)我用上位機給mcu串口發(fā)送信息,也會觸發(fā)該中斷從而引發(fā)LED閃爍.串口引腳為PD5,PD6,USB轉(zhuǎn)串口的芯片為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); }
熱門產(chǎn)品 :
USB3.0 HUB控制器:CH634