Skip to main content
Visitor II
August 22, 2006
Question

counter with timer

  • August 22, 2006
  • 4 replies
  • 833 views
Posted on August 22, 2006 at 14:05

counter with timer

    This topic has been closed for replies.

    4 replies

    jorge2399Author
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:31

    hi all !

    i would like to know how to implement a 16bit counter with

    the st libraries. in fact, in functions, there's no timer mode as counter possible (just compare, pwm, etc, see 91x_tim.h ) ??

    Have you any example ?

    thx

    Regards

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:31

    hi,

    I made the timer counter in this way:

    I omit the SCU init for thi peripheral...

    ...

    TIM_StructInit(&TIM_InitStructure);

    TIM_InitStructure.TIM_Mode = TIM_PWM;

    TIM_InitStructure.TIM_OC1_Modes = TIM_TIMING;

    TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB;

    TIM_InitStructure.TIM_Prescaler = 0x00;

    TIM_InitStructure.TIM_Pulse_Length_1 = 0x0030;

    TIM_InitStructure.TIM_Full_Period = 0x8000;

    /* Initialize the Timer 2 */

    TIM_Init(TIM3, &TIM_InitStructure);

    /* Enable the Timer Overflow interrupt */

    TIM_ITConfig(TIM3, TIM_IT_OC2, ENABLE);

    /* Enable and configure the priority of the USB_LP IRQ Channel*/

    VIC_Config(TIM3_ITLine, VIC_IRQ, 0);

    VIC_ITCmd(TIM3_ITLine, ENABLE);

    ....

    // irq handler

    void Timer3_irq()

    {

    .. your code ..

    /*Clear the TIM OC1 flag */

    TIM_ClearFlag(TIM3, TIM_FLAG_OC2);

    }

    #define TIMER_TICKS(nsec) ( ((PERIPHERAL_CLOCK / 1000000) * nsec) / 1000 )

    // to set the timer period in ticks

    inline void SetTimerPeriod(u32 per)

    {

    static unsigned int old_per=0;

    if (old_per == per)

    {

    return;

    }

    old_per = per;

    s_u32 *p = (s_u32*)&per;

    if (p->h)

    {

    TIM_PrescalerConfig(TIM3, p->h & 0x00FF);

    per /= p->h + 1;

    }

    else

    {

    TIM_PrescalerConfig(TIM3, p->h & 0x00FF);

    }

    TIM_SetPulse(TIM3, TIM_OC2_Channel, per);

    }

    jorge2399Author
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:31

    that's works well !! Thanx stevejamal :D

    I have a little problem :when I want to change TIM_CLK_APB in order to

    increase my frequecy of timer, the timer is blocked (no interruptions) ?

    (with oscillator of 25mhz). Can't go over 32khz ! !

    So,What Is the a limitation in frequency for timers?

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:31

    I don't understand exactly what are you doing, sorry :)

    For what I read in the ref. manual you can chose from an external

    clock or a Peripheral clock divided by prescaler..

    Check the setting of APBDIV in the SCU_CLKCTRL.

    I'm working with a Peripheral clock of 96MHz...

    theoretically you can work with a tick of 1usec or less.

    Unfortunately the irq routine require a certain time to be served ;)