cancel
Showing results for 
Search instead for 
Did you mean: 

trigger adc after counter is reached

Posted on March 21, 2016 at 23:50

Hello there,

I was wondering either in STM32F4 it is possible, to get a similar functionality to C2000 one, that can trigger an ADC conversion after certain counter valueon a timer is reached, like here:

https://www.dropbox.com/s/d6lonep1m3i5x4d/Zrzut%20ekranu%202016-03-21%2023.48.06.png?dl=0

(point CA, middle).

In stm34f4 I am using center aligned pwm mode. I can trigger ADC each time timer starts counting up/down or both. But I need to trigger the adc when the counter is halfway down or up. Is there a way to do this?

I would appreciate all help.

#timer #adc #synchronization
26 REPLIES 26
Posted on March 22, 2016 at 17:29

Ill try to figure this out, maybe using an additional timer. Thank you.

Walid FTITI_O
Senior II
Posted on March 22, 2016 at 20:33

Hi Bremen,

Yes , you can do that by using the capture/compare register of that channel, and when there is a match with the counter value an update event is generated and your ADC is triggered.

(so if you want a trig in the halfway like you said, you should make TIMx_CCRx = 1/2 x TIMx_ARRx.

Note that in center aligned mode, if you select up/down counting you will have one trig per period and when you select both up and down counting you will have double trig per period.

-Hannibal-

Hello there,

I was wondering either in STM32F4 it is possible, to get a similar functionality to C2000 one, that can trigger an ADC conversion after certain counter valueon a timer is reached, like here:

https://www.dropbox.com/s/d6lonep1m3i5x4d/Zrzut%20ekranu%202016-03-21%2023.48.06.png?dl=0

(point CA, middle).

In stm34f4 I am using center aligned pwm mode. I can trigger ADC each time timer starts counting up/down or both. But I need to trigger the adc when the counter is halfway down or up. Is there a way to do this?

I would appreciate all help.
Posted on March 22, 2016 at 20:46

But if i change the CCRX of that channel, the duty will change. I dont want to trigger the ADC in the middle of the period (ARR), but in the middle of the PWM pulse. So the would it be possible for counter to work in the way, that OC interrupt would be triggered in the middle of the PWM pulse and not on the beggining or on the end of it?

re.wolff9
Senior
Posted on March 22, 2016 at 22:44

[ posting seems to work today. ]

The normal way to do it is to use CCR1-3 for the PWM outputs and then use CCR4 to trigger the ADC. 

Also, don't you want to trigger the ADC just BEFORE the halfway point so that the sampling happens ''around'' the halfway point? Or, if you are trying to avoid the switching noise of the edges, maybe you want to sample near the end of the PWM period, but not too late? Anyway, using an extra channel of one of the bigger timers will give you all that flexibility. 

Posted on March 22, 2016 at 22:58

I wish the forums were moved to phpbb or another modern platform.

About the topic- I really need the interrupt exacly in the middle of the pwm pulse. Not as in the middle of whole period- in the middle of pulse. So if pwm is 50% i need interrupts at 25% and 75%. The switching happens at the edges so in the middle I am very safe. Like you said, i could use a little erlier interrupt to give time for the ADC conversion. But I believe in sample and hold that works ok. maybe im wrong and then I would have to do it erlier like you said.

Unfortunetally, I am using all available channels on TIM1 and TIM8 so I cant use the 4th channel. I think I got 2 channels left on timer 5 so maybe ill use them.

Thank you for the answers. I only thought this could be done automatically, in hardware.
Posted on March 23, 2016 at 00:08

It is modern, it is just that Microsoft seems not to have learned any lessons from the past.

If you can't pick a timer/channel to pull the appropriate phase, then perhaps you can get the ADC to run at a higher rate and fill a 100 point (or whatever) buffer and pull the value you want from that.

You could also have another timer slaved from the primaries, and put the phase trigger in that.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 23, 2016 at 14:43

But can a channel from another timer be slaved to this channel? So lets say TIM1 OC1 triggers interrupt and it makes TIM5 OC3 channel start counting?

Walid FTITI_O
Senior II
Posted on March 23, 2016 at 15:25

Hi Bremen,

If I well understand your need, you want to trig the ADC in the middle of your Timer pulse.

Taking into account that you are using center aligned mode, which alternate between counting up (0 -> ARR) and counting down (ARR-> 0) to make a period. The pulse is the double match between CCRX and ARR; the rising edge is a match in counting-up part and the falling edge of pulse from the counting-down part. (I have drawn a diagram that explain that more [see attached] )

So, in this case, once the counter reach the ARR, you trig the ADC. To do that you should interconnect the TRGO signal of your timer to the ADCx that you want to use for conversion using EXTSEL[3:0] and EXTSEL[3:0] in ADC_CR2 register.

(See table External trigger for injected channels in your STM32F4 device reference manual)

In Timer side, you configure your Timer master as follow: select TRGO_update as master output trigger.

In ADC side, you select “ExternalTrigConv�? as TIMx_TRGO signal of your timer.

-Hannibal-

________________

Attachments :

Timer_s_center_aligned_mode.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0Yf&d=%2Fa%2F0X0000000bbe%2FKU62B2QbgF8E2y3ZFLPxnp6w_FbdENK9pFfLg08Yxqg&asPdf=false
Posted on March 23, 2016 at 15:36

Thank you for your explanation, Ill try to acomplish this first thing in the morning.

Posted on March 23, 2016 at 16:47

But when I think of it now, I am not sure your image is correct. In center aligned mode 3 I can trigger interrupts (Capture compare event X) only when counter starts counting down or up, so accroding to your it would be where the dots are:

https://dl.dropboxusercontent.com/u/14154631/counter_ccr.png

There are 4 pwm channels in TIM1. What source should exacly used for adc trigger? Should it be ''Trigger out event''? If yes then I cant use ADC1 for TIM1, only for TIM8, right?