新作了一个板子,用PA端口的A2和A3作为UART2的发和收,初始化波特率为115200。初始化程序:
USART_ClockInitTypeDef USART_ClockInitStruct;
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ;
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(GPIOA,&GPIO_InitStructure);
USART_ClockStructInit(&USART_ClockInitStruct);
USART_ClockInit(USART2,&USART_ClockInitStruct);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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); //
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
我的发送函数为:
uint16 i_u16Count= 0x3000;
do
{
i_u16Count--;
}while ((!(USART2->SR & USART_FLAG_TXE)) && (i_u16Count > 0)) ;
if(i_u16Count == 0)
{
// m_Error_Flag.bit.UartCommErrorFlag = ENABLE;
USART2_Configuration();
}
else
{
i_u16Count = 0x3000;
USART2->DR = (i_u16Data & (uint16)0x01FF);
do
{
i_u16Count--;
}while ((!(USART2->SR & USART_FLAG_TC)) && (i_u16Count > 0)) ;
if(i_u16Count == 0)
{
USART2_Configuration();
}
}
现在的问题是我用上位机串口工具以115200的波特率发送一个0x60的数据,F4接收到的是0x00,然后我用115200的波特率发送“0123456789abcdef”到上位机,发现115200下是乱码,我调到38400是正确的,我是用库函数初始化的,这个跟APB1的时钟是系统时钟的1/4这个应该没关系啊,怎么刚好差4倍的关系,但是我用串口工具以38400的波特率发送数据给F4,F4接收到的也不是0x60,所以我觉得很奇怪,之前用F103都是很正常使用的,F4的原子的是UART1,UART2的没用过,哪位仁兄测试过UART2,不吝赐教下啊。急!!!!谢谢。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>