cancel
Showing results for 
Search instead for 
Did you mean: 

ADC Sampling Rate

s7v7n92
Associate II
Posted on August 15, 2014 at 15:57

I am using a STM32L series MCU. I am trying to trigger ADC at 16KHz with a TIM3 and later use DMA to store the ADC values in a buffer. The issue is that I am trying to test the ADC rate using a GPIO_Toggle in the ADC1_IRQ but it does not toggle with the correct frequency. I have tested the TIM3 and it is triggering at 16KHz. I am attaching the code:

[code]

//APB1 @32MHz

void NVIC_Config();

void TIM_Config();

void GPIO_Config();

void ADC_Config();

void DMA_Channel1_Config();

uint8_t ADCConvertedValue[512];

int main()

{  

  NVIC_Config();

  TIM_Config();

  GPIO_Config();

  ADC_Config();

  DMA_Channel1_Config();

  while(1)

  {

  }

}

void NVIC_Config()

{

  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

void TIM_Config()

  GPIO_InitTypeDef GPIO_InitStructure;

  TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;

  RCC_APB1PeriphClockCmd(RCC_Periph_TIM3 , ENABLE);

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Prescaler = 2 - 1; // 

  TIM_TimeBaseStructure.TIM_Period = 1000-1;                       

  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /*TIM3_TRGO triggering ADC*/

  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

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

    

  TIM_Cmd(TIM3, ENABLE);

}

void GPIO_Config()

{

  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0 ;

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

}

void ADC_Config()

{

  ADC_InitTypeDef ADC_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  ADC_BankSelection(ADC1, ADC_Bank_A);

  

  ADC_StructInit(&ADC_InitStructure);

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;

  ADC_InitStructure.ADC_ScanConvMode = DISABLE;

  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;

  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_NbrOfConversion = 1;

  ADC_Init(ADC1, &ADC_InitStructure);

  

  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_48Cycles);

   ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

   ADC_DMACmd(ADC1, ENABLE);

   ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);

  ADC_Cmd(ADC1, ENABLE);

  while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)

  {

  }

   

}

 void DMA_Channel1_Config()

 {

  //DMA_InitTypeDef   DMA_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  /* Enable DMA1 channel1 IR1 Channel */

  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  

  DMA_DeInit(DMA1_Channel1);

  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;

  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

  DMA_InitStructure.DMA_BufferSize = 512;

  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_M2M = DMA_M2M_Disable;

  DMA_Init(DMA1_Channel1, &DMA_InitStructure);  

  

  DMA_Cmd(DMA1_Channel1, ENABLE);

 }

void ADC1_IRQHandler()

{

  if(ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET)

   {

    ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); 

     GPIO_ToggleBits(GPIOC,GPIO_Pin_0);

    

  }

  

}

[/code]

1 REPLY 1
os_kopernika
Associate II
Posted on August 15, 2014 at 19:47

No, I didn't analyse your unformatted code.

Debugging ADC on STM32L is PITA as it is clocked from asynchronous RC. The ADC ticking cannot be halted by halting the core while debugging..

Check code provided from STM from peripheral library. First start from polling then IRQs and only then try DMA.