研究生
最后登录1970-1-1
在线时间 小时
注册时间2017-10-7
|

楼主 |
发表于 2018-3-30 16:25:39
|
显示全部楼层
串口初始化函数如下,麻烦您帮忙看一下:(如果这个配置错了,应该插上USB也没用吧)
void USART_Config(void)//串口初始化函数
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_7);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_7);
/*
* USART1_TX -> PA6 , USART1_RX -> PA7
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//设置串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//设置数据位
USART_InitStructure.USART_StopBits = USART_StopBits_1;//设置停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//设置效验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置工作模式
USART_Init(USART1, &USART_InitStructure); //配置入结构体
// 串口中断优先级配置
NVIC_Configuration();
/* USART1中断允许************************************************/
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_ERR,ENABLE);
USART_Cmd(USART1, ENABLE);//使能串口1
} |
|