cancel
Showing results for 
Search instead for 
Did you mean: 

Timer as Counter

muralij21
Associate II
Posted on December 03, 2010 at 11:49

Timer as Counter

14 REPLIES 14
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?
lowpowermcu
Associate II
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

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
Up vote any posts that you find helpful, it shows what's working..
picguy2
Associate II
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.
muralij21
Associate II
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;

        }       

    }

 } 

muralij21
Associate II
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
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

mehmet.karakaya
Associate III
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 )

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

Thanks Karakaya

You saved my days!!