cancel
Showing results for 
Search instead for 
Did you mean: 

Read multi channels ADC using DMA

dthan.17
Associate

hi everyone. my project uses STM32f407, i read 4 channesl analog by ADC1 and using DMA .

when i debug the program, i can only read the first analog value... please help me.

bellow this is my program

#include "stm32f4xx.h"
#include "stdio.h"
 
GPIO_InitTypeDef	GPIO_InitStruct;
ADC_InitTypeDef	ADC_InitStruct;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
USART_InitTypeDef			USART_InitStruct;
RCC_ClocksTypeDef RCC_ClockFreq;
DMA_InitTypeDef					DMA_InitStruct;
 
void Configue_clock(void);
void Configue_adc(void);
void Configue_uart(void);
void Configue_timer(void);
void Configue_dma(void);
void Delay( uint32_t     num);
 
uint16_t a[4];
__IO uint16_t uhADC1ConvertedValue[4];
 
int main()
{
	uint8_t i =0;
	SystemInit();
	Configue_clock();
	Configue_adc();
	Configue_dma();
	Configue_uart();
 
	RCC_GetClocksFreq(&RCC_ClockFreq);
	while(1)
	{ 
		if(ADC_GetSoftwareStartConvStatus(ADC1) == RESET)
		{	
			ADC_SoftwareStartConv(ADC1);
			ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
			a[0] = uhADC1ConvertedValue[0];
			Delay(1000);
			
		
			ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 2, ADC_SampleTime_3Cycles);
			a[1] = uhADC1ConvertedValue[1];
			Delay(1000);
	
			ADC_RegularChannelConfig(ADC1,ADC_Channel_4, 3, ADC_SampleTime_3Cycles);
			a[2] = uhADC1ConvertedValue[2];
			Delay(1000);
		
			ADC_RegularChannelConfig(ADC1,ADC_Channel_5, 4,ADC_SampleTime_3Cycles);
			a[3] = uhADC1ConvertedValue[3];
			Delay(1000);
		}
	}
}
//----------------------------------------------------------------------------------------------//
void Configue_clock(void)
{
 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOD, ENABLE);
//RCC_ADCCLKConfig(RCC_ADCPLLCLK_Div4);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 , ENABLE);	
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStruct);
	
}
 
//-----------------------------------------------------------------------------------------------//
void Configue_adc(void)
{
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);	
 
	ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_3;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInit(&ADC_CommonInitStructure); 
	
  ADC_InitStruct.ADC_ScanConvMode = ENABLE;
  ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;	
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStruct.ADC_NbrOfConversion = 4;
  ADC_Init(ADC1, &ADC_InitStruct);
  
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
 
	ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 2, ADC_SampleTime_3Cycles);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_4, 3, ADC_SampleTime_3Cycles);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_5, 4, ADC_SampleTime_3Cycles);
	
	ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
 
  ADC_Cmd(ADC1, ENABLE);
	ADC_DMACmd(ADC1, ENABLE);
}
 
//-----------------------------------------------------------------------------------------------//
 
void Configue_dma(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
	
	DMA_InitStruct.DMA_Channel = DMA_Channel_0; 
  DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
  DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&uhADC1ConvertedValue;
  DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStruct.DMA_BufferSize = 4;
  DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  
	DMA_Init(DMA2_Stream0, &DMA_InitStruct);
 
	DMA_Cmd(DMA2_Stream0, ENABLE);
 
}
 
void Delay( uint32_t     num)
{
	uint32_t     index = 0;
	for(index = (10000 * num);
	index != 0; index--);
}
 

0 REPLIES 0