本帖最后由 z312958887 于 2018-3-14 17:14 编辑
有遇到过类似情况的么?硬件就那两根线,感觉出问题的可能性不大。以下是USART3的初始化函数和中断服务函数:
/*****************************************************************
function name:usart3_init
Description :usart3初始化
parameter :None
return :None
*****************************************************************/
void usart3_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //声明一个名为GPIO_InitStructure的结构体,结构体原型为GPIO_InitTypeDef
USART_InitTypeDef USART_InitStructure; //声明一个名为USART_InitStructure的结构体,结构体原型为USART_InitTypeDef
NVIC_InitTypeDef NVIC_InitStructure; //声明一个名为NVIC_InitStructure结构体,结构体原型为NVIC_InitTypeDef
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能APB2中GPIOB的时钟(USART3的TX、RX分别对应PB10、PB11)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能APB1中USART3的时钟
//PB10:TX
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);
//PB11:RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
//NVIC中断向量配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART3初始化
USART_InitStructure.USART_BaudRate = 115200; //波特率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;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //同时使能发送模式和接收模式
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //RXNE中断开启
USART_ITConfig(USART3, USART_IT_TXE, ENABLE); //TXE中断开启
USART_ITConfig(USART3, USART_IT_TC, ENABLE); //TC中断开启
USART_Cmd(USART3, ENABLE); //使能USART3
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
usart3_receive_char = USART_ReceiveData(USART3); //读取USART_DR,清除RXNE
usart3_receive_flag = 0x01;
}
if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
{
USART3->DR = 0x0; //写USART_DR,清除TXE
tc_flag = 1;
}
if(USART_GetITStatus(USART3, USART_IT_TC) != RESET)
{
usart3_sta = USART3->SR;
USART3->DR = 0x0; //先读USART_SR,再写USART_DR,清除TC
tc_flag = 1;
}
}
|