Timer Accuracy


Closed Thread
Results 1 to 9 of 9

Thread: Timer Accuracy

Hybrid View

  1. #1
    GEEZER's Avatar
    GEEZER Guest

    Default Timer Accuracy

    I'm new a this. I need a background timer for a project to give me an exact second. I have it down to a max error of .3% it seems to drift between .3% and .1%. Is this as good as it gets?

    I am using a 16F877A at 20 mHZ. Can I make any adjustments that will give me absolute precision?

    Here's my code.

    DEFINE LCD_DREG PORTC
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTC 'DEFINES
    DEFINE LCD_RSBIT 1
    DEFINE LCD_EREG PORTC
    DEFINE LCD_EBIT 0
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 4
    DEFINE LCD_COMMANDUS 3000
    DEFINE LCD_DATAUS 1000
    DEFINE OSC 20

    'VARIABLES
    REFRESHED VAR BIT
    X VAR WORD
    Y VAR WORD
    Z VAR WORD
    ZZ VAR WORD
    X = 0
    Y = 0
    Z= 0
    ZZ=0

    ON INTERRUPT GOTO PULSE_COUNT 'WHERE TO GO ON INTERRUPT

    GOTO INITIALIZE 'SKIP INTERUPT CODE TO SET UP TIMER

    ''' INTERRUPT CODE '''

    DISABLE
    PULSE_COUNT:
    T1CON.0 = 0 ' TURNS TIMER OFF
    T1CON.0 = 1
    REFRESHED = 1 'SET FLAG TO SAY COUNT READ


    X = X + 1
    IF X = 133 THEN
    Y = Y + 1
    X = 0
    ENDIF



    INTCON.2 = 0 'CLEARS INTERRUPT FLAG
    RESUME
    ENABLE ' RELOAD SYSTEM VARIABLES

    ''' END OF INTERRUPT CODE '''


    ''' INITIALIZE ALL REGS '''

    INITIALIZE:
    OPTION_REG.0 = 0 'SET PRESCALER TO 1:1
    OPTION_REG.1 = 0
    OPTION_REG.2 = 0
    OPTION_REG.5 = 0 'TMR0 CLOCK FROM MAIN OSC
    OPTION_REG.3 = 0 'PRESCALER ASSIGNED TO TMR0
    INTCON.2 = 0 'CLEARS INTERRUPT FLAG
    T1CON = %00000110
    T1CON.0 = 1 'TURNS TIMER ON
    TMR0 = 7 'LOAD THE TIMER IN MEMORY
    INTCON.5 = 1 'ENABLE TMR0 INTERRUPTS
    REFRESHED = 0 'HOLD FIRST READ TILL MAIN INT OCCURS
    INTCON.7 = 1 'START TIMER RUNNING

    ''' CALCULATIONS AND READ OUT '''
    HIGH PORTC.1
    PAUSE 1000
    LCDOUT $FE, 1

    LOOP:
    IF Y = 59 THEN
    Z = Z + 1
    Y = 0
    ENDIF



    iF REFRESHED = 1 THEN
    LCDOUT $FE, 1
    LCDOUT $FE, 2
    LCDOUT
    LCDOUT $FE, $C0
    LCDOUT " SEC ", DEC Y
    LCDOUT $FE, $90
    LCDOUT " MIN ", DEC Z
    LCDOUT $FE, $D0
    LCDOUT
    REFRESHED = 0 'PREVENT RE-READ TILL NEXT
    ENDIF 'INTERRUPT
    PAUSE 50
    GOTO LOOP
    END

    Your help is appreciated.

    The Geezer

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    IMHO, the most accurate solution is to get TIMER overflow interrupt counting the internal clock ticks. adjust the prescaller and pre-load the timer registers TMR1L and TMR1H. once you get interrupt re-load TMR1L, TMR1H.

    a other way to do it simple too... can you acces to the ac-line source and divide the frequency by using internal counter to get a accurate 1 second?? I mean counting the ac-line pulses (let's say 60hz) and once you get 60 or a counter overflow interrupt do your stuff.

    maybe confusing but that's food for brain cells...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    GEEZER's Avatar
    GEEZER Guest


    Did you find this post helpful? Yes | No

    Default Timer Accuracy

    Thanks Steve,

    The 60 HZ is out as this will be used on AC and Battery power. I'll do a search for TMR1H and TIMR1L. It's all new to me with only a couple of months experience.

    The Geezer

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


    Did you find this post helpful? Yes | No

    Default

    I hate to tear other peoples code apart, but I think it's shot.

    In one spot you're playing with TMR0...

    TMR0 = 7

    and then you're messing with TMR1...

    T1CON.0 = 1 'TURNS TIMER ON

    So which Timer are you using? And then what interrupt time interval are you keeping track of?

    Also those Pause commands... what are you going to do when an interrupt occurs in the middle of one? Remember, you don't get an interrupt until the current PICBasic command completes.

    As an example (including interrupt usage), have a look at this...

    http://www.picbasic.co.uk/forum/showthread.php?t=632

  5. #5
    GEEZER's Avatar
    GEEZER Guest


    Did you find this post helpful? Yes | No

    Default Timer Accuracy

    Thanks Melanie, you just shot my whole timer plan. I didn't think about the pauses.

    I have been writing computer routines in basic anc C for more than 20 years, 50 years of amatuer and professional electronics experience, and about 2 months of experimenting with this PIC.

    This timer was to be used in a pic program that includes a 4 button - 4 line lcd interface, the analog converters and a number of digital pots, to precisely control and monitor the work of four power transistors. This program will probably strain the 8k program limit of the 16F877A.

    I guess starting small is just not me.

    The systems are all working except for the timer and the digital pots which arrived yesterday.

    My mind just can't seem to run at 20 mHZ.

    I guess I am going to have to add an outboard RTC and find a way to access it.

    Is there any other way?

    I surely appreciate the suggestions I have been given on this forum by you and others.

    Thanks again,

    The Geezer

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    It would help if you gave us an idea of what are you are using your "exact second" for, and why it has to be so accurate.

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  3. Timer + rc5
    By naga in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th November 2009, 07:56
  4. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  5. timer interupt help 16f73
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd July 2005, 08:41

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