STM32F30X USART串口初始化順序
void InitUart(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#if 1// (USART_USED == USART1)
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10,GPIO_AF_7);
//TX PIN
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//RX PIN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOA,&GPIO_InitStructure);
#else
#error "not defined"
#endif
//UART配置
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_DeInit(USART_USED);
USART_Init(USART_USED,&USART_InitStruct);
}
在做串口初始化時(shí)候,務(wù)必先將串口的RCC時(shí)鐘開(kāi)啟,后進(jìn)行相對(duì)于的GPIO與USART配置,否則串口會(huì)無(wú)法工作.
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
重新修改,關(guān)于串口IO配置需要對(duì)GPIO_OType進(jìn)行賦值,否則會(huì)引起
http://blog.csdn.net/lan120576664/article/details/40657979
這個(gè)鏈接的問(wèn)題.