Interrupts

From Mech
Revision as of 22:25, 25 December 2007 by LIMS (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The PIC allows interrupts to occur on the basis of a lot of different triggering events. I've only tried out a few of them.

The most useful is a timed interrupt which can be used as a servo routine to control motors and read sensors on a regular schedule, independent of the logic of your program.

Any of the four timers can be used to create regular interrupts, but we'll use Timer 2 which is the most flexible. We'll set up Timer 2 to overflow/restart at 16KHz (which is used for PWM timing) and to call an interrupt every 16th overflow, so that we have repeating interrupts at 1mS intervals:

setup_timer_2(T2_DIV_BY_4, 78, 16);

To enable interrupts we need these two statements:

enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

Then, at 1mS intervals, the interrupt service routine (ISR) labeled #INT_TIMER2 will be invoked. As shown in the sample code all that happens in the interrupt service routine is that pin D7 is flashed high for 10uS (to watch it on a scope) and the 32-bit integer msecs is incremented to keep track of time.

We could do a lot more in the ISR, but it's a good idea to keep it as brief as possible, especially for a rapidly repeating ISR like this one. Keep in mind that an interrupt may occur between any two instructions in your main program. So, a variable like msecs that is written by the ISR can change unexpectedly.

You can also trigger an interrupt on a high-to-low or low-to-high transition of a digital input pin (INT0, INT1, or INT2). See InterruptExternal.c