Skip to main content
Visitor II
November 12, 2009
Question

TIM2 FWLIB Example

  • November 12, 2009
  • 10 replies
  • 2013 views
Posted on November 12, 2009 at 06:15

TIM2 FWLIB Example

    This topic has been closed for replies.

    10 replies

    frupiAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:05

    Hi to All

    Looking in the examples of FWlib from ST in TIM2 PWM frequency Configuration, I don't understand how those numbers come out.I've read the read me file in the folder but seems wrong to me. In particular I would like to produce a square waveform of frequency 125kHz and duty cycle of 50%. I'm using a STM8S103F 20pin QFN package. I was trying to start with the FWLIB from ST with timer2.

    Can some one advise me how to?

    Thank's for all the support!

    Franco

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

    Also have a look on this document:

    discrete.st.com/stonline/products/literature/an/14054.pdf

    timers of STM8S103 have more features of the other STM8S family timers, I think that on STM8S103 you have timer5 (see RM) instead of timer2, so timer5 has different register map of timer2.

    coluber

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

    Which compiler and version are you using?

    frupiAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:05

    Hi brazov2!

    Thank's for the reply. I just want to generate a square wave at 125kHz and duty cycle at 50%. Nothing more. I just want to use The timer2 output compare interrupt but doesn't seem so linear in using it.

    Thank's in advace

    frupi

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

    Hi All,

    The STM8S103F have 3 timers: TIM1, TIM2 and TIM5

    Franco,

    Yes, you can do this by using timer2

    Example: using the default clock 2MHz and channel1

    /* Time base configuration TIM2 Frequency = TIM2 counter clock/(TIM2_ARR + 1) --> TIM2_ARR =(TIM2 counter clock/TIM2 Frequency)-1

    =(2000000/125000)-1

    =15*/

    TIM2_TimeBaseInit(TIM2_PRESCALER_1, 15);

    /* TIM2 Channel1 duty cycle = [CCR1_Val/(TIM2_ARR + 1)] * 100 = 50%

    --> CCR1_Val = 0.5*(TIM2_ARR + 1)

    = 8

    */

    /* PWM1 Mode configuration: Channel1 */

    CCR1_Val = 8;

    TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);

    TIM2_OC1PreloadConfig(ENABLE);

    TIM2_ARRPreloadConfig(ENABLE);

    /* TIM2 enable counter */

    TIM2_Cmd(ENABLE);

    while(1);

    Regards

    mozra

    [ This message was edited by: mozra27 on 11-11-2009 09:57 ]

    frupiAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:05

    Thank's !!!

    Another question is: I'm using the internal HSI clock (16Mhz) and fmaster is 16Mhz(prescaler 2^0)is this the clock going in the TIM2 peripheral or not. I don't understand where the 2 Mhz come out, there is no reference in the manual about it.

    Ciao

    frupi

    frupiAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:05

    Hi mozra27

    Thank's for the suggestions!!! I will give it a try and come back to you with result. By the way here is the interrupt that I'm using:

    void TIM2_CAP_COM_IRQHandler (void)interrupt 14

    {

    if (TIM2_GetITStatus(TIM2_IT_CC1) != RESET)

    {

    TIM2_ClearITPendingBit(TIM2_IT_CC1);

    capture = TIM2_GetCapture1();

    TIM2_SetCompare1( (capture + (UINT16)CCR1_Val) );

    }

    Can you confirm if this is right!!

    Thank's in advance A drink is offered to you!! Cheers!

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

    Hi Mozra,

    are you sure timer2 of stm8s103 is the same of timer2 of stm8s207? If I look at the datasheet of the two products I can see different register mapping. What am I missing?

    coluber

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

    Ops, I made some confusion between devices, I tried mozra suggestion using cosmic compiler and it works fine :)

    coluber

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

    Hi,

    Frupi,

    The HSI is selected as default clock source;)

    Regarding the CLK block diagram: the fHSI is divided by the prescaler that can be configured through HSIDIV[1:0] bits in the CLK_CKDIVR register (the reset value for these bit is 1 so, it divide the fHSI/4 by default) for more details refer to reference manual.

    When you use the mode output compare PWM1 you don't need to configure the interrupt.

    Coluber,

    Yes, the timerx(x=1,2,3,4,5) when available on the product (refer to product datasheet) is the same as the described one in the STM8S microcontroller family reference manual.

    Regards

    mozra