18F4550, How to use multiple External Interupts


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262

    Default 18F4550, How to use multiple External Interupts

    Hi, this is my first post so I will try to keep it brief. Im working on a basic test program to upgrade later to a large program. I have a single interrupt working right now with RB0, I want to use up to 3 external interupts, RB0 - RB2. I'm posting my current code below, I am using MicroCode Studio with a 18F4550. Later I may want to use the USB funtion on this also, but I'm lost on USB right now.

    Code:
    Trisb.0 = 1
    Trisb.7 = 0
    INTCON2.7 = 0' Enable PORTB pullups, and falling edge on B0 interrupt
    On Interrupt Goto myint ' Define interrupt handler
    INTCON = %10010000 ' Enable INT0 interrupt
    loop:
    PORTB.7 = 1 ' Turn LED on
    Goto loop ' Do it forever
    ' ****** Interrupt handler *******
    Disable ' No interrupts past this point
    myint:
    PORTB.7 = 0 ' If we get here, turn LED off
    Pause 500 ' Wait .5 seconds
    INTCON.1 = 0 ' Clear interrupt flag
    Resume ' Return to main program
    Enable
    I would like to perform something that lets me use something like myint0 myint1 myint2 labels. im just using a button right now to trigger the RB0, my next step is to have a 4x4 keypad using a 74922 chip , I can tie the 922's data ready pin to RB0 for the keypad interrupt, that uses 1 interrupt, the 4bit output of the keypad I would like to use with PORTB 4-7 , i can move the last 4 bits to a variable then read the variable as a 4bit number 0-15. but for right now, lets just try to get INT 0-2 working with PORTB 5,6,7 as output leds.each INT should work with Each output seperately.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    Hi,
    When the interrupt fires and the program jumps to myInt, the first thing you do is check the various interrupt request flags (INTCON.0 for INT0, INTCON3.0 for INT1 etc) to determine which one actually caused the interrupt and act accordingly.

    Once you realise the limitations of ON INTERRUPT make sure you look into Darrels DT-INTS routines. Plenty of posts on that on the forum.

    Can't help with the USB I'm afraid, too much voodoo for me ;-)

    /Henrik.

  3. #3
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    So something like this?


    myint:
    if INTCON.0 = 1 then LED1 = 1
    if INTCON2.0 = 1 then LED2 = 1
    if INTCON3.0 = 1 then LED3 = 1
    Resume ' Return to main program
    Enable

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    So something like this?
    Yes, but don't forget to clear the interrupt request flag for the interrupt in question before you RESUME.

    /Henrik.

  6. #6
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    ok thanks, one question though, say im using RB1 and RB2 as I/O's and not interupts, do I have to enable interupts on those pis or do I have to disable interupts on those. if so what do i need to do.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    Interrupts are disabled by default. If you aren't using the interrupt for a pin or peripheral you don't enable it.

  8. #8
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    still cant figure out how to enable all 3 interupts? anyone got a bit of code that uses all 3 int's on a 4550 chip

  9. #9
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    Ok someone else just posted an article with INT's and a response looks very good, first look at my original post code, this works for one interupt, now heres the code that made me interested (if this is part of darrels routines let me know)

    Code:
    INTCON2.6 = 0   'Interrupt on falling edge of INT0
    INTCON2.5 = 1   'Interrupt on rising edge of INT1
    
    ' Here two interrupts are declared. One for INT0 (PortB.0) and one for INT1 (PortB.1). 
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT0_INT,  _RcvCAN,   PBP,  yes
            INT_Handler    INT1_INT,  _DoSomething, PBP, yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    Main:
      Pause 100
      Toggle LED
    Goto Main
    
    RcvCAN:
    ' Put your code here or GOSUB it but make SURE you have a RETURN at the end of the routine so you come back here, otherwise you'll crash.
    @ INT_RETURN
    
    DoSomethingElse:
    Toggle AnotherLED
    @ INT_RETURN
    now if this is darrels way then I can see the ease of use, im not sure why INT0 and INT1 have falling and leading edge differnt but thats ok. I would like to know how to alter my original code to use 3 interupts though?

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: 18F4550, How to use multiple External Interupts

    Hi,
    Yes, this is an example of how to use DT-INTS, if you want more information on it I strongly suggest that you
    A) Check Darrels website and all his examples and
    B) Search the forum because there's been a TON of examples and discussions on these great routines over the years.

    im not sure why INT0 and INT1 have falling and leading edge differnt but thats ok
    That was just to show that you CAN select on which edge the interrupt should fire. You want them both (or all three including INT2) on the falling edge then you set them up that way.

    I would like to know how to alter my original code to use 3 interupts though?
    Using ON INTERRUPT:
    First you set up the three interrupts you want to use and enable them, just like you've done with INT0 in your working example. To determine which of the three interrupts that fired you check the three individual interrupt flags in your interrupt service routine and act accordingly. Before you leave the interrupt service routine you clear the interrupt flag that caused the interrupt. (I thought we covered that earlier in the thread).

    /Henrik.

Similar Threads

  1. Help with interupts
    By Frozen001 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 22nd October 2011, 18:46
  2. External Eeprom - trying to store multiple variables
    By bluesmoke in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 6th April 2010, 02:07
  3. Can I have interupts on any pin ?
    By Bonxy in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 17th March 2010, 15:19
  4. DT Interupts and ICD?
    By ShaneMichael in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th November 2009, 02:02
  5. Multiple External Interupts
    By w7ami in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 31st May 2007, 22:56

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