DMA简介
- DMA(Direct Memory Access)直接存储器存取
- DMA可以提供外设和存储器或者存储器和存储器之间的高速数据传输,无须CPU干预,节省了CPU的资源
- 12个独立可配置的通道: DMA1(7个通道), DMA2(5个通道) 每个通道都支持软件触发和特定的硬件触发
- STM32F103C8T6 DMA资源:DMA1(7个通道)
存储器映像
DMA框图
DMA基本结构
自动重装器:转运完成后恢复到初值,决定单次转换还是循环转换
M2M:0位硬件触发(外设到存储器或者存储器到外设),1为软件触发(存储器到存储器)
传输计数器:触发一次,转运一次,计数器减1.
*写传输计数器时,要先关闭DAM使能
DMA请求
数据宽度与对齐
数据转运+DMA
#include "DMA.h"
uint16_t SIZE;
void myDMA_Init(uint32_t AddrA,uint32_t AddrB,uint16_t Size)
{//开启时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);SIZE = Size;DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_BufferSize = Size;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;//触发方式DMA_InitStructure.DMA_MemoryBaseAddr = AddrB;//目标地址DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//字节DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//是否自动重装DMA_InitStructure.DMA_PeripheralBaseAddr = AddrA;//源地址DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//字节DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;//地址是否递增DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//优先级DMA_Init(DMA1_Channel1,&DMA_InitStructure);DMA_Cmd(DMA1_Channel1,DISABLE);}void myDMA_Transmission(void)
{DMA_Cmd(DMA1_Channel1,DISABLE);DMA_SetCurrDataCounter(DMA1_Channel1,SIZE);DMA_Cmd(DMA1_Channel1,ENABLE);while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);DMA_ClearFlag(DMA1_FLAG_TC1);
}
ADC扫描模式+DMA
#include "ADC.h"
uint16_t AD_value[] = {0,0,0,0};void AD_Init(void)
{//开启对应时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//GPIO初始化GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);//配置ADC的通道配置ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_55Cycles5);ADC_InitTypeDef ADC_InitStructure;ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;ADC_InitStructure.ADC_NbrOfChannel = 4;ADC_InitStructure.ADC_ScanConvMode = ENABLE;ADC_Init(ADC1,&ADC_InitStructure);DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_BufferSize = 4;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//触发方式DMA_InitStructure.DMA_MemoryBaseAddr = (uint16_t)AD_value;//目标地址DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//字节DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//是否自动重装DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;//源地址DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//地址是否递增DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//优先级DMA_Init(DMA1_Channel1,&DMA_InitStructure);DMA_Cmd(DMA1_Channel1,ENABLE);ADC_DMACmd(ADC1,ENABLE);ADC_Cmd(ADC1,ENABLE);//ADC校准ADC_ResetCalibration(ADC1);//重置所选ADC校准寄存器。while(ADC_GetResetCalibrationStatus(ADC1) == SET);//获取所选ADC重置校准寄存器的状态,重置完成硬件清零ADC_StartCalibration(ADC1);while(ADC_GetCalibrationStatus(ADC1) == SET);//获取所选ADC校准状态,校准完成后硬件清零
}uint16_t AD_GetValue(void)//单通道
{DMA_Cmd(DMA1_Channel1,DISABLE);DMA_SetCurrDataCounter(DMA1_Channel1,4);DMA_Cmd(DMA1_Channel1,ENABLE);ADC_SoftwareStartConvCmd(ADC1,ENABLE);//ADC软件触发启动转换。while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);DMA_ClearFlag(DMA1_FLAG_TC1);
}uint16_t AD_GetAppointValue(uint8_t ADC_Channel)//指定通道
{ADC_RegularChannelConfig(ADC1,ADC_Channel,1,ADC_SampleTime_55Cycles5);ADC_SoftwareStartConvCmd(ADC1,ENABLE);//ADC软件触发启动转换。while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);return ADC_GetConversionValue(ADC1);
}uint16_t AD_GetVoltage(void)
{return (AD_GetValue()/4095)*3.3*100;
}