stm32 USART

1 USART基础知识

三种模式:轮询模式、中断模式、DMA模式

轮询模式属于阻塞模式

中断模式和DMA模式属于非阻塞模式

发送数据

接收数据

1.1 Polling mode IO operation

  • Send an amount of data in blocking mode using HAL_UART_Transmit()
  • Receive an amount of data in blocking mode using HAL_UART_Receive()

1.2 Interrupt mode IO operation

  • Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
  • At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can add his own code by customization of function pointer HAL_UART_TxCpltCallback
  • Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
  • At reception end of transfer HAL_UART_RxCpltCallback is executed and user can add his own code by customization of function pointer HAL_UART_RxCpltCallback
  • In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can add his own code by customization of function pointer HAL_UART_ErrorCallback

1.3 DMA mode IO operation

  • Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
  • At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  • At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can add his own code by customization of function pointer HAL_UART_TxCpltCallback
  • Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
  • At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  • At reception end of transfer HAL_UART_RxCpltCallback is executed and user can add his own code by customization of function pointer HAL_UART_RxCpltCallback
  • In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can add his own code by customization of function pointer HAL_UART_ErrorCallback
  • Pause the DMA Transfer using HAL_UART_DMAPause()
  • Resume the DMA Transfer using HAL_UART_DMAResume()
  • Stop the DMA Transfer using HAL_UART_DMAStop()

2 重定向USART的输入与输出

参考1:

//* printf重定向
//!配置好printf重定向的源文件。(有一处需要结合实际替换)
//!开启usart中断时:需要在主循环之前手动开启接收中断:HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 1); #ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
//! 直接printf("HelloWorld");会导致打印不出来,自觉加/r/n
PUTCHAR_PROTOTYPE
{
//具体哪个串口可以更改huart1为其它串口
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1 , 0xffff);
return ch;
}

参考2:https://stackoverflow.com/questions/51515052/redefining-fputc-function-when-using-arm-none-eabi-toolchain


/*# 7- Retarget printf to UART (std library and toolchain dependent) #########*/ #if defined(__GNUC__)
int _write(int fd, char * ptr, int len)
{
HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
return len;
}
#elif defined (__ICCARM__)
#include "LowLevelIOInterface.h"
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
HAL_UART_Transmit(&huart1, (uint8_t *) buffer, size, HAL_MAX_DELAY);
return size;
}
#elif defined (__CC_ARM)
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif // OR: // Add syscalls.c with GCC #ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */ /**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}

3 USART中断

  • 使用USART接收中断(根据接收数据的个数判断接收是否完成)连续接收数据时注意使用HAL_UART_Receive_IT(&huart1,buff,buffsize);重复使能中断。需要在中断回调函数的最后再使用HAL_UART_Receive_IT(&huart1,buff,buffsize);使能一次。
//The specific UART interrupts (Transmission complete interrupt, RXNE interrupt
//and Error Interrupts) will be managed using the macros
//__HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the
//transmit and receive process. //【注】:实际上使能中断的(宏)函数是__HAL_UART_ENABLE_IT只不过该函数在HAL_UART_Receive_IT和HAL_UART_Transmit_IT中被调用了。 #define buffsize 5
uint8_t buff[10]; HAL_UART_Receive_IT(&huart1,buff,buffsize);//可以接收指定长度的字符。任意长度需要使用DMA void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
if(huart->Instance==USART1){ }
HAL_UART_Receive_IT(&huart1,buff,buffsize);
}
  • 进入中断回调函数需要对USART的句柄进行判断。
if(huart->Instance==USART1){

}

4 其他

  • 可以使用string.h对字符串进行处理、比较。

  • 串口通信有多种模式:

    • 普通模式:HAL_UART_Transmit、HAL_UART_Receive
    • 中断模式:HAL_UART_Transmit_IT、HAL_UART_Receive_IT
    • DMA模式:

    注意:在传输数据量较大,且通信波特率较高(大于38400)时,如果采用中断方式,每收发一个字节的数据

    (CPU都会被打断,造成(PU无法处理其他事务。因此在批量数据传输,通信波特率较高时,建议采用DMA方式。

  • 其他重要函数:

    __HAL_UART_ENABLE_IT(HANDLE,INTERRUPT) 使能串口中断

    __HAL_UART_GET_FLAG(HANDLE,INTERRUPT) 查询串口中断标志

    __HAL_UART_CLEAR_IDLEFLAG(HANDLE) 清除对应的串口空闲中断标志

stm32 USART的更多相关文章

  1. [stm32] USART USART1收发功能工程

    >_<!功能:PC端发送一个特定的字符:0x0d 0x0a,单片机则返回一句话,如图: >_<!知识: 1.复用功能I/O和调试配置(AFIO)  为了优化外设数目,可以把一些 ...

  2. 单片机stm32 USART串口实际应用解析

    stm32作为现在嵌入式物联网单片机行业中经常要用多的技术,相信大家都有所接触,今天这篇就给大家详细的分析下有关于stm32的出口,还不是很清楚的朋友要注意看看了哦,在最后还会为大家分享有些关于stm ...

  3. stm32 usart 异步传输示例

    STM32F103xE的USART异步数据传输示例 USART全称Universal Synchronous/Asynchronous Receiver/Transmitter,是一种可以进行同步/异 ...

  4. stm32 USART rs485 rs232

    转载自:http://www.cnblogs.com/chineseboy/archive/2013/03/06/2947173.html 前题: 前段时间,在公司调试了一个项目,很简单,但对于初学的 ...

  5. STM32 USART 波特率计算

    The baud rate for the receiver and transmitter (Rx and Tx) are both set to the same value as program ...

  6. stm32 usart的几种通信模式

    一 USART 通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换. USART支持同步单向通信和半双工单线通信,也支持LIN(局 ...

  7. STM32 ~ USART接收不定长数据

    IDLE中断什么时候发生? IDLE就是串口收到一帧数据后,发生的中断.什么是一帧数据呢?比如说给单片机一次发来1个字节,或者一次发来8个字节,这些一次发来的数据,就称为一帧数据,也可以叫做一包数据. ...

  8. stm32 USART使用标志

    在USART的发送端有2个寄存器,一个是程序可以看到的USART_DR寄存器,另一个是程序看不到的移位寄存器,对应USART数据发送有两个标志,一个是TXE=发送数据寄存器空,另一个是TC=发送结束. ...

  9. stm32 usart 串口

    比特率是每秒钟传输二进制代码的位数,单位是:位/秒(bps).如每秒钟传送240个字符, 而每个字符格式包含10位(1个起始位.1个停止位.8个数据位),这时的比特率为: 10位 × 240个/秒 = ...

  10. [stm32] NRF24L01+USART搞定有线和无线通信

    前言 一般进行远程监控时,2.4G无线通信是充当远程数据传输的一种方法.这时就需要在现场部分具备无线数据发送装置,而在上位机部分由于一般只有串口,所以将采集到的数据送到电脑里又要在上位机端设计一个数据 ...

随机推荐

  1. vite vue插件打包配置

    import { defineConfig, UserConfigExport, ConfigEnv } from "vite"; import externalGlobals f ...

  2. 类Class

    C# 类(Class) 当你定义一个类时,你定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...

  3. iOS开发 网络学习(4)HTTPS

    一.HTTPS简介 HTTPS : Hyper Text Transfer Protocol over Secure Socket Layer,是以安全为目标的HTTP通道,简单讲是HTTP的安全版. ...

  4. QTableWidget设置表头标题不成功

    上网查说是由于在设置标题之前没有设置列数,但是我的是设置了列数的,最后发现是由于我在设置数据的时候把标题删除了,清除QTableWidget的方法有两种,clear()和clearContents() ...

  5. Elasticsearch Windows版安装配置

    Elasticsearch简介 Elasticsearch是一个开源的搜索文献的引擎,大概含义就是你通过Rest请求告诉它关键字,他给你返回对应的内容,就这么简单. Elasticsearch封装了L ...

  6. [419] C1 Harbingers Of War OpCodez

    [419] C1 Harbingers Of War Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 Request ...

  7. 什么是压测,为什么要进行压力测试?Jmeter工具的使用

    1.背景介绍 什么是压测? 压力测试是通过不断向被测系统施加"压力",测试系统在压力情况下的性能表现,考察当前软硬件环境下系统所能承受的最大负荷并帮助找出系统瓶颈所在,也就是我们可 ...

  8. freeswitch开启https,wss

    1.sip.js配置访问wss://域名:7443 2.freeswitch配置certs,使用cat   .pem .key >wss.pem,合成wss证书.需重启freeswitch 3. ...

  9. c++11 智能指针学习汇总

    c++为什么要引入智能指针? C/C++ 语言最为人所诟病的特性之一就是存在内存泄露问题,因此后来的大多数语言都提供了内置内存分配与释放功能,有的甚至干脆对语言的使用者屏蔽了内存指针这一概念.这里不置 ...

  10. 类和动态内存分配的课后习题(C++ prime plus)

    第一题 1. 对于下面的类声明: class Cow { char name[20]; char *hobby; double weight; public: Cow(); Cow(const cha ...