硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1047|回复: 2
收起左侧

[客户分享] AT32F421-软件模拟过采样

[复制链接]

22

主题

16

回帖

82

积分

初级会员

积分
82
发表于 2021-11-25 20:15:19 | 显示全部楼层 |阅读模式
/*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();
       
        /* USART configuration */
        UART_Print_Init(115200);
       
       
        /* 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[i];                                                                //过采样求和
                        ADCConvertedValue_16 = (ADCConvertedValue_16>>ADC_shift)&0xFFFF;                        //求位移&取16位结果截断
                        DMA_ClearITPendingBit(DMA1_INT_TC1);
                        printf("ADCConvertedValue_16 = 0x%x\r\n",ADCConvertedValue_16);
                }

        }
}

/**
  * @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);
}


421-软件模拟过采样.rar

6.26 MB, 下载次数: 23

相关帖子

回复

使用道具 举报

5

主题

519

回帖

534

积分

金牌会员

积分
534
发表于 2021-11-26 10:03:31 | 显示全部楼层
大佬,讲讲,能达到16BIT?
回复

使用道具 举报

1

主题

1

回帖

4

积分

新手上路

积分
4
发表于 2022-3-9 16:52:20 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-18 23:33 , Processed in 0.220825 second(s), 31 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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