本帖最后由 anazel 于 2016-10-24 14:26 编辑
用串口2接收发送数据,发送数据正常,接收数据的时候只能接收到第一个字节,比如发送:0x5A 0xA5 0x04 0x83 0x04,0x12,只能收到0x5A,哪位老师帮忙看看问题出在哪儿了?多谢!!!
void uart2_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2时钟
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2_RX PA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART2 初始化设置
USART_InitStructure.USART_BaudRate = bound;//与DGUS屏通信,设置为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接收中断
USART_Cmd(USART2, ENABLE); //使能串口
}
void USART2_IRQHandler(void) //串口2中断服务程序
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志.
Receive_UART2_Buf[Receive_UART2_Pointer] =USART_ReceiveData(USART2);//(USART2->DR); //读取接收到的数据
Receive_UART2_Pointer++;
if(Receive_UART2_Pointer>250) //如果缓存满,将缓存指针指向缓存的首地址
{
Receive_UART2_Pointer = 0;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志.
Receive_UART2_Buf[Receive_UART2_Pointer] =USART_ReceiveData(USART2);//(USART2->DR); //读取接收到的数据
Receive_UART2_Pointer++;
if(Receive_UART2_Pointer>250) //如果缓存满,将缓存指针指向缓存的首地址
{
Receive_UART2_Pointer = 0;
}
}
}
这段程序应该能接收一串数据啊,放在Receive_UART2_Buf[]里。
一周热门 更多>