单片机
返回首页

STM32串口操作相关事项

2024-10-21 来源:cnblogs

放了一段时间,对stm32似乎有点陌生,总结一下!

(基于3.0固件库,芯片stm32f103rbt6)

1、配置串口的管脚和时钟

由于串口1、2是在GPIOA上:

所以要是能串口GPIOA、AFIO和1或者2的串口时钟,代码如下:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | 

            RCC_APB2Periph_AFIO |

            RCC_APB2Periph_USART1 , 

            ENABLE);

2、对串口的具体物理管脚进行相应的配置:

 /* A9 USART1_Tx */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* A10 USART1_Rx  */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX

GPIO_Init(GPIOA, &GPIO_InitStructure);

3、在设置波特率、数据位、停止位。。。。。。。

完整测试程序如下:

串口的配置:

void USART_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
 USART_ClockInitTypeDef USART_ClockInitStructure;
 
 //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
            RCC_APB2Periph_AFIO |
            RCC_APB2Periph_USART1 ,
            ENABLE);

    /* A9 USART1_Tx */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* A10 USART1_Rx  */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
    GPIO_Init(GPIOA, &GPIO_InitStructure);


 USART_InitStructure.USART_BaudRate = 9600;
 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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
 USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
 USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
 USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

 USART_ClockInit(USART1, &USART_ClockInitStructure);
    USART_Init(USART1, &USART_InitStructure);
    /* Enable the USARTx */
    USART_Cmd(USART1, ENABLE);
 
 
 //
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
 
 // A2 ×?T2X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // A3 ×?R2X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 USART_InitStructure.USART_BaudRate = 9600;
 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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
 USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
 USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
 USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

    USART_ClockInit(USART2, &USART_ClockInitStructure);
    USART_Init(USART2, &USART_InitStructure);
   
    USART_Cmd(USART2, ENABLE);
 //
 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
串口发送函数:

void USART1_Putc(unsigned char c)
{
    USART_SendData(USART1, c);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}

void USART1_Puts(char * str)
{
    while(*str)
    {
        USART_SendData(USART1, *str++);
        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    }
}

串口1接收中断函数:

void USART1_IRQHandler(void)
{
 //接收中断
 if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
 {
  USART_ClearITPendingBit(USART1,USART_IT_RXNE);
  Uart1_Get_Data=USART_ReceiveData(USART1);

 }
 
 //溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题
 if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
 {
  USART_ClearFlag(USART1,USART_FLAG_ORE); //读SR
  USART_ReceiveData(USART1);    //读DR
 }
}


进入单片机查看更多内容>>
相关视频
  • 【TI MSPM0 应用实战】智能小车+工业角度编码器+血氧仪+烟雾探测器!硬核参考设计详解!

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

  • 直播回放: Microchip Timberwolf™ 音频处理器在线研讨会

  • 基于灵动MM32W0系列MCU的指夹血氧仪控制及OTA升级应用方案分享

精选电路图
  • 1瓦线性调频增强器

  • 家用电器遥控器

  • 12V 转 28V DC-DC 变换器(基于 LM2585)

  • 红外开关

  • DS1669数字电位器

  • HA1377 桥式放大器 BCL 电容 17W(汽车音频)

    相关电子头条文章