OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 608|回复: 0

AT32 ADC过采样例子(Artery MCU学习笔记)

[复制链接]

44

主题

49

帖子

0

精华

初级会员

Rank: 2

积分
175
金钱
175
注册时间
2017-5-6
在线时间
10 小时
发表于 2021-1-11 19:10:39 | 显示全部楼层 |阅读模式
ADC_over_sampling

/*************************************************/
This example describes how to use the ADC1 and DMA to transfer continuously  converted data from ADC1 to memory.
The ADC1 is configured to convert continuously channel14.
Each time an end of conversion occurs the DMA transfers, in circular mode, the converted data from ADC1 DR register to the ADCConvertedValue variable.
The ADC1 clock is set to 18 MHz.

/*ADC_OVSR:过采样率,取值:2,4,8,16,32,64,128,256*/
#define ADC_OVSR        256

/*ADC_shift:ADC结果右移,取值:0~8*/
#define ADC_shift  4

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitType ADC_InitStructure;
DMA_InitType DMA_InitStructure;
__IO uint16_t ADCConvertedValue[256] = {0,};
__IO uint32_t ADCConvertedValue_16 = 0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
      
        uint16_t i,j ;
      
        /* System clocks configuration */
        RCC_Configuration();

        /* GPIO configuration ------------------------------------------------------*/
        GPIO_Configuration();
               
        /* DMA1 channel1 configuration ----------------------------------------------*/
        DMA_Reset(DMA1_Channel1);
        DMA_DefaultInitParaConfig(&DMA_InitStructure);
        DMA_InitStructure.DMA_PeripheralBaseAddr    = (uint32_t)&ADC1->RDOR;
        DMA_InitStructure.DMA_MemoryBaseAddr        = (uint32_t)&ADCConvertedValue;
        DMA_InitStructure.DMA_Direction             = DMA_DIR_PERIPHERALSRC;
        DMA_InitStructure.DMA_BufferSize            = ADC_OVSR;
        DMA_InitStructure.DMA_PeripheralInc         = DMA_PERIPHERALINC_DISABLE;
        DMA_InitStructure.DMA_MemoryInc             = DMA_MEMORYINC_ENABLE;
        DMA_InitStructure.DMA_PeripheralDataWidth   = DMA_PERIPHERALDATAWIDTH_HALFWORD;
        DMA_InitStructure.DMA_MemoryDataWidth       = DMA_MEMORYDATAWIDTH_HALFWORD;
        DMA_InitStructure.DMA_Mode                  = DMA_MODE_CIRCULAR;
        DMA_InitStructure.DMA_Priority              = DMA_PRIORITY_HIGH;
        DMA_InitStructure.DMA_MTOM                  = DMA_MEMTOMEM_DISABLE;
        DMA_Init(DMA1_Channel1, &DMA_InitStructure);
        /* Enable DMA1 channel1 */
        DMA_ChannelEnable(DMA1_Channel1, ENABLE);

        /* ADC1 configuration ------------------------------------------------------*/
        ADC_StructInit(&ADC_InitStructure);
        ADC_InitStructure.ADC_Mode              = ADC_Mode_Independent;
        ADC_InitStructure.ADC_ScanMode          = ENABLE;
        ADC_InitStructure.ADC_ContinuousMode    = ENABLE;
        ADC_InitStructure.ADC_ExternalTrig      = ADC_ExternalTrig_None;
        ADC_InitStructure.ADC_DataAlign         = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_NumOfChannel      = 1;
        ADC_Init(ADC1, &ADC_InitStructure);
      
        /* ADC1 regular channels configuration */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1_5);   
      
        /* Enable ADC1 DMA */
        ADC_DMACtrl(ADC1, ENABLE);
      
        /* Enable ADC1 */
        ADC_Ctrl(ADC1, ENABLE);

        /* Enable ADC1 reset calibration register */   
        ADC_RstCalibration(ADC1);
        /* Check the end of ADC1 reset calibration register */
        while(ADC_GetResetCalibrationStatus(ADC1));

        /* Start ADC1 calibration */
        ADC_StartCalibration(ADC1);
        /* Check the end of ADC1 calibration */
        while(ADC_GetCalibrationStatus(ADC1));

        /* Start ADC1 Software Conversion */
        ADC_SoftwareStartConvCtrl(ADC1, ENABLE);      

        while (1)
        {
                while( DMA_GetITStatus(DMA1_INT_TC1) == SET)
                {
                        ADCConvertedValue_16 = 0;
                        for(i=0;i<ADC_OVSR;i++)
                        ADCConvertedValue_16 += ADCConvertedValue;//过采样求和
                        ADCConvertedValue_16 = (ADCConvertedValue_16>>ADC_shift)&0xFFFF;//求位移&取16位结果截断
                        DMA_ClearITPendingBit(DMA1_INT_TC1);
                }

        }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
        /* ADCCLK = PCLK2/4 */
        RCC_ADCCLKConfig(RCC_APB2CLK_Div4);
      
        /* Enable peripheral clocks ------------------------------------------------*/
        /* Enable DMA1 clocks */
        RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_DMA1, ENABLE);

        /* Enable ADC1 and GPIOA clocks */
        RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_ADC1 , ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_GPIOA , ENABLE);
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
        GPIO_InitType GPIO_InitStructure;

        /* Configure PA0 (ADC Channel0) as analog input -------------------------*/
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}

转载至21iic

ADC1_DMA.zip

13.43 MB, 下载次数: 72

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-14 18:49

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表