使用原厂的STM32F407VG Discovery开发板,进行串口通信的调试。发现接收数据有错误
比如接收到0x55的时候没有问题,接收到0x74的时候却变成了0x11,接收0x33,变成了0x66。。。
原厂开发板,晶振8M,系统时钟配置也是8M,没有更改例程的内容。配置代码如下,请各位大虾帮忙,不胜感激。
int main(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the system clocks */
RCC_ClocksTypeDef RCC_Clocks;
/* SysTick end of count event each 10ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
/********************* NVIC Configuration **********************/
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); NVIC_Config();
/*GPIO Config*/
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOA, ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/************************GPIO Config************************/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/************************GPIO Config************************/
USART_Configuration();
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
while (1)
{
Delay(0xfFFFFF);
Delay(0xfFFFFF);
USART_SendData(USART1, 3);
}
}
/*ISR 函数*/
void USARTx_IRQHANDLER(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxData = USART_ReceiveData(USART1);
RxData = 0x000000FF&RxData;
USART_SendData(USART1, RxData+1);
}
} |