cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 ADC 2channels + DMA

tom23
Associate II
Posted on December 30, 2013 at 14:47

I have realized 2channels ADC (PC1, PC2) with DMA. I found and used many examples in forum and ST examples, but It still do not work. It is depressing:-)

My problem is: 1) If I change the voltage on PC1, first channel works, but second channel shows the value too (it is smaller than first one) 2) If I change the voltage on PC2,it does not appear in either channel. Thank you for help

/* Includes ------------------------------------------------------------------*/
#include ''main.h''
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define MSG1 ''This is my console ''
#define MSG2 '' U = %d,%d V ''
#define MSG3 '' Raw = %d ''
#define MSG4 '' T = %d,%d oC ''
#define LINENUM 0x15
#define FONTSIZE Font12x12
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint16_t ADCConvertedValue[2];
__IO uint32_t ADCConvertedVoltage[2];
/* Private function prototypes -----------------------------------------------*/
static
void
Display_Init(
void
);
static
void
Display(
void
);
void
pinout_config(
void
);
/* Private functions ---------------------------------------------------------*/
int
main(
void
)
{
pinout_config();
Display_Init();
if
(SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while
(1);
}
while
(1)
{
// display on screen
Display();
// delay
Delay(200);
}
}
/**
* @brief Display Init (LCD)
* @param None
* @retval None
*/
static
void
Display_Init(
void
){
/* Initialize the LCD */
LCD_Init();
LCD_LayerInit();
/* Eable the LTDC */
LTDC_Cmd(ENABLE);
/* Set LCD Background Layer */
LCD_SetLayer(LCD_BACKGROUND_LAYER);
/* Clear the Background Layer */
LCD_Clear(LCD_COLOR_WHITE);
/* Configure the transparency for background */
LCD_SetTransparency(0);
/* Set LCD Foreground Layer */
LCD_SetLayer(LCD_FOREGROUND_LAYER);
/* Configure the transparency for foreground */
LCD_SetTransparency(200);
/* Clear the Foreground Layer */
LCD_Clear(LCD_COLOR_WHITE);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(LCD_COLOR_BLUE);
LCD_SetTextColor(LCD_COLOR_WHITE);
/* Set the LCD Text size */
LCD_SetFont(&FONTSIZE);
LCD_DisplayStringLine(LINE(0x01), (uint8_t*)MSG1);
LCD_DisplayStringLine(LINE(0x19), (uint8_t*)
'' ''
);
/* Set the LCD Text size */
LCD_SetFont(&Font16x24);
/* Set the LCD Back Color and Text Color*/
LCD_SetBackColor(LCD_COLOR_WHITE);
LCD_SetTextColor(LCD_COLOR_BLUE);
}
/**
* @brief Display ADCs converted values on LCD
* @param None
* @retval None
*/
static
void
Display(
void
){
uint32_t a =0, b=0;
uint8_t aTextBuffer[50];
uint8_t i = 0;
for
(i=0; i<2; i++)
{
sprintf
((
char
*)aTextBuffer, MSG3, ADCConvertedValue[i]);
LCD_DisplayStringLine(LINE(1+i), (uint8_t*)aTextBuffer);
ADCConvertedVoltage[i] = 3000 * ADCConvertedValue[i] / 0xFFF;
a = (ADCConvertedVoltage[i])/1000;
b = (ADCConvertedVoltage[i]%1000)/100;
sprintf
((
char
*)aTextBuffer, MSG2, a, b);
LCD_DisplayStringLine(LINE(4+i), (uint8_t*)aTextBuffer);
}
}
void
pinout_config(
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_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/**
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)&ADCConvertedValue[0]; 
//Destination address
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 2; 
//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
/**
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_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_NbrOfConversion = 2;
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_11,1,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_12,2,ADC_SampleTime_480Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE); 
//Enable ADC1 DMA
ADC_Cmd(ADC1, ENABLE); 
// Enable ADC1
ADC_SoftwareStartConv(ADC1); 
// Start ADC1 conversion
}

#hello-pleace-give-me-main.h-file #adc #dma #me-too #understand-your-tools #stm32f4-adc-dma #hello-please-help-me
27 REPLIES 27
toogoo369
Associate
Posted on March 07, 2015 at 08:22

hello I am getting start stm32f407, I cnat ADC value transfer to LCD1602 , follow, first i do configuratian on keil uvision4

________________

Attachments :

ADC3_DMA.uvproj : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0nU&d=%2Fa%2F0X0000000bdn%2F54hsv_9jd7t06rvHwLiZa7A0ohl8iCh3Ww2F5qg5AFA&asPdf=false
Posted on March 07, 2015 at 13:26

hello I am getting start stm32f407, I cant ADC value transfer to LCD1602 , follow, first i do configuration on keil uvision4

Ok, you have attached a project meta-data file for files that reside on your system, you'll need to zip up and attach a more selected group of files.

Perhaps you should also review some other ADC examples posted to the forum?

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Multi%20Channel%20ADC%20reading&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=4512]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FMulti%20Channel%20ADC%20reading&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=4512

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
toogoo369
Associate
Posted on March 08, 2015 at 12:30

Thank clive, did you watch my attached file i need ADC value to LCD 

Tomas pleace give me main.h file  

toogoo369@yahoo.com
Posted on March 08, 2015 at 15:30

Thank clive, did you watch my attached file i need ADC value to LCD 

 

You attached a project file, there is NO code in that. You should be able to attach main.c and main.h to the forum here.

Do the other ADC examples work to the point where you can see the values in the debugger?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sunilkasar7
Associate II
Posted on September 09, 2015 at 14:43

I am also facing same problem. Though I used PC3(Ch13) and PA5(CH5), problem is unsolved.

sunilkasar7
Associate II
Posted on September 09, 2015 at 15:26

Hi Thomas,

I am also facing exactly same problem. Did you get any solution for it? If so, please help me to get rid from this problem.

Posted on September 09, 2015 at 16:48

I am also facing same problem.

What specifically is *your* problem. There are dozens of examples posted to the forum.

You're working in a technical field you need to be clear and specific, both about what code you're working on, which part and what aspect is ''not working''

Don't expect respondents from ~2 years ago to magically appear.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sunilkasar7
Associate II
Posted on September 10, 2015 at 09:19

SINT8 Str_ADC_Count[10];

UINT16 ADC_Count_u16 = 0;

UINT16 ADC_values_au16[CH_TOTAL_NUM_E];

static void DMA_Config(void)  

{

    

    DMA_InitTypeDef  DMA_InitStructure;

    DMA_DeInit(DMA2_Stream0);                                       

    DMA_InitStructure.DMA_Channel = DMA_Channel_0;                    

    DMA_InitStructure.DMA_PeripheralBaseAddr = (UINT32)&ADC1->DR;

    DMA_InitStructure.DMA_Memory0BaseAddr = (UINT32)&ADC_values_au16[0];

    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;

    DMA_InitStructure.DMA_BufferSize = CH_TOTAL_NUM_E;                

    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_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_Stream0, &DMA_InitStructure);                 

    DMA_Cmd(DMA2_Stream0, ENABLE);               

}    

void DMA2_Init(void)

{

    RCC_Configuration_DMA();

    DMA_Config();     

}

static void RCC_Configuration(void)

{   

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

}

static void GPIO_Configuration()

{

    GPIO_InitTypeDef  GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;  // channel_0 PA0

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void ADC1_Init(void)

{

    ADC_InitTypeDef    ADC_InitStructure;

    ADC_CommonInitTypeDef ADC_CommonInitStructure;

    RCC_Configuration();

    GPIO_Configuration();

    ADC_DeInit();

    

     ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;

     ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;

     ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; // Orig dis

     ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

     ADC_CommonInit(&ADC_CommonInitStructure);

    

    

    ADC_StructInit(&ADC_InitStructure);

    

    ADC_InitStructure.ADC_Resolution = ADC_Resolution_10b;

    ADC_InitStructure.ADC_ScanConvMode = ENABLE;         

    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

    ADC_InitStructure.ADC_NbrOfConversion = 4;    

    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

    ADC_Init(ADC1, &ADC_InitStructure);

    

    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_56Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_56Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_56Cycles);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_56Cycles);

    

    ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

    

    

    

    ADC_DMACmd(ADC1, ENABLE);

    ADC_Cmd(ADC1, ENABLE);  

    ADC_SoftwareStartConv(ADC1);

}

void delay_short()

{

    UINT32 delayCnt = 0;

    for(delayCnt = 0; delayCnt < 100000; delayCnt++)

    {;}

}

void delay_long()

{

    UINT32 delayCnt = 0;

    for(delayCnt = 0; delayCnt < 10000000; delayCnt++)

    {;}

}

int main(void)

{

    UINT32 i = 0;

    DMA2_Init();

    ADC1_Init();

    USART1_Init();

    Serial_PutString(''ADC Init complete\n'', USART1);

    Serial_PutString(''Usart Init complete\n'', USART1);

    

    while(1)

    {

        for(i = 0; i < 4; i++)

        {

            sprintf((char*)Str_ADC_Count, ''%d, \n'',ADC_values_au16[i]);

            Serial_PutString(Str_ADC_Count, USART1);

            delay_short();

          }

        Serial_PutString(''         ;'', USART1);

        delay_long();

    }    

}

sunilkasar7
Associate II
Posted on September 10, 2015 at 09:31

I have configured 4 pins for ADC- PA0,PA1, PA2, and PA3. when I change voltage at one pin, it's effect is influencing on other 3 pins.

sunilkasar7
Associate II
Posted on September 14, 2015 at 07:34

Hi Clive,

I have shared my code. Please suggest me solution for my problem.

Thanking you.