Making a timer, 32.768kHz+555+DT_INT. Good idea?


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    That would gain about 2.5 seconds per hour, which is not good.

    Have you set LP oscillator mode in your configs?
    Since it worked for you at 4mhz, it's probably set to XT and over driving the crystal.

    How are you measuring that frequency?
    Is it a high quality frequency counter?
    Accurate measurements of low frequencies can be difficult, with large errors.
    Yes, I change it to LP when driving it with 32 kHz and your advised process to run timer 1 from internal clock (loading TMR1H=%11100000) & XT when 4MHz (with 32kHz attached 4 TMR1).

    For frequency, unfortunately I am not equipped with those luxuries, I am just using an Timer Calculator I found in the Forum only. Screenshot attached.
    Attached Images Attached Images  
    ___________________
    WHY things get boring when they work just fine?

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Even with loading 32768 into TMR1, the calculator shows the interrupts 1.00027 Hz.
    This is just to seek clarification, it is unlikely that I will be able to use 4MHz for OSC & 32kHz for TMR1.
    ___________________
    WHY things get boring when they work just fine?

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    That calculator is wrong.

    Here's the way it works ...
    The main OSC is running at 32768 hz.
    The CPU is running a 8192 hz (32768 / 4).

    Timer1 is loaded with the highbyte of 57344 and will overflow at 65536.
    65536 - 57344 = 8192

    So the timer overflows exactly 1.0000 seconds every time (depending on the crystal tolerance).
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    That calculator is wrong.

    Here's the way it works ...
    The main OSC is running at 32768 hz.
    The CPU is running a 8192 hz (32768 / 4).

    Timer1 is loaded with the highbyte of 57344 and will overflow at 65536.
    65536 - 57344 = 8192

    So the timer overflows exactly 1.0000 seconds every time (depending on the crystal tolerance).
    OK, I am now running everything as advised. I synchronised the time with my phone last night. Today appx after 12 Hrs, the phone is appx 6 seconds ahead than the clock. What could be the possible problem?
    ___________________
    WHY things get boring when they work just fine?

  5. #5
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    Financecatalyst, I would first check that the crystal is indeed operating at the proper frequency... (Frequency counter)

    Dave Purola,
    N8NTA

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


    Did you find this post helpful? Yes | No

    Default

    With Timer1 you'll normally want to stop it, add in your reload value to the existing count (+ whatever time is involved for instructions after stopping it), then restart it. This can throw you off if you're not right on.

    This could be a lot easier with Timer2 since it gets automatically reset - so you don't need to mess with reloading the timer.

    Here's an example that keeps spot-on with my PC time clock to the second;
    Code:
    DEFINE OSC 4  ' using a 4MHz crystal
     
    ' setup vars for clock
    Time    VAR BYTE   ' accumulates TMR2 to PR2 match counts
    Minutes VAR BYTE   ' minutes
    Hours   VAR BYTE   ' hours
    Seconds VAR BYTE
    Match   VAR PIR1.1 ' TMR2 to PR2 match interrupt flag bit
     
    CMCON = 7          ' disable comparators
    INTCON = 0         ' not using interupts. Just monitoring int flag bits
     
    Time = 0           ' clear TMR2 to PR2 match counter
    Hours = 11         ' set clock starting hour here
    Minutes = 52       ' set clock starting minutes here
    Seconds = 0
     
    PR2 = 249           ' 249 +1 extra cycle for TMR2 reset = 250*5*16*1uS=20mS
    Match = 0           ' clear match flag
     
    ' setup & start TMR2
    T2CON = %00100110   ' 1:5 postscale, 1:16 prescale, TMR2 on
     
    Main:
       ' every 20mS the TMR2IF flag is set, and this routine is entered.
       ' Plenty of time to do other stuff.
       IF Match THEN          ' has TMR2 matched PR2? (should happen every 20mS)
          Match = 0           ' yes. clear TMR2 to PR2 match flag bit
          Time = Time + 1     ' increment 20mS count     
          IF Time = 50 THEN   ' 50 x 20mS = 1 second
             Time = 0         ' show time in 1 second intervals
             HSEROUT ["Time = ", DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds,13,10]
             Seconds = Seconds + 1
          ENDIF    
          IF Seconds = 60 THEN     ' has 60 seconds passed?
             Seconds = 0
             Minutes = Minutes + 1 ' increment minute count
             IF Minutes = 60 THEN  ' have 60 minutes passed?
                Minutes = 0        ' yes. roll-over minutes 00
                Hours = Hours + 1  ' update hours
                IF Hours = 24 THEN Hours = 0 ' roll-over hours from 24 to 00
             ENDIF ' end IF Minutes = 60                
          ENDIF    ' end IF Seconds = 60
       ENDIF       ' end IF Match
     
       GOTO Main
       END
    Of course you'll want a 4MHz osc that's right on the money if you need really precise timing.
    Last edited by Bruce; - 30th December 2010 at 20:54.
    Regards,

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

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    Of course you'll want a 4MHz osc that's right on the money if you need really precise timing.
    He's using a 32768 crystal for the primary OSC.
    DT

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