串口DMA系列文章一共两篇,分别是利用DMA + 空闲中断实现不定长数据的收发,另一篇是使用DMA中断实现定长数据的收发,文章链接如下:
01 STM32F103 串口DMA + 空闲中断 实现不定长数据收发
02 STM32F103 串口 +DMA中断实现数据收发
上一篇串口DMA + 空闲中断 实现不定长数据收发 讲了 串口 + DMA空闲中断实现不定长数据收发的功能,除了利用空闲中断实现数据收发,还可以利用DMA的中断实现数据的收发,不同之处是后者不能实现不定长数据的接收,本文讲解DMA中断的方式实现数据的收发。
1. 代码讲解
1.1 uart_dma.c
使用DMA的中断时,无需配置串口的中断
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_dma.h"
#include "misc.h"
#include "systick.h"
#include "uart_dma.h"
uint8_t uart1RecvData[32] = {
0}; // 接收数据缓冲区
uint8_t uart1RecvFlag = 0; // 接收完成标志位
uint8_t uart1RecvLen = 0; // 接收的数据长度
uint8_t uart1SendData[32] = {
0}; // 发送数据缓冲区
uint8_t uart1SendFlag = 0; // 发送完成标志位
/* 串口1 GPIO引脚初始化 */
void Uart1GpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能GPIOA时钟
/************ ↓ RS485 相关 ↓ ************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // 使能GPIOD时钟
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // 输入输出使能引脚 推挽输出
GPIO_InitStruct.GPIO_Pin = UART1_EN_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(UART1_EN_PORT, &GPIO_InitStruct); // PD1
Uart1RxEnable(); // 初始化接收模式
/************ ↑ RS485 相关 ↑ ************/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; // TX 推挽输出
GPIO_InitStruct.GPIO_Pin = UART1_TX_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(UART1_TX_PORT, &GPIO_InitStruct); // PA9
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; // RX上拉输入
GPIO_InitStruct.GPIO_Pin = UART1_RX_PIN;
GPIO_Init(UART1_RX_PORT, &GPIO_InitStruct); // PA10
}
/************ ↓ RS485 相关 ↓ ************/
/* 使能485发送 */
void Uart1TxEnable(void)
{
GPIO_WriteBit(UART1_EN_P

本文介绍如何使用STM32F103的DMA中断实现定长数据的串口收发功能,包括配置GPIO、USART及DMA等步骤,并通过示例代码展示如何实现数据的发送与接收。

68

被折叠的 条评论
为什么被折叠?



