Clock using Instant Interrupts


Closed Thread
Results 1 to 4 of 4

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    If you use Timer2 for your clock it leaves Timer1 free to be used with hardware capture. Then you can use capture to read your IR signal.

    Here's an example tested with the SLED-C4. It only needs to test for the TMR2IF flag to keep time, so no interrupts are used. And you have quite a few instruction cycles to do other things between clock updates.

    Code:
    @ DEVICE LVP_OFF,WDT_OFF,MCLR_OFF,XT_OSC
    
    DEFINE OSC 4
    DEFINE DEBUG_REG PORTB
    DEFINE DEBUG_BIT 0
    DEFINE DEBUG_BAUD 19200
    DEFINE DEBUG_MODE 0 ' SLED-C4 serial mode = true
        
    ' setup vars for clock
    Time VAR WORD      ' accumulates TMR2 to PR2 match counts
    Minutes VAR BYTE   ' minutes
    Hours VAR BYTE     ' hours
    Match VAR PIR1.1   ' TMR2 to PR2 match interrupt flag bit
    
    PORTB.0=1          ' make sure SLED-C4 data input pin idles high        
    TRISB = %00001000  ' RB3 = CCP1 capture input, rest outputs
    CMCON = 7          ' disable comparators
    INTCON = 0         ' not using interupts. Just monitoring int flag bits
    
    PAUSE 250          ' let SLED-C4 power-up
    
    Time = 0           ' clear TMR2 to PR2 match counter
    Hours = 10         ' set clock starting hour here
    Minutes = 03       ' set clock starting minutes here
    
    ' set SLED-C4 start time
    DEBUG "D", Hours DIG 1,Hours DIG 0,Minutes DIG 1,Minutes DIG 0
    
    ' TMR2 increments from 0 until it matches the PR2 register value
    ' and then resets to 0 (on the next increment cycle) so place a
    ' value in PR2 1 cycle short of what you want. Here we want 250 for
    ' 60mS, so we write 249 to PR2.
    PR2 = 249           ' 249 +1 extra cycle for reset = 250*16*15*1uS=60mS
    Match = 0           ' clear match flag
    
    ' setup & start TMR2
    T2CON = %01110110  ' 1:16 prescale, 1:15 postscale, TMR2 on
    
    Main:
       ' every 60mS 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 60mS)
          Match = 0           ' yes. clear TMR2 to PR2 match flag bit
          Time = Time + 1     ' increment 60mS count    
          IF Time = 1000 THEN ' has 60 seconds (1000*60mS) passed?
             Time = 0         ' yes. clear count
             Minutes = Minutes + 1 ' increment minute count
             IF Minutes = 60 THEN  ' have 60 minutes passed?
                Minutes = 0        ' yes. roll-over minutes from 59 to 00
                Hours = Hours + 1  ' update hour every 60 minutes
                IF Hours = 24 THEN Hours = 0 ' roll-over hours from 24 to 00
             ENDIF                 
             ' print new time only once every minute
             DEBUG "D", Hours DIG 1,Hours DIG 0,Minutes DIG 1,Minutes DIG 0 
          ENDIF    
       ENDIF
       
       GOTO Main
       
       END
    It's pretty decent at keeping time. Ran for 3 hours, and it was spot-on with my PC clock to the second.
    Regards,

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

  2. #2
    Join Date
    Feb 2009
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    Thanks for the help both Darrel and Bruce!

    I kicked myself for not seeing that I wasn't resetting the Counts in my first posting However, things are working well now, and I can read my IR signal as I want by putting the clock routine as my interrupt and the IR PULSIN command as my main (as Darrel suggested). At this point is it just some coding to recognize alarms and different programmable options that I will throw in.

    I don't think I will have a problem with having enough time for tasks between interrupts since I am only doing the initial PULSIN command in my Main. If there was some kind of restriction in the interrupt routine I might have a problem them, but most of the heavy coding will be outside the main and the Interrupt routine. Though the code Bruce posted is very concise. I will try things out with both of them and see how things go.

    Ill post here if I have any more questions.

Similar Threads

  1. Instant Interrupts - Revisited
    By Darrel Taylor in forum Code Examples
    Replies: 772
    Last Post: - 17th February 2016, 22:14
  2. 18F2420, instant interrupts, and TMR0
    By dhickman in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 11th March 2009, 18:33
  3. DT instant interrupts with mister_e keypad
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th November 2008, 20:02
  4. DT's Instant Interrupts trouble
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th November 2008, 20:48
  5. Replies: 1
    Last Post: - 1st November 2006, 03:11

Members who have read this thread : 1

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