本人遇到如下问题,按照下面的库函数调用,单独打开一个串口,完全正常,但是同时打开USART1 USART2时,USART2不工作,不知道何故,请高人指点一二//-------------------------------------------初始化部分----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void GPIO_System_Initrial()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA,ENABLE); //串口部分初始化和LED显示部分的初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //LED全部输出状态。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出,针对TXD1 TXD2设置
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//,针对TXD1 TXD2设置
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//-------串口的初始化过程------------------------
void Uart_System_Initrial()
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
USART_InitStructure.USART_BaudRate = 9600;//波特率=115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位字长=8
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位=1
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无需RTS CTS硬件控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口接收发送同时有效
USART_Init(USART1, &USART_InitStructure); //配置串口1,若配置USART2,需重新写一遍上述
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_RXNE);
USART_ClearFlag(USART1, USART_FLAG_TXE);
USART_InitStructure.USART_BaudRate = 9600;//波特率=115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位字长=8
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位=1
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无需RTS CTS硬件控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口接收发送同时有效
USART_Init(USART2, &USART_InitStructure); //配置串口1,若配置USART2,需重新写一遍上述
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_RXNE);
USART_ClearFlag(USART2, USART_FLAG_TXE);
USART_Cmd(USART2, ENABLE);
USART_Cmd(USART1, ENABLE);
}
//---------中断模块的初始化配置过程--------------
void NVIC_System_Initrial()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //定义
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从占优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能定义的中断是否开启。
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //定义
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从占优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能定义的中断是否开启。
NVIC_Init(&NVIC_InitStructure);
}
//------------------------------------------------结束--------------------------------------------------
//---------------------------中断部分---------------------------------------------------
void USART1_IRQHandler(void)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
USART_ClearFlag(USART1,USART_FLAG_RXNE);
USART_SendData(USART1,USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
}
void USART2_IRQHandler(void)
{
if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
USART_ClearFlag(USART2,USART_FLAG_RXNE);
USART_SendData(USART2,USART_ReceiveData(USART2));
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
分享一下。。。 碰到同样问题
2个分别测试OK否,然后再2个一起测试
一周热门 更多>