void Uart4_Init(uint32_t baud)
{
GPIO_InitPara GPIO_InitStructure;
USART_InitPara USART_InitStructure;
NVIC_InitPara NVIC_InitStructure;
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_AF, ENABLE);
RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_UART4, ENABLE);
USART_DeInit(UART4);
/**/
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_PIN_12);
//进入设置状态
//USART4_TX PC.10
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//USART4_RX PC.11
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_11;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//Usart4 NVIC
NVIC_InitStructure.NVIC_IRQ = UART4_IRQn; //UART4_IRQHandler
NVIC_InitStructure.NVIC_IRQPreemptPriority=3; //least 3
NVIC_InitStructure.NVIC_IRQSubPriority = 3; //least 3
NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BRR = baud;
USART_InitStructure.USART_WL = USART_WL_8B;
USART_InitStructure.USART_STBits = USART_STBITS_1;
USART_InitStructure.USART_Parity = USART_PARITY_RESET;
USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
USART_Init(UART4, &USART_InitStructure);
//USART_DMA_Enable(UART4,USART_DMAREQ_TX | USART_DMAREQ_RX, ENABLE);
USART_INT_Set(UART4, USART_INT_RBNE, ENABLE);//开启串口接收中断
USART_Enable(UART4, ENABLE); //使能串口
//while (USART_GetBitState(UART4, USART_FLAG_TC) == RESET);
USART_ClearBitState (UART4,USART_FLAG_TC);
}
求一个gd32f103vct6 串口4初始化代码,上面的是我写的,但是发送出来数据不对,是哪里时钟不对还是初始化不对?请指点 |