cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 Timer Output Compare mode (OC mode)

srikanth
Associate III

hi sir,

I want to use timer channels to OC mode. In that same timer interval i need set time to channel 1 come 1 time and channel 2 comes 5 times.

example: In 1 milli second timer i need set & reset channel 1 for time and every 200 micro second set and reset the channel 2 (5 times in 1Millisecond) and i want start both channels at same time. Is this possible ?

if possible ,which OC mode is preferable for channel 1 and channel 2

4 REPLIES 4

In one timer, you have only one counter, which means that you can't output pulses with different frequency on different channels of the same timer.

Use two timers for that.

JW

Thank you sir for reply.

Singh.Harjit
Senior II

​Can you write our the exact sequence of what you want for a few cycles?

Bob S
Principal

@Community member​'s suggestion of using 2 timers is a cleaner solution, specially if you can choose 2 timers that share a common trigger source so there is no software delay between the 2 timers starting.

However, it is possible to do this using a single timer, though the code is much more involved. Configure the timer as a normal up counter with ARR=0xffff (or 0xffff ffff for a 32 bit timer). Channel 1 and 2 are configured as output compare "toggle" mode. Starting with timer not running, and timer's CNT == 0, Intiialize each channel's CCR to "1/2 of pulse period" of that channels signal, and enable the "output compare" interrupt for both channels. Start the timer. In the interrupt handler, check to see which channel caused the interrupt (might be both, so be ware), then update that channel's CCR = (old CCR value) + (1/2 pulse period). Continue as long as you need pulses.

This presumes that your CPU speed is high enough, and your output pulse rate low enough, that your interrupt routine can update the CCR registers before the timer's CNT reaches the next compare value. And like I said, not as clean/easy/clear as the two timer solution.