2010-12-03 02:49 AM
Timer as Counter
2011-05-17 05:17 AM
''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?
2011-05-17 05:17 AM
2011-05-17 05:17 AM
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.
2011-05-17 05:17 AM
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.2011-05-17 05:17 AM
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; } } }2011-05-17 05:17 AM
Thanks for help guys
2011-05-17 05:17 AM
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
2011-05-17 05:17 AM
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 )2011-05-17 05:17 AM
Thanks Karakaya
You saved my days!!