cancel
Showing results for 
Search instead for 
Did you mean: 

wrong values- adc with dma

arunl4g
Associate II
Posted on July 14, 2015 at 09:48

#include <stm32f4xx.h>

#include <stm32f4xx_conf.h>

i have posted the code below and i get wrong value. Can anyone tell me whats wrong ?

volatile uint16_t Buffer[12];

uint8_t receiveword;

/* ADC Configuration */

void adcConfig()

{

//clock for leds

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

//GPIO intialization for leds

GPIO_InitTypeDef GPIO_IntStruct;

GPIO_IntStruct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_IntStruct.GPIO_OType = GPIO_OType_PP ;

GPIO_IntStruct.GPIO_Pin = GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15;

GPIO_IntStruct.GPIO_PuPd = GPIO_PuPd_UP ;

GPIO_IntStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_IntStruct);

/******************** PA1 - channel1 --data sheet page number 48*******************/

//clock for PA1

RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);

//GPIO init

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//clock for dma --------page number 308 ----channel 0 and stream 0

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

DMA_DeInit(DMA2_Stream0);

DMA_InitTypeDef DMA_InitStruct;

DMA_InitStruct.DMA_Channel = DMA_Channel_0;

DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; // Receive

DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)Buffer;

DMA_InitStruct.DMA_BufferSize = (uint16_t)sizeof(Buffer);

DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;

DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

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_Enable;

DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;

DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;

DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

DMA_Init(DMA2_Stream0, &DMA_InitStruct);

//enable dma2

DMA_Cmd(DMA2_Stream0, ENABLE);

//clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

//clock for the port ------------- i dont think its needed.

RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN, ENABLE);

//common init configuration

ADC_CommonInitTypeDef ADC_CommonInitStruct;

ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;

ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div2;

ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;

ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

ADC_CommonInit(&ADC_CommonInitStruct);

//config init

ADC_InitTypeDef ADC_InitStructure;

ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

ADC_InitStructure.ADC_ScanConvMode = DISABLE;

ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left;

ADC_InitStructure.ADC_NbrOfConversion = 1;

ADC_Init(ADC1, &ADC_InitStructure);

//samplign time config

ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles);

//dma request

ADC_DMACmd(ADC1, ENABLE);

//enable adc

ADC_Cmd(ADC1, ENABLE);

}

/**************************************************************************************/

int main(void)

{

adcConfig();

int i ;

ADC_SoftwareStartConv(ADC1);

while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion

if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == SET)

{

GPIO_SetBits(GPIOD, GPIO_Pin_13);

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

{

printf(''%d\n'', Buffer[i]);

}

}

return 0;

}

18 REPLIES 18
manuel2399
Associate II
Posted on July 14, 2015 at 10:47

Probably negative voltage / Offset

try:

volatile int16_t Buffer[12];

arunl4g
Associate II
Posted on July 14, 2015 at 11:57

no its not working when i change it ...its even getting worse values...

manuel2399
Associate II
Posted on July 14, 2015 at 13:25

Try

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

nesrine
Senior
Posted on July 14, 2015 at 13:25

Hello,

I would suggest you to Have a look at the ADC_DMA example in the STM32F4 Standard Peripherals Library.

You can download it from this 

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1939/PF257901

 

I hope this helps,

Syrine.
Posted on July 14, 2015 at 18:37

The left shift is definitely going to produce large values. It's important to read and understand the documentation.

With ADC measurements, you often want to test by looking at the values in decimal and hex, for inputs tied to GROUND and then VREF.

If you get surprising values, review the documentation again.

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

thank you clive.

yes, it does. i have look at the documentation and calculated according to that. 

i am getting proper values when i calculate for the first time. but i  need to reset to calculate again. if i dont do reset then it gives value 0.

why its is like that?
Posted on July 15, 2015 at 15:08

Ok, you have an array with 12 elements, you tell the DMA controller it has 24, and then print 20. You need to pick something and apply it consistently.

If you program the DMA in Normal mode you'll need to reinitialize it each time. Circular mode will keep repeating.

I would not use EOC, but rather the DMA HT/TC flags to determine if the array was filled.

I've posted working examples, perhaps a careful review of those would help.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arunl4g
Associate II
Posted on July 15, 2015 at 15:59

clive, can you tell me how the dma is 24?... according to my knowledge, the buffer size is the dma size... if not correct can u tell me how its 24. 

arunl4g
Associate II
Posted on July 22, 2015 at 19:35

clive, can you tell me how the dma is 24?... according to my knowledge, the buffer size is the dma size... if not correct can u tell me how its 24.