AnsweredAssumed Answered

using TIM1 TRGO to start adc conversion

Question asked by inler on Nov 22, 2011
Latest reply on Mar 13, 2012 by 31696
hello, I'm trying to trigger the ADC1 with the TRGO signal of TIM1, here is the code that I,m using to configure the timer:

TIM1_DeInit();
TIM1_TimeBaseInit(128, TIM1_CounterMode_Up, 62500, 0);
TIM1_SelectOutputTrigger(TIM1_TRGOSource_Update);
TIM1_Cmd(ENABLE);

while for configuring  the ADC1 and getting the conversion result I'm using (adapted from STM8L-discovery firmware):

u16 ADC_CH3_TRIG(void)
{
    uint8_t i;
    uint16_t res;

/* Enable ADC clock */
  CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

/* de-initialize ADC */
  ADC_DeInit(ADC1);

/*ADC configuration
  ADC configured as follow:
  - Channel 3
  - Mode = Single ConversionMode(ContinuousConvMode disabled)
  - Resolution = 12Bit
  - Prescaler = /1
  - sampling time 9 */
 
  ADC_Cmd(ADC1, ENABLE);    
  ADC_Init(ADC1, ADC_ConversionMode_Single,
  ADC_Resolution_12Bit, ADC_Prescaler_1);
 
ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels,              ADC_SamplingTime_9Cycles);
ADC_ExternalTrigConfig(ADC1,
ADC_ExtEventSelection_Trigger2,
ADC_ExtTRGSensitivity_Rising);
ADC_ChannelCmd(ADC1, ADC_Channel_3, ENABLE);
  delay_10us(3);

/* initialize result */
  res = 0;
  for(i=8; i>0; i--)
  {
/* start ADC convertion by software */
    //ADC_SoftwareStartConv(ADC1);
/* wait until end-of-covertion */
    while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 );
/* read ADC convertion result */
    res += ADC_GetConversionValue(ADC1);
  }
    
/* de-initialize ADC */
  //ADC_VrefintCmd(DISABLE);

  ADC_DeInit(ADC1);
 
 /* disable SchmittTrigger for ADC_Channel_24, to save power */
  //ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE);
    
  CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE);
  ADC_ChannelCmd(ADC1, ADC_Channel_3, DISABLE);
    
  return (res>>3);
}

The program seems to hang up at the while loop waiting for EOC signal... what am I doing wrong? Is there an application note that I can use for ADC triggering with a timer?

Outcomes