本帖最后由 youki1234 于 2013-8-16 16:57 编辑
用下面的代码初始化后,串口接收、发送单个字符都没有问题。但如果上位机连续发两个字符给串口3时,第二个字符就会有问题。例如:
上位机 发 0xAA 下位机回的就是0xAA
但如果上位机发0xAA 0xAA,下位机回的就是0xAA 0xF5 (应该回0xAA 0xAA)
大家有没有碰到过类似的问题啊?或者哪位大侠能发个373能用的UART3的代码,谢谢了。。。
UART3用的是PB8,PB9, 同样的代码在UART1和UART2中使用都没有问题。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
初始化代码如下:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
/* Connect pin to Periph */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_7);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_7);
/* Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
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(USART3, &USART_InitStructure);
/* USART enable */
USART_Cmd(USART3, ENABLE);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
中断处理函数代码如下:
if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == SET)
{
temp3 = USART_ReceiveData(USART3);
}
if(USART_GetFlagStatus(USART3,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART3,USART_FLAG_ORE); //????
USART_ReceiveData(USART3); //?DR
}
RecvBUF3[COUNT3++]=temp3;
if(COUNT3==20)
COUNT3=0;
Uart_Put(3,temp3);//收到一个字符就返回一个字符。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|