PDA

View Full Version : Is it a correct way to deal with an interrupt?



financecatalyst
- 14th November 2009, 20:18
Hi, I am trying to learn about interrupts and have a few 12C671 PICs with me. I don’t want to waste them and then learn so I request few expert opinions before I burn this code into my PIC.
I am trying to create an interrupt in such a way that when GP2 is made high -> PIC should wake up from sleep and execute the code until GP2 is held high and when it goes low – it should go back to having sweet dreams until GP2 is made high again. Following is my code:


'fuses setup
TRISIO = %011111 '// Gpio =5-bit switch inputs - Gpio.5=Output
OPTION_REG = %11000000 '// INTERRUPT ON RISING EDGE OF GP2 + Internal pull ups off
ADCON0.0=0
ADCON1=7
ON INTERRUPT GOTO encode
goto rest

Send:
Send the variables
Goto rest

rest:
while GPIO.2=1
gosub encode
wend
INTCON = %10010000 ' global ints enabled, Ext int on GP2 enabled
@ SLEEP ' put PIC to sleep

encode:
INTCON=0
Prepare the variables with values
GOTO Send


Is the above way a correct one?

Also, I want to ask very briefly about what excatly peripheral interrupts are for or if you can put a link where I can read about them in detail. Thanks

mackrackit
- 15th November 2009, 13:16
Why not learn on a flash PIC? Then when the code is correct....

financecatalyst
- 15th November 2009, 20:51
Thanks Dave..........but this question will still be there in my mind if it is a correct way or not. I know there are many ways to do one thing so I wanted to have an opinion about if the way I choose above is a reliable one. I have never written any code having interrupts myself before.

Charles Linquis
- 15th November 2009, 22:44
Whichever chip you choose, I would never use the

ON INTERRUPT GOTO structure.

This statement not only adds to your code size, it makes it run more slowly
as well, because it has to check (very often) if any of the interrupt flag bits are set. The interrupt response time can also be longer than you expect, since it can only jump to the interrupt routine at the points where it checks for the flags.

On the other hand, Darrel Taylor's Instant Interrupts uses REAL interrupts. Check out that thread. MUCH better than O-I-G, and easy to use as well.