#define BaudRate 115200
void Uart2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//----开启Usart1和GPIOA的Rcc时钟,
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //开启USART1的Clk,
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE); //通过对应位设置使Usart1寄存器复位,
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE); //退出复位,
//初始化串口1,引脚Pa9,
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化串口1,引脚Pa10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化串口1,
USART_InitStructure.USART_BaudRate = BaudRate; //波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8bit,
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1,
USART_InitStructure.USART_Parity = USART_Parity_No; //奇偶校验无,CR1的PCE和PS位,
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //流控制无,
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发都使能,
USART_Init(USART2, &USART_InitStructure);
//使能串口1,
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
} |