STM32串口配置过程.doc
文本预览下载声明
串口配置过程
1,配置IO
// 设置Tx引脚为推拉输出模式,推拉式输出级既提高电路的负载能力,又提高开关速度
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, GPIO_InitStructure);
// 设置Rx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, GPIO_InitStructure);
2,配置UART
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; //设置波特率 速度越快,传输时间越短,要注意
//所用设备的波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*数据长度=8位*/
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_Tx | USART_Mode_Rx; /*允许接收和发送*/
//若只是接受则只要USART_Mode_Rx就可以
USART_Init(USART3, USART_InitStructure);
/*开始时发送中断不开*/
// USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
USART_ITConfig(USART3,USART_IT_CTS, DISABLE);//这个明白是什么意思?
- USART_IT_CTS: CTS 改变中断 (对于UART4和UART5无法使用)
* - USART_IT_LBD: LIN 间隔侦测中断
* - USART_IT_TXE: 发送寄存器空中断
* - USART_IT_TC: 发送完成中断
* - USART_IT_RXNE: 接收寄存器全满中断
* - USART_IT_IDLE: IDLE线性中断
* - USART_IT_PE: 奇偶错误中断
* - USART_IT_ERR: 错误中断(桢错误, 噪声错误, 超速错误)
可以看一下汉化固件库
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
/*使能串口*/
USART_Cmd(USART3, ENABLE); //使能串口
}
3, 打开串口时钟
void RCC_Configuration(void)
// 启动USART1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Pe
显示全部