stm32之30.DMA

DMA(硬件加速方法)一般用于帮运比较大的数据(如:摄像头数据图像传输),寄存器-》DMA-》RAM 或者 RAM-》DMA-》寄存器提高CPU的工作效率

源码--

cs 复制代码
#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支持搬运的外设有如图

相关推荐
单片机杂货铺11 小时前
基于单片机的智能粮仓控制系统设计与实现
stm32·单片机·嵌入式硬件·物联网·计算机外设·51单片机·课程设计
指尖的爷11 小时前
RKNN转化环境搭建(rknn_toolkit2新版本)
嵌入式硬件·深度学习·物联网·目标检测
梁朝辉11 小时前
滴泪的曲线(Curved)还是直线(Line)什么区别,都应用于什么场景!
嵌入式硬件·硬件工程
clear sky .13 小时前
stm32H7进入HardFault_Handler机制
stm32·单片机·嵌入式硬件
小僧景贤13 小时前
STM32 GPIO 详解(含寄存器、HAL 库、电气特性、低功耗深度剖析)
stm32·单片机·嵌入式硬件
远翔调光芯片^1382879887215 小时前
从参数到应用:FP7208如何以0.2V精密反馈提升LED驱动性能
科技·单片机·嵌入式硬件·智能家居·能源
碧海银沙音频科技研究院16 小时前
4 BES2710编译环境搭建方法
嵌入式硬件·算法
别催小唐敲代码18 小时前
STM32 定时器学习笔记:TIM 架构、输入捕获与 PWM 一篇讲透
笔记·stm32·学习
csdn杰哥18 小时前
瑞萨单片机AI教程【六】导入外部电机状态数据训练模型
人工智能·单片机·嵌入式硬件·瑞萨·ra6m5
深念Y18 小时前
ZTE_UZ901_NVRAM经验总结
linux·服务器·网络·嵌入式硬件·嵌入式·随身wifi