AnsweredAssumed Answered

wrong values- adc with dma

Question asked by kaka on Jul 14, 2015
Latest reply on Jul 24, 2015 by Clive One

#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;
}

Outcomes