PDA

View Full Version : Can I have interupts on any pin ?



Bonxy
- 10th March 2010, 14:10
Hi All

1. Can I have interupts work on any pin ? (so that i can detect when a pin changes state)

2. Can i have multiple interupts ? (one for each pin so that i can trigger diffrent iterupt handlers depending on which pin changes state)

Anyone know how to do this (on 18f4550)

Thx

rsocor01
- 10th March 2010, 14:26
Bonxy,

Yes, you can have multiple interrupts. Can you have an interrupt work on any pin on a PIC18F4550? I don't know about that. But can you create a routine where you check the status of the pins that you want and then if a change is made goto to a sub-routine? Something like



IF PORTX.0 = 1 THEN GOTO SUBROUTINE0
IF PORTX.1 = 1 THEN GOTO SUBROUTINE1
.......



Robert

sayzer
- 10th March 2010, 14:45
Use RB.0 for interrupt pin and connect as many normal pin as you want to this pin via a diode.

RB0 will interrupt with any of the pins; you will check the status of the pins in interrupt routine.

Thus, if you connect 10pins with RB0, then you will have 10 interrupt pins.

____________________________________

Acetronics2
- 10th March 2010, 14:50
Hi, Bonxy

You always can use the old method : use an " OR " gate to trigger one interrupt pin of the device.
after, read ports and just check your inputs as rsocor01 tells, hoping they didn't change state in between ... ( possible issue ... but ...)

note a couple of MCP 23016 or 23S17 ( Port expanders ) could help a lot here ...

the question is here : really need it ???

Alain

Charles Linquis
- 10th March 2010, 23:59
Or - if you can tolerate a little latency -

You can use a fast timer interrupt (say 1mSec) and check the status of any pin in that interrupt.

Bruce
- 11th March 2010, 00:15
Can I have interupts on any pin ?
No. You can't have interrupts on any pin. Only pins that have some type of associated interrupt will cause a hardware interrupt.

Testing inputs pins for high or low logic states is not a hardware interrupt.

Charles Linquis
- 11th March 2010, 03:06
This is an example of what I mentioned earlier.

I'll call it FAKE INTERRUPT

It does have a latency of a little over a millisecond, but often it is plenty good enough to do what you need done. You can make the interrupt faster if you wish - but remember each time you enter/leave an interrupt, you are stealing quite a few processor cycles.


This code will check the status of any pin once per millisecond. It was written for an 18Fxxxx at 40Mhz. This example is checking the status of PORTC.0.




Changed VAR BYTE
OldPort VAR BYTE

T0CON = %10001000 ; turn it on, 16 bits, no prescaler (osc/4)

CLEAR


'----------------- DT_INTS section ----------------------------

INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _FakeInt, PBP, yes
endm
INT_CREATE
ENDASM


Goto OverInt

'------------------ Timer 0 interrupt handler-----------------------------------


FakeInt:

TMR0H = $D8 ; Preload depends on clk speed
TMR0L = $F7 ; One millesecond timeout at 40Mhz.
; Always load HIGHBYTE FIRST

Changed = PortC ^ OldPort
IF Changed.0 = 1 THEN ; Pick your pin
OldPort = PortC
;;;; Whatever you want to do when Port bit changes
ENDIF

@ INT_RETURN


Overint:

OldPort = PortC ; Init the var

@ INT_ENABLE TMR0_INT


NormalProgramLoop:



Goto NormalProgramLoop

END

Bruce
- 11th March 2010, 14:19
Here's a schematic for 13 pins with interrupts using just two interrupt-on-change pins.

sayzer
- 11th March 2010, 15:43
Here's a schematic for 13 pins with interrupts using just two interrupt-on-change pins.

At Post#3, I was trying to explain the same method.

--------------------------

Bruce
- 11th March 2010, 15:51
At Post#3, I was trying to explain the same method.

That's what reminded me I had the schematic...;o)

Bonxy
- 11th March 2010, 22:14
WoW guys & gals, im overwhelmed with the response to this, thank you all very much.
Some of your ideas I have considdered, like a diode to the B0 pin, but I dont think that would work for my app, I need to have as many pins as input as possible which i think is about 33 at most on the 18f4550 (and 2 pins could fire at the same time), I will take a closer look at some of your suggestions on my day off work, it looks like some of it involves assembler which I dont get :-(, but Im sure I will pick something out of your ideas :-), Im just wondering if just polling the pins would be fast enough @ 20mhz, I'm gonna give all your ideas a try this w/end and see what works, thx

Bonxy
- 11th March 2010, 22:21
Just an afterthought,
do all pic micro ports/pins have internal pull up resistors ?, i've been looking at the 18f4550 datasheet but cant tell ?, maybe i'm just stupid or tired lol :-)'

Thanks

aratti
- 11th March 2010, 22:41
No, internal pullups applys only to portB.

Al.

sayzer
- 12th March 2010, 08:51
No, internal pullups applys only to portB.

Al.

Some other PICs have pull-ups on PORTA, too. I was not aware of this fact until recently.
Edit: For example, 16F630.


.... (and 2 pins could fire at the same time....

What does it matter?
If one or the other interrupts, you will get into interrupt routine anyway whether two interrupts occur at the same time or not (which I think is very close to impossible !)

While you are in interrupt routine, check the status of all pins, then act based on each of their tasks.

Something like >


RB0_Int: 'we are now in interrupt routine.
A = 0
B = 0
C = 0
D = 0
..
....
.......
IF Pin1 = 0 THEN A = 1 ' Pin1 seems to be interrupted.
IF Pin2 = 0 THEN B = 3 'Ohh, Pin2 also seems to interrupted.
....
.......

PMController
- 12th March 2010, 14:21
I Found great tutorial USING THE PIC EXTERNAL INTERRUPT on all pin

www.precision.net.in/picbasic/PIC_INT.PDF

.

Acetronics2
- 12th March 2010, 14:36
Hi, PMC

Looks your site is down ...

Could you check the link ???

Alain

PMController
- 12th March 2010, 14:42
Its not my site. but working for me.

Acetronics2
- 12th March 2010, 14:49
The message is :

" Internet explorer can't display the page "

no connectivity problems to others sites ...

Alain

mackrackit
- 12th March 2010, 15:09
Alain,
Use Fire Fox.

Bonxy
- 17th March 2010, 13:04
Well guys

Thanks for all your help, I've found that @20mhz its probably fast enough to just poll all the pins to check if they have changed state (this may change as I add more pins to my project), as for the pull-ups, I think im going to go with external resistor packs.

I have further questions (descriptors) , but I think its best to post in another thread, so if you are bored please take a look and help if you can :)

Thanks again

Mike, K8LH
- 17th March 2010, 15:04
Bonxy,

I don't see your other thread but I wanted to comment that you're correct in that you should be able to poll push button switches without any problem, even at relatively slow clock speeds, since switches produce a very slow and very easy to capture signal.

If you need lots of switches (you mentioned up to 33) then you might also consider Charlieplexing or multiplexing them. That is, organize them into columns and poll one column or row of switches at a time. You could Charlieplex 30 switches using just 6 I/O pins (and 30 diodes), or 42 switches using 7 I/O pins (and 42 diodes), or you could multiplex 32, 35, or 36 switches using just 12 I/O pins in a 4x8, 5x7, or 6x6 matrix, respectively.

On the other hand if you want to put the mcu to 'sleep' while waiting for a switch press then using one I/O pin per switch with diode 'OR' wiring to an IOC pin makes perfect sense.

Good luck on your project. Regards, Mike

Bonxy
- 17th March 2010, 15:19
Hi Mike

Thanks for that info, i never heard of "Charlieplexing" before, you can learn new word every day in electronics :p

Ps: I put my other thread in the USB section if you fancy a crack at it!