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. 【剑指Offer】【链表】反转链表

    题目:输入一个链表,反转链表后,输出新链表的表头. A:定义3个结点,pNode作移动指针,pRet作输出指针,pPrev作前驱指针    在pNode没有到达链尾之前,循环里创建pNext指针记录p ...

  2. Oracle数据泵恢复用户数据实例

    我们测试环境经常会遇到恢复生产数据的情况,我一般比较习惯使用数据泵来搞,这个具体根据自己的业务形态选择适合自己的方式. 此次我们说的是完全恢复用户数据,具体步骤如下: 1.备份数据 expdp tes ...

  3. js循环中删除数组中的某个元素

    (1)使用js中的splice方法循环删除数组中某个值 eg: var arr=new Array(); arr.push(1); arr.push(2); arr.push(3); arr.push ...

  4. 访问不了github解决方案

    1.首先通过网址https://github.com.ipaddress.com/www.github.com查看当前github.com对应的IP地址,查到的信息如下图所示 修改hosts文件,wi ...

  5. Linux 系统挂载 ntfs 移动硬盘无法写入的问题

    linux 下挂载 ntfs 移动硬盘无法写入问题 在机房使用移动硬盘时发现无法写入硬盘,具体是 Ubuntu 21.04 ,移动硬盘是 SSD ,分区类型是 NTFS . 首先百度得知最优办法是安装 ...

  6. Finance_CAPM&APT

    ACCA CAPM: https://www.bilibili.com/video/BV1KE411j7Ri?from=search&seid=14840285521502157259 投资学 ...

  7. IQueryable 和 IEnumerable 的区别

    讲一讲 IQueryable 和 IEnumerable 的区别. 我们会在使用 LINQ 查询方法之后,又使用 ToList 等方法,将查询结果转换成集合. 如果我们不使用 ToList 呢? 比如 ...

  8. js本地时钟

    js本地时钟,如上图所示,秒是跳动的 1 // 本地时钟 2 function clockon() { 3 var now = new Date(); 4 var year = now.getFull ...

  9. mysql重新设置列的自增初始值

    alter table xxx auto_increment = 100; 因为设置了列的自增之后,若删除过一些行,下次再新增时还会从已删除的id算起自增,为了让数据看起来连续,可以重新设置自增起始值 ...

  10. 【peewee】Python使用peewee时where中不同类型比较的问题

    问题 以学生表为例,TableStudents表中age字段是TextField类型,想要筛选出18岁以上的学生 TableStudents.select().where(TableStudents. ...