⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于CORTEX-M3 内核的STM32两路ADC采集例程
💻 C
字号:

/* Includes ------------------------------------------------------------------*/
#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 ADC_ConvertedValue1;
ErrorStatus HSEStartUpStatus;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);

/* Private functions ---------------------------------------------------------*/
void delay(void);
void delay()
{
int i,j=0;
for (i=0; i<0xfffff; i++) j++;
}

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

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

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

  /* DMA channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA_Channel1); //复位DMA_Channel1
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //外围设备地址
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue1; //memory 地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外围设备做为源
  DMA_InitStructure.DMA_BufferSize = 1;  //数据单元尺寸
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  //外围地址是否自动增长 disable不增长
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //memory 是否自动增长
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外围设备寄存器尺寸16位
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;  //memory尺寸 16位
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  //DMA 循环模式
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;  //DMA 通道优先级
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //memory- to-memory转换
  DMA_Init(DMA_Channel1, &DMA_InitStructure);  //初始化DMA通道1

  /* Enable DMA channel1 */
  DMA_Cmd(DMA_Channel1, ENABLE);  //使能DMA通道1

  /* ADC1 configuration ------------------------------------------------------*/
                                                       
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  //ADC 工作模式 ADC1 ADC2 单独工作    
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;  //使能多通道扫描模式
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //是否启用连续转换模式
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  //触发模式 非外部触发启动转换 软件触发
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;   //数据右对齐
  ADC_InitStructure.ADC_NbrOfChannel = 2;  //ADC规则转换通道数量
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel10 configuration */ //配置转换规则
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);  //从通道0到通道n的自动扫描模式
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5);
  //通道adc1 adc通道 规则转换序列号(优先级) 转换频率                       
  
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);

  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

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

  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);  //开始ADC1 校准转换
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));  //等待转换完成

  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);  //软件使能ADC转换

#if 1
    while(1)
  {
    vu16 value1 = ADC_ConvertedValue1;
    u8 num1 = value1&0xf;
    //u8 num2 = (value1&0xff)>>4;
    //u8 num3= (value1&0xfff)>>8;
    //u8 num4 = value1>>12;
    //delay();
  }
#endif
}

/*******************************************************************************
* 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); //使能HSE外部时钟 External High Speed oscillator

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待HSE启动

  if(HSEStartUpStatus == SUCCESS) //启动成功
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能flash时钟欲分频器

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2); //flash code潜伏周期 latency FLASH_Latency_2Two Latency cycles

    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB时钟

    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1); //Configures the High Speed APB clock (PCLK2).

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2); //Configures the Low Speed APB clock (PCLK1).

    /* ADCCLK = PCLK2/4 */
    RCC_ADCCLKConfig(RCC_PCLK2_Div4); //Configures the ADC clock (ADCCLK).

    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

/* Enable peripheral clocks --------------------------------------------------*/
  /* Enable DMA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);

  /* Enable ADC1 and GPIOC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

}

/*******************************************************************************
* 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.00 (ADC Channel10) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;  //模拟输入
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

#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

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -