Use the interrupt code generated for your exact PIC. It takes care of differences between PIC16, PIC18 and Positron16. Do not mix the wrapper from one family with another.
Context means the processor's working values that must be saved while an interrupt runs and restored afterward. Keep the generated protection in place.
The PIC Templates folder contains basic ISR setups for tested PIC devices. Use a matching example to see how the interrupt declaration, initialization and handler fit together. Framework contains the complete working examples for the USB use cases. Choose the right starting files.
Select ISR in the tool. For a new program, use the complete Integration listing. Use the ISR tab as a reference when merging USB into an existing program; do not paste the handler twice. Selecting ISR changes library behavior; it does not install your application's vector or enable every interrupt gate automatically.
Install the handler and configure its source, priority and context before allowing interrupts globally. Call USB_Init(), enable the USB source with USB_EnableIRQ(), and enable the required global/peripheral gates when the handler and application state are ready. If other interrupts already run, merge the generated setup into your existing interrupt arrangement.
On PIC18, the wrapper preserves the context used by the USB code:
' Use the generated On_Hardware_Interrupt declaration and program layout.
' Main execution must not fall through into this label.
AppISR:
Context Save FSR0L, FSR0H, FSR1L, FSR1H, TBLPTRL, TBLPTRH, TBLPTRU, TABLAT, PRODL, PRODH
USB_ISR()
' Handle and acknowledge other enabled interrupt sources here.
Context Restore
On PIC16F/LF145x, use the generated shared interrupt wrapper:
AppISR:
Context Save
USB_ISR()
' Handle and acknowledge other enabled interrupt sources here.
Context Restore
Keep the generated On_Hardware_Interrupt declaration and startup gates. The enhanced PIC16 hardware preserves its shadow context, and Positron's managed Context Save tracks compiler working storage used by called procedures. Do not replace this wrapper with a bare assembly interrupt return. The USB source is PIE2.2 / PIR2.2; the generated startup also enables the peripheral and global interrupt gates after initialization. Observe the silicon-revision restriction in PIC16F/LF145x notes. These PIC16s have a 16-level hardware return stack. For initial tests, call the USB commands directly from the main loop and keep other interrupt handlers short. Several layers of your own procedure calls, compiler helpers and an interrupt can exhaust that stack; check the final compiled call depth before adding deeply nested application code.
On Positron16, use the selected device's normal vectored handler, for example:
Isr USB1Interrupt
USB_ISR()
EndIsr
Use the actual vector generated for your target. Normal Isr/EndIsr manages CPU context; the library preserves its additional scratch and table-access state. Do not add a PIC18 Context Save wrapper or use an UnHandled ISR variant.
USB_ISR() checks the USB source enable and pending flag internally. Calling it from a shared PIC18 or PIC16 handler is therefore acceptable when another source caused entry. You must still handle that other source yourself. Do not call USB_ISR() from a periodic timer instead of the USB interrupt vector.
Keep application work outside the handler. Do not call packet APIs, blocking waits, MSC file handlers or the network server from an ISR. Another interrupt can set an application flag; the main loop can act on it. Keep other handlers brief and preserve their context too.
USB_EnableIRQ() returns USB_OK with USB_SERVICE_MODE = 1, or USB_NOT_SUPPORTED in polling mode. It enables the USB source, not the whole interrupt system. USB_DisableIRQ() masks that source and leaves USB_Result unchanged. It does not detach. If disabled for more than a brief protected operation, USB still needs foreground servicing.
Created with the Personal Edition of HelpNDoc: Make Help Documentation a Breeze with a Help Authoring Tool