一、
结构体配置
void DMA_mtp_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_addr; //USART地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SendBuff; //储存器地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //传输方向
//DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 5; //传输数目
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //储存器地址增量模式
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //储存器数据宽度(串口只能为8位)同外设一致
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据宽度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; //传输模式 只传输一次
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //通道优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //MtoM模式
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel4, ENABLE);
二、程序源码
dma_mtp.c
#include "dma_mtp.h"
uint8_t SendBuff[5]={1,2,3,4,5};
void USART_config(void) //串口初始化配置函数
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
USART_InitTypeDef USART_InitStructure; //定义GPIO初始化结构体
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //打开GPIO时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选择串口接收引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //选择串口发送引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出速率
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //开启串口1时钟
USART_InitStructure.USART_BaudRate = 115200; //设置串口波比特率
USART_InitStructure.USART_StopBits =USART_StopBits_1 ;//停止位
USART_InitStructure.USART_Parity =USART_Parity_No; //校验位
USART_InitStructure.USART_WordLength =USART_WordLength_8b ;//数据位
USART_InitStructure.USART_Mode =USART_Mode_Rx|USART_Mode_Tx;//选择串口模式
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;//配置硬件控制流
USART_Init(USART1, &USART_InitStructure);
//NVIC_Configuration(); //调用中断控制函数
//USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //串口接收使能
USART_Cmd(USART1, ENABLE); //串口使能
}
void DMA_mtp_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_addr; //USART地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SendBuff; //储存器地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //传输方向
//DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 5; //传输数目
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //储存器地址增量模式
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //储存器数据宽度(串口只能为8位)同外设一致
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据宽度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; //传输模式 只传输一次
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //通道优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //MtoM模式
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel4, ENABLE);
}
dma_mtp.h
#ifndef __DMA_MTP_H
#define __DMA_MTP_H
#include "stm32f10x.h"
#include <stdio.h>
#define USART_DR_addr (USART1_BASE+0X04) //外设寄存器地址
void DMA_mtp_Config(void);
void USART_config(void);
#endif //__DMA_MTM_H
main.c
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "bsp_led.h"
#include "dma_mtp.h"
extern uint8_t SendBuff[5]; //声明外部文件变量
int main(void)
{
USART_config(); //串口初始化
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //DMA使能
DMA_mtp_Config(); //DMA初始化
while(1);
}