STM32 ADC DMA数据不稳定的解决方案

参考文章:
ADC 采样数据抖动
STM32 ADC 采样值不准确 情况分析及解决办法

在项目开发中,经常需要用到ADC采样的做电压检测,而且多通道ADC检测的情况比较多,所以本篇基于此要求采用了ADC DMA的方法,下面先给出基础代码(STM32F030)!

#define ADC1_DR_Address            0x40012440
//对应需要检测的ADC通道个数
#define ADC_DMA_BUFFER_SIZE     2

//按照通道顺序依次存放得到的ADC值
__IO uint16_t RegularConvData_Tab[ADC_DMA_BUFFER_SIZE] = {0, 0};

/**
  * @brief  DMA channel1 configuration
  * @param  None
  * @retval None
  */
void DMA_Config(void)
{
    DMA_InitTypeDef   DMA_InitStructure;
    /* DMA1 clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* DMA1 Channel1 Config */
    DMA_DeInit(DMA1_Channel1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = ADC_DMA_BUFFER_SIZE;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    /* DMA1 Channel1 enable */
    DMA_Cmd(DMA1_Channel1, ENABLE);
}

void ADC_DMA_Config(void)
{
    ADC_InitTypeDef     ADC_InitStructure;
    GPIO_InitTypeDef    GPIO_InitStructure;

    DMA_Config();

    /* ADC1 DeInit */
    ADC_DeInit(ADC1);
    /* ADC1 Periph clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    /* Configure ADC Channel11 and channel10 as analog input */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

    RCC_AHBPeriphClockCmd(P_N_TEST_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = P_N_TEST_PIN ;
    GPIO_Init(P_N_TEST_PORT, &GPIO_InitStructure);

    RCC_AHBPeriphClockCmd(DC5V_TEST_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = DC5V_TEST_PIN ;
    GPIO_Init(DC5V_TEST_PORT, &GPIO_InitStructure);

    /* Initialize ADC structure */
    ADC_StructInit(&ADC_InitStructure);

    /* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits  */
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
    ADC_Init(ADC1, &ADC_InitStructure);

    ADC_ChannelConfig(ADC1, DC5V_TEST_channel,ADC_SampleTime_239_5Cycles);
    ADC_ChannelConfig(ADC1, P_N_TEST_channel, ADC_SampleTime_239_5Cycles);

    /* ADC Calibration */
    ADC_GetCalibrationFactor(ADC1);

    /* ADC DMA request in circular mode */
    ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);

    /* Enable ADC_DMA */
    ADC_DMACmd(ADC1, ENABLE);

    /* Enable the ADC peripheral */
    ADC_Cmd(ADC1, ENABLE);

    /* Wait the ADRDY flag */
    while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));

    /* ADC1 regular Software Start Conv */
    ADC_StartOfConversion(ADC1);
    
}

ADC按照通道顺序循环采样并转换数据,然后DMA自动将对应的数据搬运至RegularConvData_Tab[]数组中。

使用该方法得到的ADC值有时候波动会比较大,如果不做滤波就直接采用的话,有可能会因为数据波动造成程序误判。如果将ADC值做中值滤波处理,即使有个别数据波动,对程序的影响则大幅度降低。因为DMA搬运新数据时会将旧数据覆盖掉,这里采用DMA中断处理,每发生一次DMA中断时将新的数据缓存起来,存够指定数量后再做中值滤波!void DMA_Config(void)函数加上DMA中断配置的代码如下

/**
  * @brief  DMA channel1 configuration
  * @param  None
  * @retval None
  */
void DMA_Config(void)
{
    DMA_InitTypeDef   DMA_InitStructure;
    NVIC_InitTypeDef  NVIC_InitStructure;
    /* DMA1 clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* DMA1 Channel1 Config */
    DMA_DeInit(DMA1_Channel1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = ADC_DMA_BUFFER_SIZE;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    /* DMA1 Channel1 enable */
    DMA_Cmd(DMA1_Channel1, ENABLE);
    
    /* Enable the DMA1 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    /* DMA1 interrupt */
    DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
}

DMA中断处理函数如下

//ADC DMA数据传输完成
void DMA1_Channel1_IRQHandler(void)
{
    /* Check the status of the specified DMAy flag */
    if ((DMA1->ISR & DMA1_FLAG_TC1) != (uint32_t)RESET)
    {
        ADC_DMA_INTERRUPT_HANDLER();
        //清除标志位
        DMA1->IFCR = DMA1_FLAG_TC1;
    }
}

void ADC_DMA_INTERRUPT_HANDLER(void)
{
    u8 i;
    static u8  times=0;
    static u16 buffer[ADC_DMA_BUFFER_SIZE];
    for (i=0;i<ADC_DMA_BUFFER_SIZE;i++) {
        buffer[i] += RegularConvData_Tab[i];
    }
    if (++times >= 8) { //取8次平均值
        for (i=0;i<ADC_DMA_BUFFER_SIZE;i++) {
            CalAverConvData_Tab[i] = buffer[i]>>3;
            buffer[i] = 0; //清零
        }
        times = 0;
    }
}
上一篇:css中块级元素、内联元素(行内元素、内嵌元素)


下一篇:基于PCIe DMA的多通道数据采集和回放IP