Hi,
As far as my skill goes, I would like to explain the basics of interrupt. If I go wrong somewhere I would like the gurus to correct me.I am a 100% self-taught so do not expect much.
Basics of the basics :
If global interrupt is enable and the processor is interrupted (by any external or internal interrupt sources) it jumps to a particular program memory address called the interrupt vector and starts executing the code thereafter. This code is called ISR (interrupt service routine). Please note there can be multiple interrupts enabled so it is the duty of the programmer to take necessary actions by polling the hardware interrupt flags and resetting them too.After you have done your ISR job you can fire the retfie (return from interrupt) instruction. It turns on your Global Interrupt Flag again and throws you back to where you were executing your normal code. Now the normal code was between some task and probably was using the W (working), S (Status) registers. These do get change change in your ISR if you did something more than just set/clear a port. So it is a good idea to save these just as you enter the ISR and restore them before you quit. Other important registers are the bank select / FSR and the PCLATH (sometimes). One important thing to note is that if the global interrupt bit is not turned on, the program does not jump to the ISR but the hardware interrupt flags remain keep setting up. So if you have plenty of time you can just poll this in your regular code and take necessary action. Now which interrupts you want to use and how are setup by the INTCON (Interrupt Control) and other peripheral interrupt registers.
PBP and interrupt :
Now PBP handles the interrupt a little lazily. Whenever an interrupt occurs it just flags it in software and continues (till done) whatever it was doing and then goes to the basic interrupt handler (setup by the define). It then starts executing the basic code from there and when it finds a resume goes back to where it came from. Note the PBP interrupt handler is not at the processor interrupt vector. This means if you were in middle of a pause 2000 statement and an interrupt occured, PBP would complete the pause 2000 statement and then go to the basic interrupt handler. Thus it is unpredictable and slow. However it does make life easy by taking care of the context saving (sensitive registers) automatically. Now PBP itself checks the interrupt status by placing a call to its internal interrupt check routine. This call does take code space and time (however small). It flags the interrupt in software (its own working variable) and turns back the Global bit to 1 again. If you turn-off this bit in your main program PBP would falsely enter into an interrupt condition. So it is recommended in the manual to set INTCON = $80 while entering ISR. That is GIE enable all other reset.
Now the "Enable and Disable" commands are just compiler directives. It instructs the compiler where to put these calls and where not. For example would not like PBP to check for interrupt when you are already inside you basic interrupt routine. So a disable just before this routine would direct the compiler not to place the call. After you have finsihed the ISR you would like to enable it so the enable after the basic ISR. This also mean that you can control your program flow when and when not to be interrupted.
So while choosing interrupt in my progs (BTW this is not my hobby I do it for my living) , I consider the following.
1. Do I really need interrupts ? Is it possible to poll the hardware flags and manage it ?
2. Is the latency of PBP interrupt acceptable, (no fast action than my slowest bit of code) ?
If no.2 is no, I always write my ISR in assembly.
Things to keep in mind and what I practice.
1. Keep the PBP ISR on top of the main program loop.(Just after initiating variables and ports) and use a goto just before it to skip around it.
2. Make sure that the interrupts are re-enabled when existing the PBP ISR by a resume.
3. Keep tasks in little chunks. For eg. doing a repeat/until increment style pause with minimum chunks. (Rather than pausing 2000mS do it 100 times in steps of 20mS)
This post would be incomplete without a mention of Darel Taylor's instant interrupt in basic. Search for it in this forum and appreciate the nice and hard work Darel has done to make our life easy.
I personally handle all my interrupts in asm but PBP gave me the courage and insight to use interrupts effectively.So no big deal.
Looking forward to critics comments..
Regards
Sougata
Bookmarks