Skip to main content
muralij21
Associate II
December 3, 2010
Question

Timer as Counter

  • December 3, 2010
  • 14 replies
  • 2439 views
Posted on December 03, 2010 at 11:49

Timer as Counter

    This topic has been closed for replies.

    14 replies

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    ''finding difficult and unable to get the output.''

     

    Which part(s) are you finding difficult - doing the counting, or transmitting the count through the UART?

    Or both?
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Might help if you provide a more complete example. Or use the library code, if you are programming the device at the register level, you should be prepared to debug it at that level.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    lowpowermcu
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Hi David,

    Try to configure PA15 in input floating:

      /* GPIOA clock enable */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_Init(GPIOA, &GPIO_InitStructure);

    Before that check that PA15 is shared with JTAG so you will lose connection with your debugger (Check it because I am not sure).

    MCU Lüfter

    muralij21
    muralij21Author
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    I am having problem with the Counting part... USART part works fine..

    Here is the code which i wrote..

    TIM2->PSC=0x0000;

        TIM2->ARR=0xffff;             //Autoreload value

        TIM2->CNT=0x0000;

        TIM2->EGR=0x0000; //0x0001;

        TIM2->CR1=0X0280;             //0x0281;//Prescalar is 10 ie. tDTS=4*tCK_INT, autoreload enabled

        TIM2->DIER = 0x0001;;                             // enable interrupt

        NVIC->ISER[0]=0x4000000;

        TIM2->SMCR=0x57;      //SMS=111 and TS=101 for TI1(PA15)

        TIM2->CCMR1=0x100;  //CC2S=01

        TIM2->CCER=0x00;     //CC2P=0

        TIM2->CR1|=0x0001;   

        RCC->APB2ENR=0x00004004;

        AFIO->MAPR=0x00000000;

        GPIOA->CRH=(GPIOA->CRH & 0xFFFFF00F)|0x4B0;

        USART1->BRR=0x341;

        USART1->CR1=0x0000000C;

        USART1->CR2=0x800;

        USART1->CR1|=0x2000;

        ch=0;

        while(1)

        {

            if (TIM2->SR&0x40)

            {

                TIM2->SR&=0xffbf;

                ch|=TIM2->CNT;

                x=ch+0x30;

                USART1->DR=x;   

                while (USART_IT_TXE==0);

                a=USART1->SR;

            }       

        }

     } 

    picguy2
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Recommendations from a software POV:

    Allow the counter to run 0000-FFFF and back.  Don’t read out and reload as you may miss a pulse.  When it’s time to report:

      temp = TMR2_CNT

      saveForSerialPort = (temp-savedT2) & 0xFFFF //mask or save as half word

      savedT2 = temp

    Time to report could be a timer ISR, a push button or a code event.  If its either of the first two it’s best to do the above in an ISR.  But don’t hang in that ISR until all your serial data is transmitted.  Instead, save the count (in a FIFO if necessary) for your non-ISR code to convert into characters for RS-232.

    Verify the above by running T2 as a timer with an appropriate prescaler.  This will divorce you from external events allowing for easier debug.
    dsystem
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Hi Devid

    I am trying to do the same, use Tim2 as Counter (ETR pin as Pulse input).

    I failed.

    Can you past your full code to help me?

    Regards

    Vimal

    muralij21
    muralij21Author
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Hey i sort it out.. I hadn enable the clk.. Now its working fine

    Thanks for help guys

     

    dsystem
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    Thanks Karakaya

    You saved my days!!

    mehmet.karakaya
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    dont waste your time counting pulses at ETR pin

    no matter what I tried I couldnot count ETR pulses - ( there is probably a bug in hardware )

    try to count pulses at CH1 of the timer - it works

    in addition encoder mode 1 & 2 works like mode 3 ( another bug )

    mehmet.karakaya
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:17

    this is my code for CH1 counting

    void TIM4init(void) {

     

      /* Time base configuration */

     

      TIM_TimeBaseStructure.TIM_Period = 0xffff;

     

      TIM_TimeBaseStructure.TIM_Prescaler = 0;

     

      TIM_TimeBaseStructure.TIM_ClockDivision = 0;

     

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

      TIM_ITConfig(TIM4,TIM_IT_Update , ENABLE);

     

     

      TIM4->CCMR1=0x0031;

     

      TIM4->SMCR=0x0057;

     

      TIM_Cmd(TIM4, ENABLE);

     

    }