PDA

View Full Version : 18F4550, How to use multiple External Interupts



wdmagic
- 13th December 2012, 16:41
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.


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.

HenrikOlsson
- 13th December 2012, 18:54
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.

wdmagic
- 13th December 2012, 22:09
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

mackrackit
- 14th December 2012, 04:21
Maybe this will help
http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING

HenrikOlsson
- 14th December 2012, 06:05
So something like this?
Yes, but don't forget to clear the interrupt request flag for the interrupt in question before you RESUME.

/Henrik.

wdmagic
- 15th December 2012, 02:03
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.

HenrikOlsson
- 15th December 2012, 08:37
Interrupts are disabled by default. If you aren't using the interrupt for a pin or peripheral you don't enable it.

wdmagic
- 1st January 2013, 07:57
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

wdmagic
- 5th January 2013, 00:25
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)


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?

HenrikOlsson
- 5th January 2013, 08:37
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 (http://darreltaylor.com/DT_INTS-18/home.html) 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.