AnsweredAssumed Answered

ADC is triggered by timer

Question asked by mirshahi.shiva on Mar 3, 2014
Latest reply on Mar 3, 2014 by Clive One
 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;
}


Outcomes