ch32v307串口4影響串口2和串口3

使用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")));


串口4的中斷優(yōu)先級(jí)要低于串口2和串口3,串口2和串口3的中斷不能被串口4影響。


您好,PB0、PB1并不是串口4的默認(rèn)引腳,作為串口4引腳使用時(shí),注意程序中要配置開啟復(fù)用時(shí)鐘,同時(shí)配置引腳復(fù)用為串口4引腳。目前你的初始化配置有問題,需要修改一下,可參考下圖配置。

image.png


感謝回復(fù)!

串口4不能正常收發(fā)數(shù)據(jù)問題已解決。

但串口4依然影響了串口2和串口3,是否為優(yōu)先級(jí)問題導(dǎo)致?


程序開始設(shè)置了中斷組

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

串口2、3、4中斷處理函數(shù)均使用如下方式處理

void?UART4_IRQHandler(void)
{
????if(USART_GetITStatus(UART4,?USART_IT_RXNE)?!=?RESET)
????{
????????USART_ClearITPendingBit(UART4,?USART_IT_RXNE);?//?清除中斷標(biāo)志

????????u8?recv_buf?=?USART_ReceiveData(UART4);???//?存儲(chǔ)接收內(nèi)容
????????
????????//?處理接收到的數(shù)據(jù)
????}
}

具體影響表現(xiàn)為:

打開串口4的初始化,串口2、3就不能正常接收。注釋掉串口4的初始化,則串口2、3正常工作。


您好,我這邊測(cè)試3個(gè)串口同時(shí)進(jìn)行收發(fā)是沒有問題的,附件為測(cè)試?yán)?,可以參考一下。后續(xù)若有問題,可郵箱(lzs@wch.cn)溝通,若方便,可將你的測(cè)試工程發(fā)我看一下。

icon_rar.gifCH32V307 串口4_PB0_1.zip



只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频