Interrupt and Time Out


Results 1 to 4 of 4

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    Just setup Timer1 with a variable to count Timer1 over-flows.
    No need for interrupts on the timer, just monitor TMR1IF overflow
    interrupt flag bit, and let Timer1 do everything for you in the
    background.

    This gives you roughly 5 seconds at 20MHz.
    Code:
    DEFINE OSC 20
    
    ' I/O definition
    '
    TRISB = %10000001   ' Set PORTB (0)INPUT (1-5)OUTPUTS
    T1CON = %00110000   ' Timer1 1:8 prescale. Timer1 off
    
    ' Variable Definition
    '
    Switch              VAR PORTB.0
    Red                 VAR PORTB.1
    Green               VAR PORTB.2
    Blue                VAR PORTB.3
    
    
    
    PushHowManyTimes    VAR Byte
    PWMVariable         VAR Byte	' PWM Variable 1-9
    PWMVariableTimeOut  VAR Byte	' 5 second Timeout (100 loops of 50mS)
    storedloop          VAR Byte
    onled               VAR Byte
    
    Timer VAR BYTE    ' Holds timer ticks. 1 tick = 65,536*Tosc*prescale
    Timer = 0            ' Clear on start                        
    
    ' register and interrupt definition
    '
    CMCON = 7          ' Disable analog comparator on PORTA... not needed as now
    OPTION_REG=0       ' enable pull-up resistor on PORTb
                       ' Interrupt on falling edge of RB0
    INTCON = %10010000 ' Enable RB0 interrupt
    
    PORTC.0=1   ' NOTE: Just for visual test | LED blinking every 5 S
    TRISC.0 = 0
    
    On Interrupt Goto ProcedureSwitcher
    
    ' Variable initialisation
    '
    'PORTB = 0             ' Reset all outputs on PORTB
    PushHowManyTimes = 0
    
    ReStart:
        TMR1H = 0
        TMR1L = 0   ' Clear time counts before Timer1 re-start
        PIR1.0 = 0   ' CLear over-flow flag before enable
        T1CON.0 = 1 ' Turn Timer1 back on before entry into MainProcedure
    
    'Main Procedure
    
    MainProcedure:
        Select Case PushHowManyTimes
         case 1
              Gosub resetleds
              PWMVariable = 1
              PWM BLue,1,100
         Case 2
              Gosub resetleds
              PWMVariable = 2    
              PWM blue,25,100
         Case 3
              Gosub resetleds
              PWMVariable = 3        
              PWM blue,255,100
         Case 4
              Gosub resetleds
              PWMVariable = 4        
              PWM green,1,100
         Case 5
              Gosub resetleds
              PWMVariable = 5        
              PWM green,25,100
         Case 6
              Gosub resetleds
              PWMVariable = 6        
              PWM green,255,100
         Case 7
              Gosub resetleds
              PWMVariable = 7        
              PWM red,1,100
         Case 8
              Gosub resetleds
              PWMVariable = 8        
              PWM red,25,100
         Case 9
              Gosub resetleds
              PWMVariable = 9        
              PWM red,255,100
         End Select
         
         ' IF PIR1.0 = 1 TMR1 register overflowed (must be cleared in software)
         IF PIR1.0 THEN       ' IF Timer1 has over-flowed then
            Timer = Timer + 1 ' Increment Timer variable
            PIR1.0 = 0        ' Clear over-flow/int flag
            ' @20MHz 200nS * 65,536 * 8 = 0.1048576 seconds per over-flow
            ' 0.1048576 * 48 = ~5.033 seconds before jump to NextStage
            IF Timer >= 48 THEN NextStage
         ENDIF
        Goto Mainprocedure
    
    
    ResetLEDs:
        Low PortB.1                             ' reset output to PORTB
        Low PortB.2
        Low PortB.3
        Low PortB.4
        Low PortB.5
    Return
    
    NextStage: ' Dont really know what you need here. Just a visual test
    '    T1CON.0 = 0 ' Turn off Timer1 if you need to here
         Timer = 0   ' Clear Timer var on entry here
    '    TMR1H = 0
    '    TMR1L = 0   ' CLear timer count registers as required
         PORTC.0 = PORTC.0 ^ 1   ' Toggle LED for test
        ' Do something here, etc,,
         GOTO ReStart ' When you're ready to start all over
        
    ' Interrupt handler stuff here
    '
    Disable                                     ' Disable interrupts in handler
    ProcedureSwitcher:
        PushHowManytimes = PushHowManytimes + 1     ' Changing task
        If PushHowManytimes = 10 Then PushHowManytimes=1
    
    Here:
        While SWITCH = 0                        ' waiting until
        wend                                    ' push-button is release
        pause 100                               ' debounce time
        If switch = 0 then here
        PIR1.0 = 0 '  Clear Timer1 over-flow flag
        Timer = 0  ' Clear Timer counts before return
        INTCON.1=0                              ' reset RB0 interrupt flag
    Resume                                      ' Return to main program
    
    Enable                                      ' Enable interrupts after
                                                ' handler
                                                
        END
    Last edited by Bruce; - 16th March 2005 at 02:13.
    Regards,

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

Similar Threads

  1. Interrupt RPM and Taylors Elapsed time on 18F4620
    By Tobias in forum mel PIC BASIC Pro
    Replies: 70
    Last Post: - 3rd February 2010, 17:12
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 03:35
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 19:14
  4. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 03:59
  5. Interrupt cycle time limitation?
    By jswayze in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th March 2005, 08:11

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