Using TMR0 to keep time


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Default Using TMR0 to keep time

    I'm trying to use TMR0 for one second periods, but can't seem to get it. I'm using a PIC16F887 with a 4MHz clock. From what I understand you take (((4MHz / 4) / 256) / prescaler) which will give you the total overflows per second. I know that it won't be an exact second every time it advances, but it should correct itself over time. I'm using a prescaler of two and this is what I have so far:

    (1MHz / 256) / 2 = 1953.125
    1953.125 * 8 = 15625

    Code:
    While (INTCON.2 == 0) : WEND
    INTCON.2 = 0
    Time = Time + 8
    if (Time > 15625) Then
        Time = Time - 15625
        gosub Second
    endif
    Is there something wrong with what I did? I can't seem to get the clock to be very accurate.

  2. #2
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default Re: Using TMR0 to keep time

    Download and use Mister_E's PicMultiCalc from http://www.picbasic.co.uk/forum/cont....-PICMultiCalc.

    It totally rocks and will calculate the settings you want.

    Best Regards,
    Paul
    The way to avoid mistakes is to gain experience. The way to gain experience is to make mistakes.

  3. #3
    Join Date
    Nov 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Using TMR0 to keep time

    Quote Originally Posted by prstein View Post
    Download and use Mister_E's PicMultiCalc from http://www.picbasic.co.uk/forum/cont....-PICMultiCalc.

    It totally rocks and will calculate the settings you want.

    Best Regards,
    Paul
    Alright, so the number I got for each interrupt was 512 uS. With the program I got 513-527 uS. I realized I didn't include the instruction cycle in the equation which adds on a uS per cycle, any idea on how I could figure out the instruction cycle number?

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