ADC1-DMA

来源:百度文库 编辑:神马文学网 时间:2024/04/28 14:38:38
 

ADC1-DMA

#include "stm32f10x_lib.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address    ((u32)0x4001244C)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
vu16 ADCConvertedValue;
ErrorStatus HSEStartUpStatus;
   
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
 
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif

  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* NVIC configuration ------------------------------------------------------*/
  NVIC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  /* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);   //复位DMA1的通道1
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//DMA对应的外设基础地址,这个地址可以在相应的DATASHEET上查
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCConvertedValue;  //定义DMA通道存储器地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;   //指定外设为源地址
  DMA_InitStructure.DMA_BufferSize = 1;    //定义DMA缓存大小为1
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  //接收一次后,外设寄存器地址不变。
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;    //设置DMA内存模式为非递增模式。ENABLE?
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;  //定义外设数据宽度为16位。
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;    //定义存储器数据宽度为16位。
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;     //设置DMA为连续循环的传输模式。
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;     //高优先级
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;        //禁止DMA通道的M2M传输模式。
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);         //初始化DMA通道1
                     
  /* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);           //使能DMA通道1
    
  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  //设置转换模式为独立转换  。
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;     //开启扫描转换模式。
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  // 开启连续转换模式。
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  //关闭ADC外部转换触发。
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  //设置为右对齐方式。
  ADC_InitStructure.ADC_NbrOfChannel = 1;      //开启通道数1个。
  ADC_Init(ADC1, &ADC_InitStructure);       //ADC初始化  。

  /* ADC1 regular channel14 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);   //规则组通道设置

  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);    //和DMA关联
 
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);     //开户ADC1

  /* Enable ADC1 reset calibaration register */  
  ADC_ResetCalibration(ADC1);      //重置校准。
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));   //等待重置校准完成。

  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);    //开始较准。
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));    //等待校准完成。
    
  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);  //连续转换开始。

  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)    //配置系统时钟
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();      //复位RCC

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);    //使能外部晶振

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部晶振稳定

  if(HSEStartUpStatus == SUCCESS)   //如果外部晶振启动成功则进行下一步操作。
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //选择FLASH预取指缓存的模式

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);   //设置FLASH存储器延时时钟周期数。
 
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);    //设置AHB时钟=系统时钟
 
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);   //设置APB2=HCLK时钟

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);     //设置APB1时钟=HCLK时钟/2。

    /* ADCCLK = PCLK2/4 */
    RCC_ADCCLKConfig(RCC_PCLK2_Div4);   // 设置ADC时钟=APB2时钟/4。
 
    /* PLLCLK = 8MHz * 7 = 56 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);  //设置PLL时钟源和PLL的输入时钟=7倍的HCLK

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);     //使能PLL

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL稳定。
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);    //系统时钟来自PLL

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)  //切换时钟后等待系统稳定。PLL返回用作系统时钟的时钟源。0x08:PLL作为系统时钟
    {
    }
  }

/* Enable peripheral clocks --------------------------------------------------*/
  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  //启动DMA1的时钟

  /* Enable ADC1 and GPIOC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);//启动ADC1和GPIOC的时钟。
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef  VECT_TAB_RAM 
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
#endif
}

#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif