2015-06-29 10:04 AM
Hi,
I
am tyring to read 3 adc channel through DMAusing
STM32F4 discovery board ,ADC channel 10,11,12 through PC0,PC1,PC2 . i have connected 3 volt dc to the all three channel, but the control seems to be failed to exit from the loop while ((DMA_GetFlagStatus(DMA2_Stream4,DMA_FLAG_TCIF4)) != RESET) ; please find my piece of code below/* Define ADC init structures */
#include ''stdio.h''
#include ''stm32f4xx_adc.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_dma.h''
#include ''stm32f4xx_tim.h''
#include ''stm32f4xx_gpio.h''
#include ''misc.h''
#include ''app.h''
#define ADC_INTERNAL_REFERENCE 3300
#define ADC_12BIT_RES_COUNT 4096
#define MAX_ADC_CHANNELS 3
volatile unsigned int ADCConvertedValues[MAX_ADC_CHANNELS];
unsigned short ADC1_Conversion(unsigned char index)
{
unsigned short ADCConvertedVoltage = 0;
unsigned short result_adc_count = 0;
while ((DMA_GetFlagStatus(DMA2_Stream4,DMA_FLAG_TCIF4)) != RESET) ;
/* Clear DMA TC flag */
DMA_ClearFlag(DMA2_Stream4,DMA_FLAG_TCIF4);
/* Compute the voltage */
result_adc_count = ADCConvertedValues[index];
printf(''result_adc_count:%d\r\n'',result_adc_count);
ADCConvertedVoltage = (result_adc_count * ADC_INTERNAL_REFERENCE) / ADC_12BIT_RES_COUNT;
return ADCConvertedVoltage;
}
void adc_dma_init(void){
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
ADC_StructInit(&ADC_InitStructure);
ADC_CommonStructInit(&ADC_CommonInitStructure);
DMA_StructInit(&DMA_InitStructure);
/**
Set up the clocks are needed for the ADC
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
/**
Initialization of the GPIO Pins [OK]
*/
/* Analog channel configuration : PC.01, 02*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/**
Config the ADC1
*/
ADC_DeInit();
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //continuous conversion
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // NONE !Edge!
ADC_InitStructure.ADC_NbrOfConversion = MAX_ADC_CHANNELS;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // 1=scan more that one channel in group
ADC_Init(ADC1,&ADC_InitStructure);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,2,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_480Cycles);
/**
Configure the DMA
*/
//==Configure DMA2 - Stream 4
DMA_DeInit(DMA2_Stream4); //Set DMA registers to default values
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; //Source address
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValues[0]; //Destination address
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = MAX_ADC_CHANNELS; //Buffer size
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //source size - 16bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // destination size = 16b
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream4, &DMA_InitStructure); //Initialize the DMA
DMA_Cmd(DMA2_Stream4, ENABLE); //Enable the DMA2 - Stream 4
while(DMA_GetCmdStatus(DMA2_Stream4)==DISABLE){};
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 DMA
ADC_Cmd(ADC1, ENABLE); // Enable ADC1
ADC_ContinuousModeCmd(ADC1,ENABLE);
ADC_SoftwareStartConv(ADC1); // Start ADC1 conversion
}
int main(void)
{
adc_dma_init();
while(1)
{
ADC1_Conversion(0);
ADC1_Conversion(1);
ADC1_Conversion(2);
}
}
A
ny one can suggest what may cause the issue for this.regards
kudiarasu
2015-06-29 10:14 AM
Any one can suggest what may cause the issue for this.
The asserted state is not RESET?2015-06-29 11:40 PM
Thanks Clive1,
Now i have changed like below while (!DMA_GetFlagStatus(DMA2_Stream4,DMA_FLAG_TCIF4)); and control moves beyond this. But i have another issue, As mentioned in my previous post, i have reading the ADC channels PC0, PC1 &PC2 which actually connected 2.7volts to all three pins in my stm32f407 discovery board and i am expecting 3350 as a result. But result seems to be erratic and it reads 4095,4095,0 as a results . i thing did some mistakes in adc dma initialization . possible can you review my adc dma initialization code ? Thanks in advance kudiarasu.2015-06-30 08:07 AM
Skimming through it looks reasonable enough, not how I'd do it, but I've posted working examples.
PC0 has some potential issues on the STM32F4-DISCO, PC1,PC2 seem free. TC fires when all three samples are ready.