STM32F30X USART串口初始化顺序

电子元件 4年前 (2022) aysz01
0

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);

}

在做串口初始化时候,务必先将串口的RCC时钟开启,后进行相对于的GPIO与USART配置,否则串口会无法工作.

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

重新修改,关于串口IO配置需要对GPIO_OType进行赋值,否则会引起

http://blog.csdn.net/lan120576664/article/details/40657979

这个链接的问题.

版权声明:aysz01 发表于 2022-08-06 10:49:51。
转载请注明:STM32F30X USART串口初始化顺序 | 鳌游电工

暂无评论

暂无评论...