Skip to main content
Visitor II
March 14, 2010
Question

PWM capture with TIM1?

  • March 14, 2010
  • 12 replies
  • 3033 views
Posted on March 14, 2010 at 23:01

PWM capture with TIM1?

    This topic has been closed for replies.

    12 replies

    georg23Author
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    hi,

    thanks for the fast response! I have now copied the very procedure described in the STM8 reference manual - still without success. Guess I have to contact technical support..

    Regards, Georg

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    Hi,

    I remember that I used this config and it worked fine. Perhaps you missed this step

    ''Select the valid trigger input: write the TS bits to ‘101’ in the TIM1_SMCR register (TI1FP1 selected).''

    It is indicated in the reference manual !

    regards,

    mcu fan

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    Hi georg,

    you can send me your code perhaps I can help you.

    Mit besten Grüßen,

    low power fan

    georg23Author
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    hi,

    thanks a lot in advance! Of course I can provide you with a demo project for the Discovery Board (see attachments to this post). For your help and effort thanks a lot in advance!

    Regards,

    Georg

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    Hi georg,

    After a first look. I suggest to change

        while (!TIM1_SR1_UIF);

    by

        while (!TIM1_SR1_CC1IF);

    because it is at capture event the counter value will be loaded in capture register.

    Hope this helps.

    Tell if it is OK

    Mit besten grüssen,

    MCU Lüfter

    georg23Author
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    hi Lüfter,

    funny discussing in english when both our native language most likely is german...? Anyway, I've tried your proposal but I still get a funny result (see attachment). The columns are (from left to right): input frequency and duty cycle (both asserted using an oscilloscope), TIM1_CC1 and TIM1_CC2 capture values, and the calculated frequency (24MHz/2/CC11) and duty cycle (CC2/CC1). 

    To me this seems like CC1 & CC2 do capture the edges, but for some reason the timer doesn't start correctly...? This is also supported by the observation that the 1st measurement is only slightly off (starts immediately after start of PWM generation), whereas all the other measurements are more off (+ some ms delay). Any idea???  And again, thanks in advance for any help!

    Regards, Georg

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    Hi georg,

    You put 1KHz and you find in CCR1 12000. Das kommt nicht !

    If your timer is clocked by 16 MHz so you should have 16000 in CCR1 register +/- error. What is your clock divider, timer clock 16 MHz, 8MHz or ...?

    I suuggest to make an update event after each config of timer so the new one (prescaler, ARR...) are taken into account immediately (you can conclude from the reference manual that the timer needs update event to take into consideration the new config)

    the update is done as following: TIM1->EGR |= 0x01;

    Mit besten grüssen,

    MCU Lüfter

    georg23Author
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    Hallo Lüfter,

    der Einfachheit halber antworte ich mal in Deutsch (alternativ könntest Du mir mal Deine e-Mail an ''georg - at - cream-tea.de'' schicken, dann machen wir per PM weiter). Aber hier zunächst der Versuch bzw. mein Problem:

    • ich benutze das STM8 Discovery-Board. Für direkten Zugriff auf PC1 habe ich Lötbrücke SB3 umgelötet (wie im Board Manual beschrieben)
    • der STM8 läuft mit 16MHz externem Takt
    • die PWM wird an TIM2_CC2 (= PD3 = CN4, P8) generiert und an TIM1_CC1 (= PC1 = CN2, P2) zurückgelesen.
    • die PWM ist über die gesamte Dauer konstant (1kHz, 30%) und wird über ein kurzes Kabel von PD3 -> PC1 gegeben
    • die Korrektheit der erzeugten PWM habe ich mit einem Oszi kontrolliert (s. angehängtes Bild). Sie stimmt geradezu unheimlich gut...
    • bei 1kHz und 16MHz (PRE=0) erwarte ich einen Capture-Wert von 16000(=16e6/1e3) für die volle Periode bzw. 4800(=0,3*16000) für die high-Dauer
    • die erste Messung (direkt nach Start der PWM Generierung) liefert einen Wert ''in der Nähe'', konkret CC1=15945 und CC2=4744
    • alle weiteren Messungen (finden nur alle 5ms statt, siehe main loop) liefern hingegen Werte um die CC1=13900 und CC2=2690
    • wenn ich im Main-Loop das sleep_ms(5) VOR die erste Messung der PWM setze, erhalte ich auch in der ersten Messung Werte um die CC1=13900 und CC2=2690
    • wenn ich von aussen ein konstantes aber unkorreliertes(!) PWM Signal anlege, (z.B. von einem anderen Discovery Board) bekomme ich ganz andere Werte

    Speziell die letzten 4 Punkte liessen sich einfach erklären, wenn TIM1 noch vor der ersten steigenden Flanke losläuft, obwohl ich dachte, ich habe ihn so konfiguriert, dass er genau das macht (TIM1_SMCR). Aber offenbar mache ich da was falsch, aber ich komme einfach nicht dahinter... :( Anbei noch ein vereinfachtes Projekt. Die interessante Routine ist in ''hw_layer.c'' Z.124ff.

    Für jedwede Hilfe bin ich dankbar und für Deine Mühe vielen Dank im voraus!

    Gruss, Georg

    PS: Anbei ein Screenshot der PWM sowie das Projekt
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:07

    Hi georg,

    Entshuldigung, aber ich verstehe ein bischen Deutsch

    deshalb schlage ich vor, um Ihre Nachricht auf Englisch übersetzen

    .

    Danke shön,

    MCU Lüfter

    georg23Author
    Visitor II
    May 17, 2011
    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