下面是我用的STM32单片机串口5的初始化 然后再主函数里面一直发送数据 串口助手的相关配置和串口的配置一样 但是接收的数据和自己发送的数据不一致下面是串口5的初始化(单片机用的是CP2102芯片转USB和电脑连接的),求大神解答
void Uart5_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 , ENABLE);
// Configure USART5 Tx (PC.12) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// Configure USART5 Rx (PD.2) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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(UART5, &USART_InitStructure);
// 使能 USART5
USART_Cmd(UART5, ENABLE);
USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);//开中断
USART5_NVIC_Configuration();
USARTClearFlag(UART5,USART_FLAG_TC);
USARTClearFlag(UART5,USART_FLAG_TXE);
}
void USART5_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/*Enable the USARTy Interrupt*/
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//接收串口来的数据
void UART5_IRQHandler(void)
{
}
void USARTSendData(USART_TypeDef* USARTx, uint8_t Data)
{
/* Transmit Data */
USARTx->DR = (Data & (uint16_t)0x01FF);
delay_ms(1);
}
下面是主函数
void main()
{
Uart5_Init();
while(1)
{
USARTSendData(UART5,0x12);
}
}