PDA

View Full Version : Multiple External Interupts



w7ami
- 31st May 2007, 17:48
Help!

I want to use multiple external interupts and perform different functions depending upon which interupt was triggered. From reading the green book and the product manual I can't see how to do this. Can someone give me a push in the right direction.

I am using a PIC18F4620.

Terry

skimask
- 31st May 2007, 18:08
Help! I want to use multiple external interupts and perform different functions depending upon which interupt was triggered. From reading the green book and the product manual I can't see how to do this. Can someone give me a push in the right direction. I am using a PIC18F4620.
Terry
If you use the 'On Interrupt' function, when you get into the 'On Interrupt' loop, you have to check the individual interrupt flags that you've enabled.
In the case of using TMR0-overflow interrupt...
1) You set up the TMR to run at whatever rate you want (timer prescaler, timer source, etc)
2) Enable global interrupts
3) enable the timer interrupt
4) When the overflow interrupt fires (i.e. tmr0 counted from 255 and rolled over to 0), 'On Interrupt' fires...
5) The code inside the 'On Interrupt' looks at the TMR0-OverFlow Interrupt flag. If it's set (or reset depending on which interrupt you're using), that particular interrupt caused the interrupt.
6) Then (most likely) you have to reset the particular interrupt flag that you checked
7) Execute your interrupt code accordingly
8) RESUME your normal code

What, in particular, were you lookingfor? Just playing around?
I use 'On Interrupt' a lot, only because I believe I understand it well enough to overcome it's limitations.
There are a couple of much faster, more flexible interrupt routines on this forum. They deal somewhat with assembly, macros, etc. They aren't that hard to use. I believe they are the way to go if you're looking for something time critical.

w7ami
- 31st May 2007, 18:21
Thanks Skimask,

I think I'm good now. I just needed to enable INTCON1 and INTCON2. Somehow I missed them. I have some test code up and working now.

What I doing is not super time critical so I am doing everything I can to stay away from assembly. The only assembly I can do is with a soldering iron, screw driver, wrench....

Thanks also for the hints on TMR0 I'll be needing that soon.

Terry

skimask
- 31st May 2007, 18:34
Thanks Skimask,
I think I'm good now. I just needed to enable INTCON1 and INTCON2. Somehow I missed them. I have some test code up and working now.
What I doing is not super time critical so I am doing everything I can to stay away from assembly. The only assembly I can do is with a soldering iron, screw driver, wrench....
Thanks also for the hints on TMR0 I'll be needing that soon.
Terry

In the case of TMR0...registers to note:

INTCON.7 = 1 'enable ALL interrupts
INTCON.6 = 1 'enable ALL peripheral interrupts
INTCON.5 = 1 'enable TMR0 OVERFLOW interrupt
INTCON.2 = 1 'TMR0 OVERFLOWed, so it interrupted, must be cleared manually after your program has recognized the interrupt (INTCON.2 = 0)
INTCON2.2 = 1 'sets TMR0 to high priority, not really needed, but useful to note
T0CON...all bits for controlling TMR0 (see the datasheet)

w7ami
- 31st May 2007, 22:56
Just in case someone wants to see how to do multiple hardware interupts, here is my quick and dirty program.

;************************************************* *******
; PIC18F4620
DEFINE OSC 40
; Setup Ports
TRISB = %11111111 ; Port B all inputs
TRISD = %00000000 ; Port D all outputs
DTR var INTCON.0 ; alias to intcon
DSR var INTCON2.0 ; alias to intcon1
ACC var INTCON3.0 ; alias to intcon2
INTCON = %10010000 ; Enable External Interupt INT0

INTCON2 = %10110000 ; INTCON2.7 = All pull up are disabled
; INTCON2.6 = INT0 trigger on falling edge
; INTCON2.5 = INT1 trigger on rising edge
; INTCON2.4 = INT2 trigger on rising edge
; INTCON2.3 = Not used
; INTCON2.2 = TMR0 low priority
; INTCON2.1 = Not used
; INTCON2.0 = Port change priority
INTCON3 = %01011000 ; INTCON3.7 = INT2 Low priority
; INTCON3.6 = INT1 Hi priority
; INTCON3.5 = Not used
; INTCON3.4 = INT2 external enabled
; INTCON3.3 = INT1 external enabled
; INTCON3.2 = Not used
; INTCON3.1 = INT2 Flag (Read Only)
; INTCON3.0 = INT1 Flag (Read Only)

On Interrupt Goto INTERUPTS ; Define interrupt handler
;************************************************* ********
Loop:
PORTD.0 = 0 ; Turn all LEDs on
PORTD.1 = 0
PORTD.2 = 0
PORTD.3 = 0
Goto loop ; Do it forever
;************************************************* ********
; Interrupt handler
Disable ; No interrupts past this point
INTERUPTS:
if INTCON.1 = 1 then
PORTD.0 = 1 ; Turn LED1 OFF
Pause 500 ; Wait .5 seconds
INTCON.1 = 0 ; Clear interrupt flag
endif
if INTCON3.0 = 1 then
PORTD.1 = 1 ; Turn LED2 OFF
Pause 500 ; Wait .5 seconds
INTCON3.0 = 0 ; Clear interrupt flag
endif
if INTCON3.1 = 1 then
PORTD.2 = 1 ; Turn LED2 OFF
Pause 500 ; Wait .5 seconds
INTCON3.1 = 0 ; Clear the interupt flag
Endif
RESUME ; Return to main program
ENABLE
;************************************************* ********
END

Terry