cancel
Showing results for 
Search instead for 
Did you mean: 

problem with stm32f adc+timer+

ghfa0098
Associate
Posted on November 27, 2015 at 08:31

i'm beginner in stm32f407 .i'm trying to get adc values of 6 channels at 2khz frequency

i dont know how to do it.i tested timer i expected is overfollws in 1s but it isnt giving me the right results . ive questions about adc and timer clock

1-what is these two terms in adc configuration

** ADC_TwoSamplingDelay_5Cycles;

**ADC_TwoSamplingDelay;

2-how can i configure adc

sampling rate

with timer and interrupt

please someone kindly help me to do it here is my code :

#include ''stm32f4xx.h''

#include ''system_stm32f4xx.h''

#include ''tm_stm32f4_hd44780.h''

#include ''stm32f4xx.h''

#include ''stm32f4xx_tim.h''

#include ''stm32f4xx_rcc.h''

#include ''stm32f4xx_gpio.h''

#include ''defines.h''

//#include ''arm_const_structs.h''

//#include ''math_helper.h''

//#include ''system_ARMCM4.h''

#include ''stm32f4xx_adc.h''

#include ''stm32f4xx_dma.h''

#include ''tm_stm32f4_adc.h''

#include ''tm_stm32f4_delay.h''

#include ''tm_stm32f4_gpio.h''

#include ''stdio.h''

#define rezabuffer0 [1024]

#define rezabuffer1 1024

#define rezabuffer2 1024

#define rezabuffer3 1024

#define rezabuffer4 1024

#define rezabuffer5 1024

#define rezabuffer6 1024

uint16_t rezareadadc(ADC_TypeDef* ADCx, uint8_t channel) {

    int i;

    ADC_RegularChannelConfig(ADCx, channel, 0, ADC_SampleTime_15Cycles);

    ADC_SoftwareStartConv(ADCx);

    

    /* Wait till done */

    while (ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET);

    

    /* Return result */

    return ADC_GetConversionValue(ADCx);

}

int rezafft(){

}

void rezapins()

{

      GPIO_InitTypeDef GPIO_InitStructure;

      GPIO_InitTypeDef gpioStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    gpioStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;

    gpioStructure.GPIO_Mode = GPIO_Mode_OUT;

    gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOD, &gpioStructure);

 

    GPIO_WriteBit(GPIOD, GPIO_Pin_12 | GPIO_Pin_13, Bit_RESET);

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1/* |GPIO_Pin_2 | GPIO_Pin_3

    | GPIO_Pin_5 | GPIO_Pin_6*/;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

 

void rezaTimer()

{

      TIM_TimeBaseInitTypeDef timerInitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);

 

    

    timerInitStructure.TIM_Prescaler = 84;

    timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

    timerInitStructure.TIM_Period = 9999;

    timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    timerInitStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM7, &timerInitStructure);

    TIM_Cmd(TIM7, ENABLE);

}

 int rezaintrrupt(){

 NVIC_InitTypeDef nvicStructure;

    nvicStructure.NVIC_IRQChannel = TIM7_IRQn;

    nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;

    nvicStructure.NVIC_IRQChannelSubPriority = 0;

    nvicStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&nvicStructure);

}

void rezaadcconfg(void)

{

    ADC_InitTypeDef       ADC_InitStructure;

  ADC_CommonInitTypeDef ADC_CommonInitStructure;

 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;

  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;

  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;

  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

  ADC_CommonInit(&ADC_CommonInitStructure);

 

  /* ADC1 Init ****************************************************************/

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_6b;

  ADC_InitStructure.ADC_ScanConvMode = ENABLE;

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

  ADC_InitStructure.ADC_NbrOfConversion = 1;

}

int main()

{

    int j=0;

    int VA[1024];

    int VB[1024];

    int VC[1024];

    int IA[1024];

    int IB[1024];

    int IC[1024];

    int IN[1024];

    rezapins();

     rezaTimer();

     rezaintrrupt();

     rezaadcconfg();

         while (j<=1024){

            

            if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

    {

        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

               VA[j]=rezareadadc(ADC1,0);         

               j++;

}             

}

}

1 REPLY 1
Posted on November 27, 2015 at 11:49

1) Has impact if you interleave multiple ADC units

2) You need to use DMA to collect your array of sample (one array 6x1024 elements), configure all 6 channels into the ADC, turn off continuous mode, and enable triggered mode off a timer, configure the timer with a 2 KHz time base. Use the DMA TC interrupt to indicate the array is full of samples, ideally double up the array, and use HT interrupt to trigger that it's completed the first half.

I've posted examples of how to do this on the forum, please review related posts.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..