原创 STM32学习笔记之DMA模式下的ADC

2012-10-10 15:09 6592 11 11 分类: MCU/ 嵌入式 文集: C

今天学习STM32的内部自带12位ADC,

1、特点:

(1)12位分辨率,最短时钟周期为14个,时钟周期可调,分别可以调整为14、20、26、41、54、68、252,因此当时钟为14MHz时候,最快转换时间为1us;

(2)供电电压为2.4V到3.6V,注意stm32的最低供电电压可以是2V,但是使用ADC时候,必须达到2.4V以上;

(3)输入电压范围:Vref-

(4)最小量化单位为:LSB=Vref+/4096mV;

(5)为逐次比较型AD;

2、处理AD转换的方法有两个:一个是常用的中断,另一个是DMA,相对来说,DMA模式下的效率要高,所以这里研究DMA下的ADC。

3、实验目标:通过ADC1的11通道采集外部电压,然后又DMA传送到缓存,然后通过串口发到到PC上。

4、实验程序:

 

#include"stm32f10x.h"
#include"adc.h"
 
 /******************
  function name:ADC1_Init()
  discribe :ADC1初始化函数,调用配置函数和模式选择函数
  input:null
  output:null
 *******************/
  void ADC1_Init(void)
  {
     ADC1_GPIO_Config();
ADC1_Mode_Config(); 
  }
 
/******************
  function name:ADC1_GPIO_Config()
  discribe :ADC1 config function
     (1)enable ADC,GPIO,DMA's clock,
(2)ADC1's IO is PC1,Mode is analog input(AIN)
  input:null
  output:null
  return: null
 *******************/
 void ADC1_GPIO_Config(void)
 {
    
 }
 
/******************
  function name:ADC1_Mode_Config()
  discribe :ADC1  mode configuration
     (1)config DMA
(2)config ADC1
  input:null
  output:null
  return:null
 *******************/
 void ADC1_Mode_Config(void)
 {
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
 
/*******config DMA channel 1 ********/
DMA_DeInit(DMA_Channel1);   //reset DMA channel register
 //define the peripheral base address
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address; 
//define the memory base address
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADC1_ConvertedValue;
//choose the peripheral is the destination or source
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
//set the DMA buffer size 缓存大小
DMA_InitStructure.DMA_BufferSize=1; //16bits
//set the peripheral address register is incremented or not 递增与否
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
//set the memory address register is incremented or not 
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Disable;
//set the peripheral data size as 16bits(halfword)
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HlafWord;
//set the memory data size 
     DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HlafWord;
//set DMA mode
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
//set the channel priority 
DMA_InitStructure.DMA_Priority=DMA_Priority_High;
//enable the DMA memory to memory transfer
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
 
DMA_Init(DMA_Channel1,&DMA_InitStructure);
/*****enable channel 1******/
DMA_Cmd(DMA_Channel1,ENABLE);
 
/*****independent scanmode none external ,right align,***/
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode=ADC_ScanConvMode_ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExteralTrigConv_None;
ADC_InitStructure.ADC_DataAlign=ADC_DataAglin_Left;
ADC_InitStructure.ADC_NbrOfChannel=1;
ADC_InitStructure.ADC_ConstinuousConvMode=ADC_ConstinuousConvMode_ENABLE;
 
 //set the ADC clock
RCC_ADCCLKConfig(RCC_PCLK2_Div8);//PCLK2/8=9Mhz
 //set the AD sampling time  55.5个采样周期
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,1,ADC_SampleTime_55Cycle5);
 //enable the ADC begins DMA transfer
ADC_DMACmd(ADC1,ENABLE);
//enable the ADC1
ADC_Cmd(ADC1,ENABLE);
//enable ADC1 reset the calibration register
ADC_ResetCalibration(ADC1);
//when reset is ok ,it returns 0;if not ,wait
while(ADC_GetResetCalibration(ADC1));
 
//begin ADC1 calibration
ADC_StartCalibration(ADC1);
//wait untill the calibration is finished when it will return 0
while(ADC_GetCalibrationStatus(ADC1));
//software starts the conversation 
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
 }
 

 

it

文章评论0条评论)

登录后参与讨论
我要评论
0
11
关闭 站长推荐上一条 /2 下一条