Skip to main content
lior
Associate II
March 26, 2008
Question

timer 1 with external clock

  • March 26, 2008
  • 4 replies
  • 1002 views
Posted on March 26, 2008 at 06:29

timer 1 with external clock

    This topic has been closed for replies.

    4 replies

    lior
    liorAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 12:27

    hello ,

    i tried to work with timer 1 with external clock of 2 hz.

    the source clk is 1 second pulse on and 1 second off.

    the problem is that the interrupt excute every 3 pulses (6 SECONDS) and not every 2 seconds. (2 pulses delays).

    my configuration is below.

    what did i worng?

    /* Enable the TIM1 UP Interrupt */

    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    /***config TIM1 for priod 1 time and autorload of 1 ******/

    TIM1_TimeBaseStructInit(&TIM1_TimeBaseStructure);

    TIM1_OCStructInit(&TIM1_OCInitStructure);

    TIM1_TimeBaseStructure.TIM1_Prescaler = 0;

    TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Down;

    TIM1_TimeBaseStructure.TIM1_Period = 0x1;

    TIM1_TimeBaseStructure.TIM1_ClockDivision = 0x0;

    TIM1_TimeBaseStructure.TIM1_RepetitionCounter = 0x0;

    TIM1_SetAutoreload(0x1);

    TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);

    /**********************************************************************/

    /************config external clock without presclar and rising edge*****/

    TIM1_ETRClockMode2Config(TIM1_ExtTRGPSC_OFF,

    TIM1_ExtTRGPolarity_NonInverted, 0x0);

    /************config PE7 AS ETR for TIM1****************************/

    GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_Init(GPIOE, &GPIO_InitStructure);

    /**********ENABLE INTERRUPT****************************************/

    TIM1_ITConfig(TIM1_IT_Update, ENABLE);

    TIM1_Cmd(ENABLE);

    lior
    liorAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 12:27

    thanks for your remarks.

    the external clk is 2 hz (1 sec on and 1 off).

    and i the problem that the interrupt happen every 3 rise edge and not every rise edge as i expected.

    for your question:

    yes it is possible to do this also with EXTI .

    for our application we supply the same clock source (our clock source is very slow 2 hz)to some devices in order to synchronize between them and to do some operations simultaneous.

    lanchon
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:27

    I can't understand your post:

    ''i tried to work with timer 1 with external clock of 2 hz.

    the source clk is 1 second pulse on and 1 second off.''

    then it's 0.5Hz, not 2Hz. which is it?

    ''the problem is that the interrupt excute every 3 pulses (6 SECONDS) and not every 2 seconds. (2 pulses delays).''

    if you want an interrupt on every external pulse why are you using a timer? you need an EXTI line.

    lanchon
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:27

    hi,

    if the period is 2 seconds (1 on, 1 off) the frequency (1/T) is 0.5 Hz, not 2.

    forget the timer, use an EXTI. using a timer with a period of 1 count (that is, a constant value) is a corner case and is never recommended; and besides you don't need it.

    many timers misbehave in this condition. in particular, I'm remembering one of the PICs or dsPICs with motor control: in this chip, if you set the period to 1 you'll enter the counter in an invalid state from which you can't recover by software, but only with a reset (the shadow reg is never updated again). without going to that extreme, many timers don't fire the events correctly in this condition.

    I don't see why you want a timer, please explain if you continue to do so.