how to use INTERRUPT?


Closed Thread
Results 1 to 8 of 8
  1. #1

    Default how to use INTERRUPT?

    The code below blinks an LED on and off at two different rates. It switches between the two rates when a button is pressed. The button is connected to GPIO.1 of a PIC12F609.

    The code works fine, or I should say, it works exactly as I expect it to. The problem is that I would like the program to jump to the "buttonpress" subroutine as soon as the as the button makes GPIO.1 go low. As it is now, it needs to wait for the LED to finish its blink loop before it will recognize the button.

    Is an ON INTERRUPT statement what I want to use here? I've tried using it, but I don't think I'm setting it up correctly.

    Code:
    length var word
    LED var GPIO.0
    pushbutton var GPIO.1
    ANSEL = 0 ' Set all digital
    CMCON0 = 0 ' Analog comparators off
    TRISIO = %00000010
    length = 100
    
    
    main:
        do
            LED = 1
            pause length        
            LED = 0
            pause length
            
            if pushbutton = 0 then
                goto buttonpress
            endif       
        loop
    goto main
    end
    
    
    buttonpress:
        
        pause 10                            'debounce switch press
        if length = 100 then
            length = 500
        elseif length = 500 then
            length = 100
        endif
        
        do while pushbutton = 0             'wait for switch to be released
            pause 5
        loop 
        
        pause 10                            'debounce switch release
        
    goto main

  2. #2
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    This will answer all - works great!

    http://darreltaylor.com/DT_INTS-14/intro.html
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    Excellent! Thank you.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    I'm doing something wrong. I'm trying the first "hello world" program. I'm guessing PBP3 doesn't like that I've selected the 12F609 as my device. I want to change to a "falling edge". And I want to change the INT input pin. Where/how do I do that? I've installed the .bas files and they come up in my tabbed browser as defines, but I don't really know what I'm looking at.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    your not giving us much to work on , but from the data sheet and if you are using INT/int on gp2 then

    12.4.1 GP2/INT INTERRUPT
    The external interrupt on the GP2/INT pin is edgetriggered;
    either on the rising edge if the INTEDG bit of
    the OPTION register is set, or the falling edge, if the
    INTEDG bit is clear. When a valid edge appears on the
    GP2/INT pin, the INTF bit of the INTCON register is set.
    This interrupt can be disabled by clearing the INTE
    control bit of the INTCON register. The INTF bit must
    be cleared by software in the Interrupt Service Routine
    before re-enabling this interrupt. The GP2/INT interrupt
    can wake-up the processor from Sleep, if the INTE bit
    was set prior to going into Sleep. See Section 12.7
    “Power-Down Mode (Sleep)” for details on Sleep and
    Figure 12-9 for timing of wake-up from Sleep through
    GP2/INT interrupt.
    if you want more help post your code and a sketch/schematic of your setup

  6. #6
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    Quote Originally Posted by keithv View Post
    The code below blinks an LED on and off at two different rates. It switches between the two rates when a button is pressed. The button is connected to GPIO.1 of a PIC12F609.

    The code works fine, or I should say, it works exactly as I expect it to. The problem is that I would like the program to jump to the "buttonpress" subroutine as soon as the as the button makes GPIO.1 go low. As it is now, it needs to wait for the LED to finish its blink loop before it will recognize the button.

    Is an ON INTERRUPT statement what I want to use here? I've tried using it, but I don't think I'm setting it up correctly.
    ON INTERRUPT waits for the pause to complete before executing the interrupt routine. To get over this issue use short pauses in a loop. Here is a non interrupt option.

    Code:
    I var word
    length var word
    LED var GPIO.0
    pushbutton var GPIO.1
    ANSEL = 0 ' Set all digital
    CMCON0 = 0 ' Analog comparators off
    TRISIO = %00000010
    length = 100
    
    main:
       
       TOGGLE    LED 
    for I = 1 to length
            pause 1
     if pushbutton = 0 then  gosub buttonpress
    next        
    goto main
    end
    
    
    buttonpress:
        
        pause 10                            'debounce switch press
        if length = 100 then  length = 500
        if length = 500 then  length = 100
            
        do while pushbutton = 0             'wait for switch to be released
            pause 5
        loop 
        
        pause 10                            'debounce switch release
        
    return

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    Quote Originally Posted by richard View Post
    your not giving us much to work on , but from the data sheet and if you are using INT/int on gp2 then



    if you want more help post your code and a sketch/schematic of your setup
    Ah....so the interrupt pin must to be GP2 (pin 5)? I can't use any other pin? And "OPTIONREG.6 = 0" would need to be added to my code, not changed in the .bas define files? I shouldn't have to modify anything in the .bas files right?

    My code thus far looks exactly likes exactly like the example, but I changed the LED1 var to GPIO.0. I'm following the same schematic as well (going to different I/Os), but I haven't made it that far because PBP won't let me compile.

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: how to use INTERRUPT?

    Quote Originally Posted by EarlyBird2 View Post
    ON INTERRUPT waits for the pause to complete before executing the interrupt routine. To get over this issue use short pauses in a loop. Here is a non interrupt option.
    Thanks! That does solve my immediate problem, but I'm planning on doing more complex stuff with the "length" var. Ultimately, I want the LED to continue blinking uninterrupted while the button is being pressed. I'm doing it one piece at a time, but ultimately the blinking LED will be in sync with a PWM, and the button will be a tap tempo to control the frequency of the PWM. The DT Instant Interrupts looks to be exactly what I need for this project. It's got the button. It's got the LED. ANd it's got the timer.
    Last edited by keithv; - 8th August 2014 at 18:14.

Similar Threads

  1. Interrupt
    By gunayburak in forum General
    Replies: 1
    Last Post: - 26th December 2012, 06:19
  2. Need help with interrupt
    By mbox in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th August 2010, 16:48
  3. RX interrupt
    By Arciure in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 5th March 2010, 17:23
  4. I need help with Interrupt
    By wildbilly in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st September 2007, 19:16
  5. Replies: 0
    Last Post: - 27th August 2004, 07:20

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