AnsweredAssumed Answered

Again...ADC with 2 Channel with DMA not working!?

Question asked by potato.mr on Oct 15, 2015
Latest reply on Oct 15, 2015 by Clive One
Hi.I'm a newbie in ST and ARM.I tried to setup 2 Channel ADC with DMA but it's not working.Can somebody guide me?.I think i keep on making mistakes somewhere.Thanks!

/*Preprocessors Directives*/
#include <stm32f0xx_adc.h>
#include <stm32f0xx_gpio.h>
#include <stm32f0xx_rcc.h>
#include <stm32f0xx_dma.h>
 
/*APB memory address*/
#define ADC1_DR_Address                0x40012440
 
/*Variable declaration - ADC*/
volatile uint16_t ADC1_val[2];
 
/*Function Prototypes*/
void ADC_GPIO_DMA_CONFIG(void);
 
#define BLed GPIO_Pin_8;
 
int main(void)
{
    //RCC_ClocksTypeDef     RCC_Clocks;
 
    //RCC_GetClocksFreq(&RCC_Clocks);                                  
 
    //if (SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000))        
    //while(1);
 
    ADC_GPIO_DMA_CONFIG();
 
    while(1)
    {
        if(ADC1_val[0] > 2000)
        {
            GPIOC->BRR = BLed;
        }
        else
        {
            GPIOC->BSRR = BLed;
        }
    }
}
 
/*Initialise Configurations for GPIO, ADC and DMA*/
void ADC_GPIO_DMA_CONFIG()
{
 
    ADC_InitTypeDef   ADC_InitStructure;                       
    DMA_InitTypeDef   DMA_InitStructure;                     
    GPIO_InitTypeDef  GPIO_InitStructure;                    
 
    SysTick_Config(SystemCoreClock/1000);                      
 
    /*************************************************/
    /*Configure AHB Peripheral Clocks for PA.1 & PA.2*/
    /*              ANALOG INPUT PINS                */
    /*************************************************/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;     
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;             
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;         
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    /*************************************************/
    /*Configure AHB Peripheral Clocks for PC.9 & PC.8*/
    /*                   LEDS                        */
    /*************************************************/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;              
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;          
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;        
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;          
    GPIO_Init(GPIOC, &GPIO_InitStructure);
 
    /************************************************/
    /*  Configure ADC and DMA in nested statements  */
    /*  Somehow confusing but it works compared to  */
    /*  seperate configuration methods              */
    /************************************************/
    ADC_DeInit(ADC1);                                          
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);       
 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);        
 
    /************************************************/
    /*             DMA Configuration                */
    /************************************************/
 
    DMA_DeInit(DMA1_Channel1);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;            
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_val;                 
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = 2;                                      
    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);
 
    DMA_Cmd(DMA1_Channel1, ENABLE);                                            
    ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);                     
    ADC_DMACmd(ADC1, ENABLE);                                                  
 
    /************************************************/
    /*           ADC Configuration                  */
    /************************************************/
 
    ADC_StructInit(&ADC_InitStructure);                                         /
 
    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_Backward;
    ADC_Init(ADC1, &ADC_InitStructure);
 
    /*ADC_Channel_1 ->PA.1 - Conversion period at 55.5 Cycles (enough for small impedance) */
    ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_55_5Cycles);
 
    /*ADC_Channel_2 ->PA.2 - Conversion period at 55.5 Cycles (enough for small impedance) */
    ADC_ChannelConfig(ADC1, ADC_Channel_2 , ADC_SampleTime_55_5Cycles);
 
    ADC_GetCalibrationFactor(ADC1);                                        
    ADC_Cmd(ADC1, ENABLE);                                                     
 
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));                           
    ADC_StartOfConversion(ADC1);                                               
 
}

Outcomes