1、串口的操作步骤
1)打开GPIO的时钟使能和USART的时钟使能。
2)设置串口 IO 的 IO 模式(一般输入是浮空输入,输出是复用推挽输出)
3)初始化USART。(包括设置波特率、数据长度、停止位、校验位等)
4)如果使用中断接收的话,那么还要设置 NVIC 并打开中断使能。(即设置 它的中断优先级)
2、库函数说明
1)RCC_)APB2PeriphClockCmd()函数
开启时钟函数,我们要打开时钟有两个 GPIO 口时钟和 USART 时钟。RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
两个函数分别打开了 GPIOA 和 USART1 的时钟(USART 使用的是 PA9 、PA10)。
2) GPIO_Init()函数
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //Tx
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mod