Справочник Пользователя для Microchip Technology SW006021-2
How To’s
2012 Microchip Technology Inc.
DS52053B-page 53
3.4.6
Interrupts
Interrupt and interrupt service routine questions are discussed in this section.
3.4.6.1
HOW DO I USE INTERRUPTS IN C?
First, be aware of what interrupt hardware is available on your target device. Baseline
PIC devices do not implement interrupts at all; mid-range devices utilize a single inter-
rupt vector, and PIC18 devices implement two separate interrupt vector locations and
use a simple priority scheme.
PIC devices do not implement interrupts at all; mid-range devices utilize a single inter-
rupt vector, and PIC18 devices implement two separate interrupt vector locations and
use a simple priority scheme.
In C source code, a function can be written to act as the interrupt service routine by
using the interrupt qualifier, see Section 5.9.1 “Writing an Interrupt Service Rou-
tine”. Such functions save/restore program context before/after executing the function
body code and a different return instruction is used, see Section 5.9.3 “Context
Switching”. There must be no more than one interrupt function for each interrupt vec-
tor implemented on the target device.
using the interrupt qualifier, see Section 5.9.1 “Writing an Interrupt Service Rou-
tine”. Such functions save/restore program context before/after executing the function
body code and a different return instruction is used, see Section 5.9.3 “Context
Switching”. There must be no more than one interrupt function for each interrupt vec-
tor implemented on the target device.
Aside from the interrupt qualifier, the function prototype must specify no parameters
and a void return type. If you wish to implement the low priority interrupt function on
PIC18 devices, use the low_priority keyword as well as the interrupt qualifier.
and a void return type. If you wish to implement the low priority interrupt function on
PIC18 devices, use the low_priority keyword as well as the interrupt qualifier.
Code inside the interrupt function can do anything you like, but see Section 3.6.6
“How Can I Make My Interrupt Routine Faster?” for suggestions to enhance
real-time performance.
“How Can I Make My Interrupt Routine Faster?” for suggestions to enhance
real-time performance.
Prior to any interrupt occurring, your program must ensure that peripherals are cor-
rectly configured and that interrupts are enabled, see Section 5.9.4 “Enabling Inter-
rupts”. On PIC18 devices, you must specify the priority of interrupt sources by writing
the appropriate SFRs.
rectly configured and that interrupts are enabled, see Section 5.9.4 “Enabling Inter-
rupts”. On PIC18 devices, you must specify the priority of interrupt sources by writing
the appropriate SFRs.
3.4.7
Assembly Code
This section examines questions that arise when writing assembly code as part of a C
project.
project.
3.4.7.1
HOW SHOULD I COMBINE ASSEMBLY AND C CODE?
Ideally, any hand-written assembly should be written as separate routines that can be
called. This offers some degree of protection from interaction between compiler-gener-
ated and hand-written assembly code. Such code can be placed into a separate
assembly module that can be added to your project, see Section 5.12.1 “Integrating
Assembly Language Modules”.
called. This offers some degree of protection from interaction between compiler-gener-
ated and hand-written assembly code. Such code can be placed into a separate
assembly module that can be added to your project, see Section 5.12.1 “Integrating
Assembly Language Modules”.
If necessary, assembly code can be added in-line with C code using either of two meth-
ods, see Section 5.12.2 “#asm, #endasm and asm()”. The code added in-line should
ideally be limited to instructions such as NOP, SLEEP or CLRWDT. Macros are already
provided which in-line all these instructions, see Appendix A. “Library Functions”.
More complex in-line assembly that changes register contents and the device state can
cause code failure if precautions are not taken and should be used with caution. See
Section 5.7 “Register Usage” for those registers used by the compiler.
ods, see Section 5.12.2 “#asm, #endasm and asm()”. The code added in-line should
ideally be limited to instructions such as NOP, SLEEP or CLRWDT. Macros are already
provided which in-line all these instructions, see Appendix A. “Library Functions”.
More complex in-line assembly that changes register contents and the device state can
cause code failure if precautions are not taken and should be used with caution. See
Section 5.7 “Register Usage” for those registers used by the compiler.