cancel
Showing results for 
Search instead for 
Did you mean: 

Timer4 trigger for ADC

stuart239955
Associate II
Posted on June 15, 2011 at 10:15

Hi,

Using STM32F103.

I want to use the external trigger mode of the ADC to start a regular group of channels (in dual, simultaneous mode) via the TIM4 CC4 Event.

The examples that ST provide illustrate using TIM1 CC1 event using a pwm OUTPUT channel to trigger the ADC.

I wish to trigger my regular channels via INPUT capture on TIM4 channel4. Is this possible?

The documentation seems to suggest so.

I have tried using a basic example, but to no avail. The TIMER4 input capture event occurs, but doesn't trigger the ADC. I can manually trigger the ADC via software, so am not sure what could be wrong.

Is there some trick that I am missing here? Is there an example someone could send me?

Also note that I am doing a full remapping TIMER 4 pins.

I'm using my own code - not using the library, so have not got any code to fully illustrate this yet, sorry.

Any ideas would be most welcome.

Thanks,

#adc-trig-in-dual-mode #poor-quality-of-documentation
24 REPLIES 24
Posted on June 15, 2011 at 16:44

TIM4_CC4 should be viable (TIMER 4 CHANNEL 4)

You'll need to program ADC1->CR2, with EXTSEL[2:0] = 5   (101)

  /* TIM4 configuration ------------------------------------------------------*/

  /* Time Base configuration */

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Period = 0xFF;

  TIM_TimeBaseStructure.TIM_Prescaler = 0x4;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* TIM4 channel4 configuration in PWM mode */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0x7F;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OC4Init(TIM4, &TIM_OCInitStructure);

..

  /* ADC1 configuration ------------------------------------------------------*/

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

  ADC_InitStructure.ADC_ScanConvMode = DISABLE;

  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_NbrOfChannel = 1;

  ADC_Init(ADC1, &ADC_InitStructure);

..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stuart239955
Associate II
Posted on June 15, 2011 at 17:06

Hi Clive,

Thanks for that. I've tried that and it works.

However, the lines:

 /* TIM4 channel4 configuration in PWM mode */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0x7F;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OC4Init(TIM4, &TIM_OCInitStructure);

set the timer into output compare mode.

According to the ADC section in the reference manual, the event from the timer Capture/Compare triggers the ADC - which works in the example you posted.

However, in input capture mode (which I need), the TIM4_CC4 event caused doesn't trigger the ADC. The input capture event definitely happens, because the timer count is latched.

I think the timer can only externally trigger the ADC in output compare mode, not input capture.

I am changing my hardware to trigger the ADC via EXTernal Interrupt 11, rather than TIM4_CC4. This should solve my problem - I hope!!

Thanks,

Stuart

Posted on June 15, 2011 at 19:44

Without spending hours digging through the timer documentation/diagrams, I would imagine the following would a) get a time stamp in CCR4, b) initiate an ADC conversion, with a rising edge on the TIM4_CC4 pin.

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_ICInit(TIM4, &TIM_ICInitStructure);

  /* TIM enable counter */

  TIM_Cmd(TIM4, ENABLE);

..

I'm not sure if the CC trigger on the ADC has an pulse width requirements, or what's generated in this case.

Now that would also assume you have the IO pin configured, and remapped. GPIOD and AFIO clocks enabled too.

  /* TIM4 Full remapping pins */

  GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);

  /* TIM4 channel 4 pin (PD.15) configuration - remapped */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sanjib
Associate III
Posted on February 10, 2014 at 05:43

Hi stuart , I am facing the same issue. Can you help me if you got the solution

Posted on February 10, 2014 at 09:04

I wouldn't be surprised if the description of ADC triggers in RM0008 is wrong, and in fact it won't act upon a TIMx_CCy event as depicted on the timers' block diagram (i.e. which is generated from both capture and compare), rather, on the TIMx_OCy signal.

If this is the case, you still should be able to trigger an injected conversion on ADC1 and ADC2 from the TIM4_TRGO signal (ADC_CR2.JEXTSEL = 101), which in turn can be tied to TIM4_CH1 capture, if TIM4_CR2.MMS is set to 011 (Compare Pulse).

I did not try any of these, though; this all is based entirely on manual reading.

JW

sanjib
Associate III
Posted on February 10, 2014 at 10:57

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6Xn&d=%2Fa%2F0X0000000bqJ%2Fg32_KwQhzZKdEhBDaaufdOvwbGtXgmrdIWMUfRUDmKY&asPdf=false
ivan23
Associate II
Posted on February 10, 2014 at 13:30

Sanjib, you are hereby awarded the Leon Tolstoy prize for the longest post. Let us say nothing of its contents.

Posted on February 10, 2014 at 17:18

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6ds&d=%2Fa%2F0X0000000bsK%2F.NKegmQV8bh.e0AWCHdvetzQM8F2h5e.Vi1VGBcOkHk&asPdf=false
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 10, 2014 at 17:31

TIM_TRGOSource_OC1

Now not only is ''Compare Pulse'' a confusing name for TRGO being driven from CC1IF (i.e. both capture and compare), it's even more confusing to name the related constant ''_OC1''. Sorry, but this is why when it comes to the ''library'', I can't think of any other adjective than ''idiotic''. JW