当前位置:首页 > 单片机 > 单片机
[导读]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

这个链接的问题.


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭