TMR0 Pre Scale Question


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79

    Default TMR0 Pre Scale Question

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    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.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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;

    Code:
        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.
    Last edited by Bruce; - 8th March 2008 at 15:34.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    A high priority interrupt can interrupt a low priority interrupt, but not vice versa.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  2. TMR0 interrupt and HSERIN
    By boban in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd September 2008, 11:48
  3. help: TMR0 interrupts disabling PORTAchange interrupts???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th August 2008, 15:10
  4. using TMR0 as a counter..
    By sirvo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th July 2007, 02:56
  5. Help to Pre Scale 4mhz to 20mhz
    By jetpr in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th September 2005, 03:30

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts