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


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    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 19:54.
    Regards,

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

  2. #2
    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

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


    Did you find this post helpful? Yes | No

    Default

    Not a great idea with PBP since it doesn't support a 32,768 primary osc, but it could still work with;

    Load PR2 with 31. Set TMR2 prescaler & postscaler to 16. That would give you 1 second per match period.

    Try this with your sim program;

    Code:
    ' 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 = 31           ' 31 +1 extra cycle for TMR2 reset = 1/8192*256*32=1 second
    Match = 0           ' clear match flag
     
    ' setup & start TMR2
    T2CON = %01111110   ' 1:16 prescale, 1:16 postscale, TMR2 on
    TRISB.0 = 0
    PORTB.0 = 0
     
    Main:
       ' every 1S 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
          TOGGLE 0            ' toggles RB0 at ~1 second intervals
          Seconds = Seconds + 1 
          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
    Last edited by Bruce; - 30th December 2010 at 21:08.
    Regards,

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

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Thanks everyone.
    @Bruce - You are not using DT_INT in your sample code. Would it be possible to use these settings when using DT_INT. Because what you are doing in main, in my code it is done in ISR, because code also has sub_routines to look after, it will not stay in main all the time.
    ___________________
    WHY things get boring when they work just fine?

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


    Did you find this post helpful? Yes | No

    Default

    Yes. They can definitely be used with DT_INTs. I was just trying to show you that with Timer2/PR2 match you don't need to reload any timer values. Timer2 interrupts for sure work with DT_INTs.

    Any interrupt the PIC has should.
    Regards,

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

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Thank you everyone. I really appreciate the intention to help. But I think I have to give up on using timer for my purposes. Luckily I laid my hands on few DS1307, got them only for £0.55 each which is surely not what I expected them to cost (Everywhere I saw they were min £2-£2.50). They will be with me in few days time. Until then I will be using Proteus.

    For this I will start a new thread. Thanks again for the above answers.
    ___________________
    WHY things get boring when they work just fine?

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