访问手机版页面
你的位置:老古开发网 > STM32单片机编程 > 正文  
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,ENA

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

这个链接的问题.


标签: stm32f30x,usart,串口,初始化顺序,
来源:互联网 作者:karen 时间:2019/1/11 11:20:01
相关阅读
推荐阅读
阅读排行
最近更新
商品推荐