打印
[其他ST产品]

STM32串口通信配置(USART/UART)

[复制链接]
681|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
610u|  楼主 | 2023-2-25 16:45 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
在配置USART和UART之前,得先明白什么是USART和UART以及它们之间有什么区别,其实两者是同步和异步的区别。
UART:universal asynchronous receiver and transmitter通用异步收发器;
USART:universal synchronous asynchronous receiver and transmitter通用同步异步收发器。
一般而言,单片机中,名称为UART的接口一般只能用于异步串行通讯,而名称为USART的接口既可以用于同步串行通讯,也能用于异步串行通讯。

使用特权

评论回复
沙发
610u|  楼主 | 2023-2-25 16:49 | 只看该作者
串口接受和发送数据的程序(USART/UART)
STM32串口引脚如下图所示

使用特权

评论回复
板凳
610u|  楼主 | 2023-2-25 16:50 | 只看该作者
配置USART1
static unsigned char TxBuffer[256];
static unsigned char TxCounter=0;
static unsigned char count=0;
extern void CopeSerial1Data(unsigned char ucData);
void Initial_UART1(unsigned long baudrate)
{
         //GPIO端口设置
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
        //USART1_TX   GPIOA.9
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化   
   //USART1_RX   GPIOA.10
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
        GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
          
        USART_InitStructure.USART_BaudRate = baudrate;//设置波特率
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
        USART_InitStructure.USART_StopBits = USART_StopBits_1;//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); //初始化
        USART_ITConfig(USART1, USART_IT_TXE, DISABLE);  
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启串口接受中断      
        USART_ClearFlag(USART1,USART_FLAG_TC);
        USART_Cmd(USART1, ENABLE);//使能
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
地板
610u|  楼主 | 2023-2-26 00:18 | 只看该作者
配置USART2
由于和前一个配置大同小异,所以只在和前一个不同的地方加入注释
static unsigned char TxBuffer[256];
static unsigned char TxCounter=0;
static unsigned char count=0;
extern void CopeSerial2Data(unsigned char ucData);
void Initial_UART2(unsigned long baudrate)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE);
        //PA2为TX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);  
        //PA3为RX  
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
         
        USART_InitStructure.USART_BaudRate = baudrate;
        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(USART2, &USART_InitStructure);
        USART_ITConfig(USART2, USART_IT_TXE, DISABLE);   
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
        USART_ClearFlag(USART2,USART_FLAG_TC);       
        USART_Cmd(USART2, ENABLE);
        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
5
610u|  楼主 | 2023-2-26 00:18 | 只看该作者
配置USART3
static unsigned char TxBuffer[256];
static unsigned char TxCounter=0;
static unsigned char count=0;
extern void CopeSerial2Data(unsigned char ucData);
void Initial_UART3(unsigned long baudrate)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOA, ENABLE);
         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);   // GPIOB时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
          USART_DeInit(USART3);
         //PB10为TX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);   
        //PB1为RX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
         
        USART_InitStructure.USART_BaudRate = baudrate;
        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(USART3, &USART_InitStructure);
        USART_ITConfig(USART3, USART_IT_TXE, DISABLE);   
        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
        USART_ClearFlag(USART3,USART_FLAG_TC);       
        USART_Cmd(USART3, ENABLE);
        NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
6
610u|  楼主 | 2023-2-26 00:18 | 只看该作者
配置大概就是如此,在笔者的程序中,USART1用来STM32连接电脑上串口助手,USART2、3用来连接两个传感器收发数据

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

49

主题

517

帖子

0

粉丝