cancel
Showing results for 
Search instead for 
Did you mean: 

ADC is triggered by timer

shiva
Associate
Posted on March 03, 2014 at 11:28

 Helllooooooooo,

I want to use the configured timer as a trigger for the A/D converter. I have to set up an interrupt service routine (that will be called when new data is ready to be read) to read the data and fill an array variable with the values. When the array is full, deactivate the conversions. The timer works properly but the ADC does not convert anything. I am very new in this chip. Could you please guide me?

 /* ///Timer setup./// */

static void timer2_setup ( void ) 

{

  /* Enable the TIMER2 interrupt. */

  nvic_enable_irq(NVIC_TIM2_IRQ);

  nvic_set_priority(NVIC_TIM2_IRQ,1);

  /* Set STM32 to 48 MHz. */

  rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_48MHZ]);

  /* Set timer start value. */

  TIM_CNT(TIM2) = 1;

    /* Set timer prescaler. */

  TIM_PSC(TIM2) = 4000;  

   /* End timer value. */

  TIM_ARR(TIM2) = 12000;  

  /* Update interrupt enable. */

  TIM_DIER(TIM2) |= TIM_DIER_UIE;

  /* Start timer */

  TIM_CR1(TIM2) |= TIM_CR1_CEN;

  return;

}

  /* ///Timer interrupt./// */

void tim2_isr(void)

{

  /*Clear a Status Flag. */

  timer_clear_flag(TIM2, TIM_SR_UIF);

  /* Blink the LED (PD14) on the board. */

  gpio_toggle(GPIOD, GPIO12);

  /* unsigned 16-bit integer. */

  uint8_t channel_array[16];

  channel_array[0] = 1;

  /* ADC Set a Regular Channel Conversion Sequence. */

  adc_set_regular_sequence(ADC1, 1, channel_array);

  /* ADC Software Triggered Conversion on Regular Channels. */

  adc_start_conversion_regular(ADC1);

  /* ADC Read from the Regular Conversion Result Register. */

  uint16_t reg16 = adc_read_regular(ADC1);

  return;

}

 /* ///ADC/// */

static void adc_setup(void)

{

  adc_off(ADC1);

  

  adc_disable_scan_mode(ADC1);

  /* Sample Time. */

  adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_3CYC);

  /*is powered up. */

  adc_power_on(ADC1);

   /* Enable the ADC interrupt. */

  nvic_enable_irq(NVIC_ADC_IRQ);

  nvic_set_priority(NVIC_ADC_IRQ,1);

  /* enables an external trigger for set of defined regular channels,Timer 2 TRGO Event,ADC Trigger Polarity. */

  adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_TIM2_TRGO, ADC_CR2_EXTEN_RISING_EDGE );

  return;

}

static void dac_setup(void)

{

  dac_disable(CHANNEL_2);

  dac_disable_waveform_generation(CHANNEL_2);

  dac_enable(CHANNEL_2);

  /* Trigger Source,Timer 2 TRGO event. */

  dac_set_trigger_source(DAC_CR_TSEL2_SW);

  return;

}

void adc_isr(void)

{

  /* Loads the appropriate digital to analog converter data register. */

  dac_load_data_buffer_single(reg16, RIGHT12, CHANNEL_2);

  dac_software_trigger(CHANNEL_2);

  int j = 0;

  printf(''tick: %d:  adc1=%d\n'',j++, reg16);

  return;

}

int main(void)

{

  

  /*Enable interrupts. */

    cm_enable_interrupts();                                                                                             

    clock_setup();

    gpio_setup();

    adc_setup();

    dac_setup();

    timer2_setup(); 

    

    

  while (1)

  {

    __asm__(''NOP'');

  }

  return 0;

}

1 REPLY 1
Posted on March 03, 2014 at 16:01

I don't know what part you're using, or the library, but it all looks rather tortured.

You'd probably want to tell the ADC you're going to be triggering, and set the TIM trigger to be something (Update?).

You don't want to be interrupting on the TIM IRQ, and reconfiguring/starting the ADC, this can be done by correctly configuring the TIM/ADC and then interrupting on the ADC's EOC, or using DMA.

Using C++ syntax probably won't help you, and allocating buffers as local/auto variable also won't help, especially if you try using DMA.

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/ADC%20trigger%20on%20timer%20update&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=140

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