cancel
Showing results for 
Search instead for 
Did you mean: 

PWM capture with TIM1?

georg23
Associate II
Posted on March 14, 2010 at 23:01

PWM capture with TIM1?

12 REPLIES 12
georg23
Associate II
Posted on May 17, 2011 at 15:07

hi Lüfter,

sorry, I thought you were a native speaker. But no problem, here's the description of my test setup resp. my problem:

  • I use the STM8 Discovery-board. For access to pin PC1 I modified solder bridge SB3 as described in the board manual
  • the STM8 runs at 16MHz from an external resonator
  • the PWM signal is generated at TIM2_CC2 (= PD3 = CN4, P8), and measured via TIM1_CC1 (= PC1 = CN2, P2)
  • the PWM signal is (1kHz, 30%) constant during the test, and pins PD3 and PC1 are connected witha short cable
  • I have asserted the generated PWM using an oscilloscope (see attachment). It fits almost incredibly well...
  • for a 1kHz PWM signal and 16MHz timer clock (PRE=0) I expect a capture-value of 16000 (as described by you) for the full period, resp. 4800(=0.3*16000) for the high duration
  • the first measurement (immediately after the start of PWM generation) yields a value ''close to that'', specifically CC1=15945 and CC2=4744
  • all  further measurements (which take place every 5ms) yield values of CC1~13900 and CC2~2690
  • if I put the ''sleep_ms(5)'' before the measurement of the PWM signal, also the 1st measurement yields CC1~13900 and CC2~2690
  • if I apply a constant but uncorrelated(!) PWM signal to TIM1_CC1 (from a 2nd discovery board) I get completely different capture values

Specifically the 4 last points make me believe that TIM1 starts before the first rising edge of the PWM signal. I thought I had configured it to be triggered via a rising edge via TIM1_SMCR. Butt obviously I do something wrong... :(  Attached please find a yet simplified project. The relevant lines are in ''hw_layer.c'' lines 124ff. 

I very much appreciate any help on this issue. And thanks a lot in advance for your effort!

Regards, Georg

PS: attached are a screenshot of the PWM signal and a simple STVD/Cosmic project
lowpowermcu
Associate II
Posted on May 17, 2011 at 15:07

Hi georg,

Try adding this line after while (!TIM1_SR1_CC1IF);

while (!TIM1_SR1_CC2IF);  // wait until capture event

I think that you should wait for two events: first the CC2 event to read the duty cycle and the second one is to read the period.

I hope this helps you otherwise I should run your code to find out the issue.

Kep me informed.

Mit besten grüssen,

MCU Lüfter

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:07

Hi georg,

Add TIM1_SR2 = 0x00; after TIM1_SR1 = 0x00; that could help !

Lüfter