• STM32 串口


    串口初始化

    1. 2时钟使能
      • RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
    2. GPIO模式设置(GPIO_Init)
      • TX:GPIO_Mode_AF_PP 推挽输出
      • RX:GPIO_Mode_IN_FLOATING 浮空输入
    3. 串口协议设置
      1. USART_Init(GPIOx,USART_InitTypeDef)(波特率,8字长,1位停止位,无校验位,可收可发USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;)
      2. 中断
    4. 开启串口
      • USART_Cmd(USARTx, ENABLE);   

    发送数据:

      USART_SendData(UARTx,0x34);//实质是对UARTx->DR写0x34

      while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //等待发送完成,实质是读取UARTx->SR的TC位(发送成功标志位)。另外,关于UARTx->SR的TE寄存器(DR寄存器刚刚写入数据的时候,TE清0;当DR中数据被传送到移位寄存器后(此时DR可准备下一次的发送数据),TE置1)

    使用printf

    接收数据:

    • 中断方式:
    • if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
       {
        USART_ReceiveData(USART1);
        USART_ClearFlag(USART1,USART_FLAG_RXNE);
       }

        

    • 查询方式:(上位机发送字符串时会来不及接收)
      if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
       {
        USART_ReceiveData(USART1);
        USART_ClearITPendingBit(USART1,USART_IT_RXNE);
       }

    清除中断

    • 清除TXE:对USART_DR的写操作,一定将该位清零 ,不能用USART_ClearITPendingBit
    • 清除TC:先读USART_SR,然后写入USART_DR,将该位清零
    • 清除RXNE,对USART_DR的读操作,将该位清零
    • PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRunerror) and IDLE (Idle line detected) flags are cleared by software sequence: a read operation to USART_SR register (USART_GetFlagStatus())  followed by a read operation to USART_DR register (USART_ReceiveData()).//读状态写数据
    • RXNE flag can be also cleared by a read to the USART_DR register * (USART_ReceiveData()).//读数据
    • TC flag can be also cleared by software sequence: a read operation to USART_SR register (USART_GetFlagStatus()) followed by a write operationto USART_DR register (USART_SendData()).//读状态写数据
    • TXE flag is cleared only by a write to the USART_DR register (USART_SendData()).//写数据

    只有在多缓存通讯中才推荐直接对中断标志位清零


     ITStatusFlagStatus 

    USART_GetFlagStatus(USART1,USART_FLAG_RXNE);//若SR寄存器的RXNE位1则返回SET

    USART_GetITStatus(USART1,USART_IT_RXNE);=USART_GetFlagStatus &&  该位允许中断



  • 相关阅读:
    Struts数据效验
    Struts2中国际化
    ValueStack对象
    Ognl表达式语言
    Struts2拦截器
    ubuntu下tomcat运行不起来解决
    Windows 下的SSH客户端
    远程管理控制ssh
    linux用户和组账户管理
    搭建Java服务器,并且实现远程安全访问linux系统
  • 原文地址:https://www.cnblogs.com/2cats/p/3577589.html
Copyright © 2020-2023  润新知