PDA

View Full Version : TMR0 Pre Scale Question



shawn
- 8th March 2008, 07:24
Hi I am using TMR0 and trying to get an interrupt approximately once a second. This is so I can refresh my LCD display. Please check my math. 20Mhz and 16 bit timer at a 1:1 presclaer I would do this to get my interrupt interval. (1/20,000,000) * 65535 = .003276 or 3.276mS. Is that right, I would get an interrupt every 3.276mS. I set this up and I'm getting an Interrupt every half second approximately. I'm toggling the state of an LED in My interrupt routine to see the Interrupts. Sorry to start a new thread on this but I could not figure the math in some of the other tmr0 threads. The numbers they were coming up with gave me a headache.

Thanks
Shawn

Bruce
- 8th March 2008, 11:07
With a 20MHz oscillator you have an instruction cycle time of 1/5MHz or 200nS. With a 16-bit timer it will roll-over in 65536 ticks. $FFFF+1=roll-over.

65536 * 200nS = 13.1mS assuming a 1:1 prescale.

Kamikaze47
- 8th March 2008, 14:49
Bruce. The thing to remember when calculating the time is that the timer only ticks over once every 4 cycles of your main oscillator. So a 20Mhz oscillator equates to a timer clock speed of 5Mhz.

Bruce
- 8th March 2008, 15:29
Yep. So with a 16-bit timer, 1:1 prescaler, 20MHz osc, it will count up & roll-over in
200nS * 65,536, which = ~13.1mS.

If you can live with a few uS short of 1 second, then you can change the prescaler
to 1:256, and do something like this;



DEFINE OSC 20
TMR0_FLAG VAR INTCON.2 ' Timer0 over-flow flag bit

TMR0H = $B3 ' $B3B5 = 46,005
TMR0L = $B5
TMR0_FLAG = 0 ' clear TMR0 over-flow flag
T0CON = %10000111 ' 16-bit, 1:256 prescale, TMR0 ON

Main:
REPEAT ' just loop waiting for over-flow flag
@ NOP
UNTIL TMR0_FLAG = 1

TMR0_FLAG = 0 ' clear over-flow flag
TMR0H = $B3 ' reload for 1 second over-flow
TMR0L = $B5
TOGGLE PORTB.0

GOTO Main
Works out to roughly 0.999 seconds per toggle.

shawn
- 8th March 2008, 20:36
Ok guys that helps allot. So every PBP instruction takes at least 4 clock cycles or the tmr0 is just set to increment every 4 clock cycles. Also maybe I missed it in the data sheets or I am just supposed to know this answer but what is the difference between low and high priority interrupts. So far I have just been disabling the priority level with RCON.7 = 0. The reason I ask is because I am going to be using the TMR0 Interrupt, the INT0, INT1,INT2 interrupt, and the usart continuous receiver interrupt. I've used all of them before by them selfs. I've used some of them together, but not all of them together in the same program. Oh ya I am using the on interrupt command in PBP. Once I have a good hand on that I may try to venture into (forgive me if I name the wrong creator of the program) Darrels instant interrupt program. I'm using the Pic 18F458 if that matters.

Thanks again
Shawn

shawn
- 8th March 2008, 21:49
Maybe I found an answer to the priority level question. Will a high priority interrupt wake the processor out of sleep mode and a low priority interrupt will not.
New Question for ya all.
Int2 on a Pic 18f458 says that the edge trigger is selectable in the INTCON2 registers. I'm not seeing it in there. INT0 and INT1 are in there apparently int2 is rising edge trigger only.

Shawn

Kamikaze47
- 9th March 2008, 05:09
A high priority interrupt can interrupt a low priority interrupt, but not vice versa.