当前位置:网站首页>A---DMA串口通信
A---DMA串口通信
2022-07-15 11:35:00 【haichuangdianzi】
1、芯片:STM32L031F6
2.、实现DMA串口数据发送和接收
cubeMX配置:
1、芯片选择
2、根据图中三个红框进行配置,本次实验中使用的USART2。根据硬件设计,电脑与单片机是通过USART2通信,因为UART1以后也会用到,目前先做简单设置。

3、UART1设置。

4、UART2设置。
由于使用DMA,所以也要进行DMA相关配置。
5、NVIC配置
5、时钟配置

MDK设置
1、在usart.c文件中添加如下代码
/* USER CODE BEGIN 0 */
uint8_t Rx_Flag = 0;
uint16_t Rx_Len = 0;
uint8_t Rx_Buf[1024] = {
0};
/* USER CODE END 0 */
2、在usart.h文件中添加如下代码
#define Rx_Max 1024
extern uint8_t Rx_Flag;
extern uint16_t Rx_Len;
extern uint8_t Rx_Buf[1024];
3、在stm32f1xx_it.c文件中添加如下代码


#include "usart.h"
if((__HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE) != RESET))
{
/*清除状态寄存器和串口数据寄存器*/
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
/*失能DMA接收*/
HAL_UART_DMAStop(&huart2);
/*读取接收长度,总大小-剩余大小*/
temp = huart2.hdmarx->Instance->CNDTR;
Rx_Len = Rx_Max - temp;
/*接收标志位置1*/
Rx_Flag=1;
/*使能接收DMA接收*/
HAL_UART_Receive_DMA(&huart2,Rx_Buf,Rx_Max);
}
4、在main.c文件中添加如下代码
HAL_UART_Receive_DMA(&huart2, Rx_Buf, Rx_Max);
__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
if(Rx_Flag) // Receive flag
{
Rx_Flag=0; // clean flag
HAL_UART_Transmit_DMA(&huart2, Rx_Buf, Rx_Len);
}
边栏推荐
- Kotlin StateFlow 搜索功能的实践 DB + NetWork
- 558. 四叉樹交集 : 簡單遞歸運用題
- 558. 四叉树交集 : 简单递归运用题
- Want to go whoring for nothing, right? Enough for you this time!
- CDeC-Net代码实现
- 牛文文:专精特新时代,需要新样板、新风貌
- Summer learning plan [activities]
- 2022-04-20 Unity入门5——Camera
- 2022-04-19 Unity入门4——重要组件与API
- Open source real-time data warehouse Apache Doris graduated, how to go further in the future?
猜你喜欢

2022-04-18 Unity入门1——窗口布局

Career masters help you with interview and job search + career development

2022全球开发者薪资PK:中国排在第19名,使用Go语言最赚钱

Learn FPGA from the bottom structure --- MMCM and PLL

配置Sublime的C语言环境

any类型转换之谜

How to choose the appropriate automated testing tools?

Open source real-time data warehouse Apache Doris graduated, how to go further in the future?

Experiment 1 Security mechanism of SQL Server

diffusion model
随机推荐
2022-04-18 Unity入门3——脚本基础
Want to go whoring for nothing, right? Enough for you this time!
原生JS路由实现页面跳转
配置Sublime的C语言环境
Easy gene encode histone chip SEQ and transcription factor chip SEQ data standard and processing flow
在 PostgreSQL 15 上使用 postgres_fdw 进行事务的并行提交
手机怎样控制led显示屏发视频?
Is there a future for the Internet?
Mysql批量添加测试数据
558. Intersection of quadtrees: simple recursive application problem
Landing DDD (7) - some misunderstandings in tactical design
2022-04-18 C Part 4 - Advanced
可落地的DDD(7)-战术设计上的一些误区
推特大战马斯克:想走,哪那么容易
Oracle 添加索引造成其他用户对此表的查询权限丢失
MySQL神器之一锁
Entity drop-down box design applicable to MES, WMS, ERP and other management systems
从底层结构开始学习FPGA----MMCM与PLL
[NLP] deepke, an open source knowledge extraction tool that supports low resources, long chapters and multimodality
苹果和前设计总监 Jony lve 分道扬镳