DMA(硬件加速方法)一般用于帮运比较大的数据(如:摄像头数据图像传输),寄存器-》DMA-》RAM 或者 RAM-》DMA-》寄存器提高CPU的工作效率
源码--
#include "myhead.h"
#include "adc.h"
#include "delay.h"
#include "usart.h"
extern u16 ADC1_Value;
u16 ADC1_Value;
void DMA_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
//1.时钟使能ADC1 GPIOA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//1分频
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//2分频
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//1分频
//DMA2初始化
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //通道0
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR); //搬运的起始地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1_Value; //搬运的目的地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //DMA搬运的方向
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //设置搬运起始点到目的地发数据宽度
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //字节大小 半个字节
DMA_InitStructure.DMA_BufferSize = 1; //设置缓存块数
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置地址自动递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA搬运模式 Nomal Circular循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //触发优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不需要上下拉电阻
GPIO_Init(GPIOA, &GPIO_InitStructure);
//3.ADC的公共部分配置
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式,双重模式,三重模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //84MHZ---/4 给到具体ADC 21MHZ<=36MHZ
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //多重ADC模式有关,一般独立ADC都不需要开启
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//多重ADC模式的采样间隔
ADC_CommonInit(&ADC_CommonInitStructure);
//4.指定ADC配置
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //精度选择
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描模式选择 开扫描/关扫描 单个ADC开了多个通道才要扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换模式 开/关
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1; //待转换的通道数量 如果adc通道开多个(adc1,adc3),通道数量跟数量一样多,上面扫描模式就得开
ADC_Init(ADC1, &ADC_InitStructure);
/* Enable DMA request after last transfer (Single-ADC mode) */
//ADC1->CR2 第9bit DDS位 中文手册276页
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC3 DMA */
//ADC1->CR2 第8bit DMA位 中文手册276页
ADC_DMACmd(ADC1, ENABLE);
//5.通道规则设置--设置adc通道的转换顺序
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_3Cycles);//ADC_SampleTime_3Cycles通道转换速度,等级越高转换速度越快
//6.使能ADC模块
ADC_Cmd(ADC1,ENABLE);
}
int main(void)
{
adc_init();
usart1_init(9600);
DMA_init();
ADC_SoftwareStartConv(ADC1); //ADC通道配置必须放DMA_init/adc_init()后面
while(1)
{
//3.3v/4096 =xV/ADC1->DR
printf("%.2fv\r\n",ADC1_Value*3.3/4095); //不断读取ADC1的转换结果 转换成电压值 (.2f表示double类型小数点后两位打印结果)
delay_ms(1000);
}
}
DMA2支持搬运的外设有如图