cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 adc with dma

aman
Associate II
Posted on July 08, 2013 at 11:06

I am  using stm32f100, i using it to control the motor . i am using 3 adc channels to read back-emf, dumping the adc-dr values in dma. am i doing it correct.........plz suggest me.

here's  the code

 here i using  an array of size 3 and dumping the value of each array in separate variable using dma. assume variables are declared

int ADCConvertedValue[3];

//==Configure DMA1 - Channel1==

DMA_DeInit(DMA1_Channel1); //Set DMA registers to default values

DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //Address of peripheral the DMA must map to

DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) & ADCConvertedValue; //Variable to which ADC values will be stored

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

DMA_InitStructure.DMA_BufferSize = 3; //Buffer size (2 because we using two channels)

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); //Initialise the DMA

DMA_Cmd(DMA1_Channel1, ENABLE); //Enable the DMA1 - Channel1

//////////////////////////////////////////////////////////////////////      

 

////////////////////////////////////////////////////////////////////////////

//==Configure ADC1 - Channel 14 and Channel 15==

ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

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 = 3; //We using two channels

ADC_Init(ADC1, &ADC_InitStructure); //Initialise ADC1

//Setup order in which the Channels are sampled....

ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);

ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5);

ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_55Cycles5);

ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 DMA

ADC_Cmd(ADC1, ENABLE); //Enable ADC1

//==Calibrate ADC1==

//Enable ADC1 reset calibaration register

ADC_ResetCalibration(ADC1);

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

//Start ADC1 calibaration

ADC_StartCalibration(ADC1);

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

void main()

{a=ADCConvertedValue[0];

   b=ADCConvertedValue[1];

   c=ADCConvertedValue[2];

}

#discovery #dma #stm32 #adc
3 REPLIES 3
crt2
Associate II
Posted on July 09, 2013 at 10:18

Just out of curiosity - what is the question? That ACD fills DMA and then this fills array? This is true, so if your code is doing that, then we can confirm that this is correct - otherwise you will need to zoom us in the problem.

Posted on July 09, 2013 at 12:54

The array should be volatile, the paste is a bit sloppy, main exits, doesn't call anything, and doesn't loop.

Please, if you're going to do this, post a complete example.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aman
Associate II
Posted on July 09, 2013 at 13:45

 sorry , for what i did....in the org. code the functions has been called in main ......(just to save time,wrote partial code)

i wanted to ask, have i done it in the correct way of initializing the array in DMA and reading it in main,am i getting the correct  values (acc. to code) of all 3 adc channel values in the array. ?

void ADC_DMA_Configuration() {

GPIO_InitTypeDef GPIO_InitStructure; //Variable used to setup the GPIO pins

DMA_InitTypeDef DMA_InitStructure; //Variable used to setup the DMA

ADC_InitTypeDef ADC_InitStructure; //Variable used to setup the ADC

//==Configure the systems clocks for the ADC and DMA==

//ADCCLK = PCLK2 / 2

RCC_ADCCLKConfig(RCC_PCLK2_Div2); //Defines the ADC clock divider. This clock is derived from the APB2 clock (PCLK2). The

//ADCs are clocked by the clock of the high speed domian (APB2) dibivied by 2/4/6/8. The

//frequency can never be bigger than 14MHz!!!!

//--Enable DMA1 clock--

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

//--Enable ADC1 and GPIOC--

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC , ENABLE);

 

//==Configure ADC pins (PC.00 -> Channel 10, PC.01 -> Channel 11 & PC.02 -> Channel 12) as analog inputs==

GPIO_StructInit(&GPIO_InitStructure); // Reset init structure, if not it can cause issues...

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

GPIO_Init(GPIOC, &GPIO_InitStructure);

//==Configure DMA1 - Channel1=

DMA_DeInit(DMA1_Channel1); //Set DMA registers to default values

DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //Address of peripheral the DMA must map to

DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) & ADCConvertedValue; //Variable to which ADC values will be stored

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

DMA_InitStructure.DMA_BufferSize = 3; //Buffer size (3 because we using 3 channels)

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); //Initialise the DMA

DMA_Cmd(DMA1_Channel1, ENABLE); //Enable the DMA1 - Channel1

//////////////////////////////////////////////////////////////////////      

////////////////////////////////////////////////////////////////////////////

//==Configure ADC1 - Channel 14 and Channel 15==

ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

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 = 3; //We using 3 channels

ADC_Init(ADC1, &ADC_InitStructure); //Initialise ADC1

//Setup order in which the Channels are sampled....

ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);

ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5);

ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_55Cycles5);

ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 DMA

ADC_Cmd(ADC1, ENABLE); //Enable ADC1

//==Calibrate ADC1==

//Enable ADC1 reset calibration register

ADC_ResetCalibration(ADC1);

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

//Start ADC1 calibaration

ADC_StartCalibration(ADC1);

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

}//end ADC_Configuration

 

void ADC_DMA_Test_Program() 

{

/* Start ADC1 Software Conversion */

ADC_SoftwareStartConvCmd(ADC1, ENABLE);

}

void

main(void) 

{

      

ADC_DMA_Configuration();

 

    

ADC_DMA_Test_Program();

while (1)

{a=

 ADCConvertedValue

[0

]

;b=

ADCConvertedValue

[1

]

;

c=

ADCConvertedValue

[2

]

;

}

}

i also want to know about sampling time in this ADC, because i am not getting enough samples . does DMA read cycles affecting the samples?