問(wèn)題描述:我想打開(kāi)串口1~8 然后將串口收到的數(shù)據(jù)原路發(fā)回,打開(kāi)1、2、3、7串口發(fā)送和接收都正常,4、5、6串口能收到數(shù)據(jù),但是發(fā)不出去,8串口收不到也發(fā)不出
調(diào)試方法:串口轉(zhuǎn)usb工具連接pc,tx和rx按照下方式連接,MounRiver Studio運(yùn)行程序,串口調(diào)試工具打開(kāi)串口,發(fā)送和接收數(shù)據(jù)
????????????UART1 TX-->PA9 RX-->PA10
????????????UART2 TX-->PA2 RX-->PA3
????????????UART3 TX-->PB10 RX-->PB11
????????????UART4 TX-->PC10 RX-->PC11
????????????UART5 TX-->PC12 RX-->PD2
????????????UART6 TX-->PC0 RX-->PC1?
????????????UART7 TX-->PC2 RX-->PC3
????????????UART8 TX-->PC4 RX-->PC5
代碼:串口4中斷? 其他串口也基本是這樣?
//串口中斷服務(wù)程序
__attribute__((interrupt("WCH-Interrupt-fast")))
void UART4_IRQHandler(void)
{
? ? ? ? u8 Res;
? ? ? ? if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
? ? ? ? {
? ? ? ? ? ? ? ? Res =USART_ReceiveData(UART4);//讀取接收到的數(shù)據(jù)
? ? ? ? ? ? ? ? USART_SendData(UART4, Res);
? ? ? ? }
}
初始化代碼:
void?uart_init(void)
{
????//GPIO端口設(shè)置
????GPIO_InitTypeDef?GPIO_InitStructure;
????USART_InitTypeDef?USART_InitStructure;
????NVIC_InitTypeDef?NVIC_InitStructure;
????RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3,?ENABLE);
????RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,?ENABLE);
????RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,?ENABLE);
????RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART6,?ENABLE);
????RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7,?ENABLE);
????RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8,?ENABLE);
????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO?,?ENABLE);
????///初始化串口1-串口8的io?UART1~UART8
????/*?UART1?TX-->PA9???RX-->PA10?*/
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_9;
????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_10;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IPU;
????GPIO_Init(GPIOA,?&GPIO_InitStructure);
????/*?UART2?TX-->PA2???RX-->PA3?*/
????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_IPU;
????GPIO_Init(GPIOA,?&GPIO_InitStructure);
????/*?UART3?TX-->PB10??RX-->PB11?*/
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_10;
????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_11;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IPU;
????GPIO_Init(GPIOB,?&GPIO_InitStructure);
????/*?UART4?TX-->PC10??RX-->PC11?*/
????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_IPU;
????GPIO_Init(GPIOC,?&GPIO_InitStructure);
????/*?UART5?TX-->PC12??RX-->PD2?*/
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_12;
????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_2;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IPU;
????GPIO_Init(GPIOD,?&GPIO_InitStructure);
????/*?UART6?TX-->PC0??RX-->PC1??|??TX_2-->PE10???RX_2-->PE11?*/
//???? GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_0;
//???? 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_1;
//???? GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IPU;
//???? GPIO_Init(GPIOC,?&GPIO_InitStructure);
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_10;
????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP;
????GPIO_Init(GPIOE,?&GPIO_InitStructure);
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_11;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IPU;
????GPIO_Init(GPIOE,?&GPIO_InitStructure);
????GPIO_PinRemapConfig(GPIO_FullRemap_USART6,ENABLE);
????/*?UART7?TX-->PC2??RX-->PC3?*/
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_2;
????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_3;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IPU;
????GPIO_Init(GPIOC,?&GPIO_InitStructure);
????/*?UART8?TX-->PC4??RX-->PC5?*/
????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_4;
????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_5;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AIN;
????GPIO_Init(GPIOC,?&GPIO_InitStructure);
????//USART?初始化設(shè)置
????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b;//字長(zhǎng)為8位數(shù)據(jù)格式
????USART_InitStructure.USART_StopBits?=?USART_StopBits_1;//一個(gè)停止位
????USART_InitStructure.USART_Parity?=?USART_Parity_No;//無(wú)奇偶校驗(yàn)位
????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None;//無(wú)硬件數(shù)據(jù)流控制
????USART_InitStructure.USART_Mode?=?USART_Mode_Rx?|?USART_Mode_Tx; //收發(fā)模式
????USART_InitStructure.USART_BaudRate?=?115200;//串口波特率
????USART_Init(USART1,?&USART_InitStructure);?//初始化串口1
????USART_Init(USART2,?&USART_InitStructure);?//初始化串口2
????USART_Init(USART3,?&USART_InitStructure);?//初始化串口3
????USART_Init(UART4,?&USART_InitStructure);?//初始化串口4
????USART_Init(UART5,?&USART_InitStructure);?//初始化串口5
????USART_Init(UART6,?&USART_InitStructure);?//初始化串口6
????USART_Init(UART7,?&USART_InitStructure);?//初始化串口7
????USART_Init(UART8,?&USART_InitStructure);?//初始化串口8
????/*串口--1*/
????NVIC_InitStructure.NVIC_IRQChannel?=?USART1_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//信道搶占優(yōu)先級(jí)
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?3;//信道子優(yōu)先級(jí)
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--2*/
????NVIC_InitStructure.NVIC_IRQChannel?=?USART2_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--3*/
????NVIC_InitStructure.NVIC_IRQChannel?=?USART3_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);?//根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--4*/
????NVIC_InitStructure.NVIC_IRQChannel?=?UART4_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);?//根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--5*/
????NVIC_InitStructure.NVIC_IRQChannel?=?UART5_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);?//根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--6*/
????NVIC_InitStructure.NVIC_IRQChannel?=?UART6_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);?//根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--7*/
????NVIC_InitStructure.NVIC_IRQChannel?=?UART7_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);?//根據(jù)指定的參數(shù)初始化VIC寄存器
????/*串口--8*/
????NVIC_InitStructure.NVIC_IRQChannel?=?UART8_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;
????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE;
????NVIC_Init(&NVIC_InitStructure);?//根據(jù)指定的參數(shù)初始化VIC寄存器
????USART_ITConfig(USART1,?USART_IT_RXNE,?ENABLE);//開(kāi)啟串口接受中斷
????USART_ITConfig(USART2,?USART_IT_RXNE,?ENABLE);//開(kāi)啟串口接受中斷
????USART_ITConfig(USART3,?USART_IT_RXNE,?ENABLE);
????USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
????USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
????USART_ITConfig(UART6,USART_IT_RXNE,ENABLE);
????USART_ITConfig(UART7,USART_IT_RXNE,ENABLE);
????USART_ITConfig(UART8,USART_IT_RXNE,ENABLE);
????USART_ITConfig(USART1,?USART_IT_IDLE,?ENABLE);//空閑中斷
????USART_Cmd(USART1,?ENABLE);????????????????????//使能串口
????USART_Cmd(USART2,?ENABLE);????????????????????//使能串口
????USART_Cmd(USART3,?ENABLE);
????USART_Cmd(UART4,?ENABLE);
????USART_Cmd(UART5,?ENABLE);
????USART_Cmd(UART6,?ENABLE);
????USART_Cmd(UART7,?ENABLE);
????USART_Cmd(UART8,?ENABLE);
}