cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with STM32F072 timer 3 output compare unit

lou
Associate II
Posted on March 03, 2015 at 19:06

Hello. Thanks for helping.

  I have a need to generate fairly precise output pulses at

somewhat irregular intervals.  I have timer 3, Ch1 set up for

input capture, and it works great. I have the timer set to roll over

at 0xFFFF -->0000 and it works fine. I can read the input

captures all day long, they work great. (I am measuring a 

frequency on this Chanel)  On Ch2, I need to drive a solenoid

that will have a varying pulse length (This is a paint-injector

that will be used to make a ''precision'' length strip of

paint on a roll of paper going by, (Roll speed is the frequency

input)

 

   The need I have is that on occasion I want CH2 to be used

to output a pulse. My idea is, when this pulse is needed I will

read the value of timer3, add my pulse length, and put that value

into Timer 3, Ch 2 output compare.  I want the OC2 pin to

go high for XXX miliseconds, and then be set low by the

output compare reaching its targeted value. I need to do this

many times per minute. I have this working, but it

only works ONE time, I cannot seem to re-arm the output

compare unit.  What is the ''trick'' to setting output compare 

pin high again?

   Here is how I set up the output compare unit..

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OCInitStructure.TIM_Pulse = 5000;  //default pulse length value

        TIM_OC2Init(TIM3, &TIM_OCInitStructure);

      Then, in my ''paint'' function, I have this code that

I WANT to have put OC2 high, but it never goes high again..

void start_paint_injector()

{..

 .. I_paint_lenght_count = 500; // test code!

   I16_present_capture_count = TIM_GetCapture1(TIM3);

   TIM_SetCompare2(TIM3,I16_present_capture_count+ I_paint_lenght_count);

 }

   What is the trick to getting OCx to ''go high'' again?

   Thank you!!!!

   

  

/* TIM enable counter */

TIM_Cmd(TIM3, ENABLE);

#stm32f072-timer
6 REPLIES 6
Posted on March 03, 2015 at 19:43

Define precision. A couple of microseconds?

Toggle mode would allow you to alternate the state. But remember you'd still need to keep advancing the Compare Register. ie TIMx->CCRx += 500;

The timer only has one main counting element.

Could you use the CCRx values to interrupt and turn OFF the GPIO.

ie

 TIM3->CCR1 = TIM3->CNT + 500;

 GPIO = ON;

at CC1 IRQ

  GPIO = OFF;

Or perhaps slave another timer, or use one in On-Shot mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lou
Associate II
Posted on March 03, 2015 at 20:02

I cant use toggle mod b/c I only want one pulse.

What I really want is a re-triggerable one shot..

Is that possible ? I didn't see that in the documentation of the timer..

  Toggle mode is no good b/c I don't want to have

to poll the timer to dis-able it after the first pulse.

Posted on March 03, 2015 at 21:19

What I really want is a re-triggerable one shot..

 

Is that possible ? I didn't see that in the documentation of the timer.

You'll need another timer to do the one-shot, and you'll trigger than from your input capture on this current one, or an output compare with a phase setting wrt to other sensor.

You'd have to check the reference manual to figure out the TIMx vs TIM3/ITRx master/slave trigger relationship.

So you'd set the Trigger Output (TRGO) of TIM3, and the Internal Trigger (ITR) of the other.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lou
Associate II
Posted on March 03, 2015 at 21:41

So, there is no way to set an output capture channel to 

''go high. Stay high until this many counts have gone by,

and then go low''   There is almost an example of this in

the STM standard peripherals library..

   In other words, I want the output block to behave like  a

set-reset flip flop (like they are on almost any other

micro controller)  I want the act of writing the output

compare match value to SET the output, and then

I want the match to re-set it to 0.  like they do in 

the PIC's AVR's.   I don't even care if i have to write

some other register to ''set'' the output block, i can 

deal with that, as the write instruction is going to be

sub-microsecond, I just need to be able to do it.

   Is there any good, detailed logic block diagrams of

the inner workings of the output block? The best i have seen

is in the STM32 reference manual, but it really has no detail

as to how it really works with the configuration bits.

  Sounds brutally simple and obvious to me as something

that is a basic thing this chip should be able to do - but

the module seems so complex and, quite frankly, poorly

documented, that perhaps this is impossible?

   There has GOT to be an easy way to do this? no?

It is probably that I just do not understand what mode 

to select the output compare block to be in.

 

Posted on March 03, 2015 at 23:24

Ok, so let me re-iterate.

There is a single counting element (ignoring timebase prescaler, input prescaler, and repetition count) per timer peripheral.

If you are rolling a timer 0 thru 65535 to measure delta ticks from your tachometer output of your paper roller, you are using the only ''counter'' in the timer.

You can't zero basis a PWM pulse without resetting the counter, or use it for a One-Shot, and thus losing the tachometer function.

I mentioned Toggle mode, you dismissed that, but it's quite effective at making variable length pulse, and ones at various phases to the rotation, using a singular timer. I mentioned driving a GPIO and have a interrupt catch the rotating counter further down the line. A lot of the motor control, multi-phase PWM modes require you to baby sit the timer, again something you don't want to do.

I mentioned that you could have a slave timer do a one-shot output triggered by either the tachometer input, or a phase relationship to the timer.

There are a great number of aspects of the STM32 design I find a bit retarded, the clocks and timers being at the top of my list.

The timer documentation is awkward, I've worked with worse, but most all the salient detail is in there.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lou
Associate II
Posted on March 04, 2015 at 05:49

It turns out that you are dead wrong.

I have it working fine, I just needed to find the

bits to jiggle to get the output compare to re-assert

the output flip flop.

  So, to be very clear, with one timer,  (timer 3)

I am able to measure RPM's and generate not just

one, but 3 outputs of varying pulse width at my

whim.

  I guess the lesson here is never assume the 

''experts'' know it all - It is a complicated and

somewhat poorly documented chip.

   thanks for the help.