使用ch32v307的串口2中斷和串口3中斷,接收發(fā)送數(shù)據(jù)均無問題,但是添加串口4(也使用中斷)后,串口2和串口3被影響不能正常工作。
串口2、串口3為USART,串口4為UART。
串口2初始化為
void?USART2_init(void) { ????GPIO_InitTypeDef??GPIO_InitStructure?=?{0}; ????USART_InitTypeDef?USART_InitStructure?=?{0}; ????NVIC_InitTypeDef??NVIC_InitStructure?=?{0}; ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,?ENABLE); ????//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,?ENABLE); ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,?ENABLE); ????/*?USART2?TX-->A.2??RX-->A.3?*/ ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_2; ????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP; ????GPIO_Init(GPIOA,?&GPIO_InitStructure); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_3; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(GPIOA,?&GPIO_InitStructure); ????USART_InitStructure.USART_BaudRate?=?115200; ????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b; ????USART_InitStructure.USART_StopBits?=?USART_StopBits_1; ????USART_InitStructure.USART_Parity?=?USART_Parity_No; ????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None; ????USART_InitStructure.USART_Mode?=?USART_Mode_Tx?|?USART_Mode_Rx; ????USART_Init(USART2,?&USART_InitStructure); ????USART_ITConfig(USART2,?USART_IT_RXNE,?ENABLE); ????NVIC_InitStructure.NVIC_IRQChannel?=?USART2_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?2; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?1; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); ????USART_Cmd(USART2,?ENABLE); }
串口3初始化為:
void?USART3_init(void) { ????GPIO_InitTypeDef??GPIO_InitStructure?=?{0}; ????USART_InitTypeDef?USART_InitStructure?=?{0}; ????NVIC_InitTypeDef??NVIC_InitStructure?=?{0}; ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,?ENABLE); ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,?ENABLE); ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,?ENABLE); ????USART_DeInit(USART3); ????//?USART3??TX--C10??RX--C11 ????GPIO_PinRemapConfig(GPIO_PartialRemap_USART3,?ENABLE); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_10; ????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP; ????GPIO_Init(GPIOC,?&GPIO_InitStructure); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_11; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(GPIOC,?&GPIO_InitStructure); ????USART_InitStructure.USART_BaudRate?=?115200; ????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b; ????USART_InitStructure.USART_StopBits?=?USART_StopBits_1; ????USART_InitStructure.USART_Parity?=?USART_Parity_No; ????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None; ????USART_InitStructure.USART_Mode?=?USART_Mode_Tx?|?USART_Mode_Rx; ????USART_Init(USART3,?&USART_InitStructure); ????USART_ITConfig(USART3,?USART_IT_RXNE,?ENABLE); ????NVIC_InitStructure.NVIC_IRQChannel?=?USART3_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?2; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?2; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); ????USART_Cmd(USART3,?ENABLE); }
串口4初始化為:
void?uart4_init(void) { ????GPIO_InitTypeDef??GPIO_InitStructure?=?{0}; ????USART_InitTypeDef?USART_InitStructure?=?{0}; ????NVIC_InitTypeDef??NVIC_InitStructure?=?{0}; ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,?ENABLE); ????//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,?ENABLE); ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,?ENABLE); ????/*?UART4?TX-->B.1??RX-->B.0?*/ ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_1; ????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP; ????GPIO_Init(GPIOB,?&GPIO_InitStructure); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_0; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(GPIOB,?&GPIO_InitStructure); ????USART_InitStructure.USART_BaudRate?=?115200; ????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b; ????USART_InitStructure.USART_StopBits?=?USART_StopBits_1; ????USART_InitStructure.USART_Parity?=?USART_Parity_No; ????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None; ????USART_InitStructure.USART_Mode?=?USART_Mode_Tx?|?USART_Mode_Rx; ????USART_Init(UART4,?&USART_InitStructure); ????USART_ITConfig(UART4,?USART_IT_RXNE,?ENABLE); ????NVIC_InitStructure.NVIC_IRQChannel?=?UART4_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?2;?//?搶占優(yōu)先級(jí) ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?3;?//?子優(yōu)先級(jí) ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); ????USART_Cmd(UART4,?ENABLE); }
串口4使用兩種聲明均一樣會(huì)影響串口2和串口3:
void?USART2_IRQHandler(void)?__attribute__((interrupt())); void?USART3_IRQHandler(void)?__attribute__((interrupt())); //void?USART4_IRQHandler(void)?__attribute__((interrupt())); void?UART4_IRQHandler(void)?__attribute__((interrupt("WCH-Interrupt-fast")));