On Interrupt


Closed Thread
Results 1 to 8 of 8

Thread: On Interrupt

  1. #1
    Mark's Avatar
    Mark Guest

    Question On Interrupt

    I am beginning to experiment with interrupts and can see there are many possibilities.

    Using the PBP On Interrupt seems easy but I have a few questions about how it works that I can't seem to find answers to in the manual.

    1. Does it scan all the interrupt flag bits or just RB0 ?

    2. Do enable or disable reset the flag automatically ?

    The datasheet states that the flag bit needs to be reset in software so I assume that PBP is taking care of this - I'm just not sure how it does it.

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


    Did you find this post helpful? Yes | No

    Default

    Just a brief on it!

    Before using On INTERRUPT, you must enable the interrupt you want to monitor, RB0, USART, TIMER overflow.... by setting the according registers (INTCON...)

    PBP will not automaticaly set it for you, same for reseting the flag intterupt.

    Tell us wich PIC you have and wich interrupt you want to monitor and we can do a code snip for you.
    Steve

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

  3. #3
    Mark's Avatar
    Mark Guest


    Did you find this post helpful? Yes | No

    Default

    I'm using a 16F628 and RB0

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


    Did you find this post helpful? Yes | No

    Default

    here's something to play with.
    Code:
        ' RB0 interrupt
        ' =============
        '
        ' File name : RB0.bas
        ' Company : Mister E 
        ' Programmer : Steve Monfette
        ' Date : 03/05/2005
        ' Device : PIC16F628
    
    
        '
        '    PIC setting
        '    ===========
             '
             ' Using MPASM to compile the code to target device
             '
             '
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _LVP_OFF & _BODEN_ON 
             ' Internal Oscillator
             ' Enable watch dog timer
             ' Enable power up timer
             ' Disable MCLR pin
             ' Disable low voltage programming
             ' Enable brown out detect
    
    
        '
        '    Hardware definition
        '    ===================
             '
             '
             TRISB = %00000001   ' RB0 as input, other as output
                                 ' Push button between RB0 & ground
        
        '
        '    I/O Alias definition
        '    ====================
             '
             '
             RB0LED              var   PORTB.6
             MainLoopLED         var   PORTB.7
    
             
        '
        '    Interrupt Definition
        '    ====================
             '
             '
             OPTION_REG = 0      ' Enable pull-up on PORTB
                                 ' RB0 interrupt on falling edge
                                 
             INTCON = %10010000  ' Enable Global interrupt
                                 ' Enable RB0 interrupt
    
             on interrupt goto RB0Interrupt
             
        '
        '    Variable definition
        '    ===================
             '
             '
             Delay              var word
             
             
        '
        '    MainLoop
        '    ========
             '
             '
    Start:
         ' Let's do some led blink on RB.7
         '
         '
        Toggle mainloopled
        for Delay = 1 to 500 ' delay loop 500ms
            pause 1          ' using 1 ms delay to avoid latency
            next             '
        goto start           ' do it forever
        
             
        Disable Interrupt
    RB0Interrupt:
                ' RB0 interrupt subroutine
                '
                ' will toggle RB6 state everytime we get an interrupt
                '
                '
        Toggle rb0led
        while PORTB.0 = 0                ' wait untill RB0 go high 
        wend
        pause 50                         ' debounce delay
        INTCON.1 = 0                     ' Reset RB0 int flag
        resume                           ' return to main loop
        Enable interrupt
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    and another way by polling interrupt flag.
    Code:
        ' RB0 interrupt
        ' =============
        '
        ' File name : RB0.bas
        ' Company : Mister E 
        ' Programmer : Steve Monfette
        ' Date : 03/05/2005
        ' Device : PIC16F628
    
    
        '
        '    PIC setting
        '    ===========
             '
             ' Using MPASM to compile the code to target device
             '
             '
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _LVP_OFF & _BODEN_ON 
             ' Internal Oscillator
             ' Enable watch dog timer
             ' Enable power up timer
             ' Disable MCLR pin
             ' Disable low voltage programming
             ' Enable brown out detect
    
    
        '
        '    Hardware definition
        '    ===================
             '
             '
             TRISB = %00000001   ' RB0 as input, other as output
                                 ' Push button between RB0 & ground
        
        '
        '    I/O Alias definition
        '    ====================
             '
             '
             RB0LED              var   PORTB.6
             MainLoopLED         var   PORTB.7
    
             
        '
        '    Interrupt Definition
        '    ====================
             '
             '
             OPTION_REG = 0      ' Enable pull-up on PORTB
                                 ' RB0 interrupt on falling edge
                                 
    
        '
        '    Variable definition
        '    ===================
             '
             '
             Delay              var word
             
             
        '
        '    MainLoop
        '    ========
             '
             '
    Start:
         '
         '
        Toggle mainloopled
        for Delay = 1 to 500    ' delay loop 500ms
            pause 1             ' using 1 ms delay to avoid latency
            if INTCON.1 then    ' test RB0 interrupt flag
               rb0led=RB0LED ^1 ' toggle RB0LED status
               INTCON.1=0       ' reset interrupt flag
               endif
            next                 
        goto start              ' do it forever
    Steve

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

  6. #6
    Mark's Avatar
    Mark Guest


    Did you find this post helpful? Yes | No

    Question

    Thanks Steve

    This is great - these examples give me 2 ways to use the interrupt.

    I will try these out - but before I do I have a question about the first example using the On Interrupt.

    I assume we need to disable the interrupt in the handler so we don't get caught in a endless loop - is that correct ?

    Should we have the "disable interrupt" inside the RBOinterrupt routine at the start ?

  7. #7
    Mark's Avatar
    Mark Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    I have tested both examples and they both produce the same function.

    They compile into 108 vs. 103 words.

    I modified the code and found the answers to my questions - There is no substitue for testing.

    I don't have an exact application in mind yet so it is good to know both.

    Do you have a preference for one technique or a simple way to describe the trade offs between the two?

    Thanks for your help.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Mark
    Do you have a preference for one technique or a simple way to describe the trade offs between the two?
    Since every project are different, need are also different. I don't prefer one to another, just need to know what i need and how much time i want to spend on. But more than often i'll use ON INTERUPT.
    Steve

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

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 18:14
  4. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 02:59
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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