PDA

View Full Version : using TMR0 as a counter..



sirvo
- 27th July 2007, 16:46
Hi! I have two question, can you help me?

1) I'm using TMR1 as a timer and it ticks every 100ms. Now, I would like to measure an external clock (RPM) (0 - 1000Hz) and display it every 500ms on a LCD display. The extenal source is connected to RA4 and think it is possible to use TMR0 as a counter. The problem is that TMR0 is a 8-bit timer and it counts till 256 until overflow. So, my question is: If I set TMR0 prescaler to 1:4, would it be possible to count 256*4 = 1024 until overflow? I mean, will TMR0 count every 4 external ticks?

Here is Bruce's TMR0 counter prescaled 1:1 from the forum. It uses interrupt but as my TMR1 ticks every 100ms, i think it's not necessary to interrupt TMR0.



TRISA = %00010000 ' Set PORTA.4 (TOCKI) to input for timer0

OPTION_REG = %00111000
'Transition on TOCKI (RA4),
'Increment on falling edge
'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
INTCON.2 = 0 ' Clear Timer0 int flag
INTCON.5 = 1 ' Enable Timer0 int
TMR0 = 0 ' Set TMR0 to 0.

ON INTERRUPT GOTO LapCount
Main:
' Do something here
goto Main

disable
LapCount:
TMR0 = 0 ' Indicate we're in interrupt handler
INTCON.2 = 0 ' Clear TMR0 Interrupt Flag (Bit 2)

Resume
Enable


2) If a set TMR2 prescaler to 1:256 and set TMR2 = 0, it will interrupt every ~65,5ms (4MHz crystal). I a set the postscaler to 1:16, will it interrupt every 16*65,5ms = 1,048s?

Thanks in advance.

Sylvio

Michael Wakileh
- 28th July 2007, 02:56
I am new here and do not have any experience with timers but I will try to help nonetheless, because I too am trying to learn about this...


regarding 2) YES

regarding 1) I think you are right about the timer0 counting every 4 ticks for prescaler 1:4 but only it will be counting till 256...what I mean is that timer0 is 8 bit.

if you are measuring rpm at maximum of 1000HZ then Timer 1 should overflow the earliest after approximatly 1.024 second using prescaler 1:4. So you don't have to worry about an overflow before 500ms. On every 5th timer1interrupt (after 500ms) you could save the count in Timer0 to a word variable (and then reset it). This variable you need to multiply by 8 for the display in Hz(. (Times four because you are only counting every 4th clock....Times 2 because you've only been counting for 500ms. I believe multiplying by 8 is the same as left shifting by 3 (<< 3). This is why I would use a word variable to beginn with.

Alternatively you could probably use prescaler 1:1 and Timer0 interrupts, to add the actual counted ticks to a (word sized) variable. You could start timer 1 and timer 0 at the same time... on each timer0 interrupts (about every 1/4 of a second) add 256 to the counting word variable. Then on the 5th timer 1 interrupt (using software counter), stop timer0 retrieve its value and add it to the counting word variable. The number in the counting word variable is multiplied by 2 (=<< 1) to get the count per second.


I really suggest you have a look at Darrell Taylors instant interrupts pages

http://darreltaylor.com/DT_INTS-14/intro.html
or
http://darreltaylor.com/DT_INTS-18/home.html


He has made it easy for anyone to use multiple interrupt sources with pic basic pro and ASM interrupt handlers



good luck!