Showing results for 
Search instead for 
Did you mean: 

Cannot Update TIM CCR Register in Timer Master/Slave Mode for Phase Shift PWM

Associate II


I am quite new to STM32, so I would highly appreciate your help.

I am using an STM32F407G-DISC1 board.

I am trying to generale phase shifted complementary PWM for PFSB control. I am using TIM1 CH1/1N to generate one pair of signals, and TIM8 CH2/2N to generate the other. To obtain the phase shift, I am using Output Compare No Output Mode at TIM1 CH2, which generates an interrupt that controls the counter of TIM8 via ITR0. When I modify the initial pulse configuration, the phase shift is achieved correctly (oscilloscope picture, yellow trace for TIM1 CH1 and blue for TIM8 CH2). However, when I try to vary TIM1->CCR2 in order to change the pulse value and essentially change the phase shift, nothing happens. Below you can see my configuration and change of CCR2 value:


Below I attach the oscilloscope traces and screenshots of TIM1 configuration. Thank you in advance!


> To obtain the phase shift, I am using Output Compare No Output Mode at TIM1 CH2, which generates an interrupt that controls the counter of TIM8 via ITR0.



PS. Don't post code as screenshot; use the </> icon at top of editor pane to post code.

PS2. Use TIM1_CH2 output to visualize first-hand the effect of changing TIM1_CCR2.


Thanks for replying! Below I describe the configuration of the key features for that function:


Channel 2 set to Output Compare No Output. Trigger event is set by OC2REF. Channel 2 mode set to active level on match, to generate a trigger event for TIM8. The counter counts up to 1119, so that value varies between 0-1119.


Reference Manual for the board states that TIM8 can be controlled as a slave by TIM1 via internal trigger ITR0:


So, in the TIM8 config I set the trigger source as ITR0:


So, in short:
1. Counter of TIM1 counts up and generates complementary PWM on CH1/1N.
2. When it reaches the pulse value on CH2, it generates a trigger, which enables the counter of TIM8.
3. TIM8 generates complementary PWM on CH2/2N.
4. By varying the pulse value of TIM1 CH2 Output Compare, i.e. CCR2 (as per my understanding), I should be able to vary the offset between TIM1 PWM and TIM 8 PWM. And it works with the initial setup, i.e. when I change the value to e.g. 0/100/500, the offset between the PWM signals is correct. However, I cannot vary this offset dynamically, during my code execution, only during configuration.

Does that clarify things a bit?

Associate II

Oh and apologies, just noticed your PS! Will change Channel 2 to Compare Output Ch2.

OK so contrary to what you've said:

> To obtain the phase shift, I am using Output Compare No Output Mode at TIM1 CH2, which generates an interrupt that controls the counter of TIM8 via ITR0.

you are *not* using interrupt to control TIM8_CNT.

However, the slave-mode controller in TIM8 set to *trigger* mode does not control TIM8_CNT either.

Trigger mode does nothing else just sets TIM8_CR1.CEN. It means, that if you set up both timers but don't enable TIM8 (i.e. you don't set TIM8_CR1.CEN - Cube may do things in this way when properly clicked), the first TRGO from TIM1_CH2 sets TIM8_CR1.CEN thus starts TIM8. That's then the source of your "phase shift".

One easy and quick change is to set TIM8 slave-mode controller to Reset mode rather than Trigger.


Associate II

Thanks again! I think I understand your explanation regarding TIM8_CR1.CEN - essentially TIM8 counter starts counting after the first trigger. I might have been changing TIM1->CCR2, but it didn't matter as TIM8 was not restarting. 

Now, I understand that the reset mode should work as follows - when the counter on TIM1 CH2 reaches the pulse value, TIM8 will reset. I changed the mode, but it doesn't work at all. The shift is constant (despite changing the initialized pulse value) and I cannot change it during execution.

Associate II

I did some reading in the reference manual and I think I managed to make it work! Instead of changing CCR value, I change the CNT value of TIM8 and drive it in reset mode, triggered when TIM1 overflows. This synchronizes the the timers and allows to apply the phase shift. Think this should solve it. Thank you so much for your help!

Read out and check/post content of TIM1 and TIM8 registers.

> I cannot change it during execution

What do you mean by this? You cannot change TIM1_CCR2 content? How do you do that?

A simple and effective method of testing these features is in debugger: stop execution after the initial setup, and then in the registers view simply change the registers' content directly in the debugger, while observing on pins the effect of these changes.