Although you can understand CAN controller interrupts and flags without a second CAN device, you will need one to experiment with interrupt- driven software. I use an inexpensive Microchip
CAN BUS analyzer tool (Figure 1). This device accepts
differential CAN bus or logic-level signals, and its PC
software transmits and receives data via a USB
connection.
My investigations use a true differential bus such as
the one shown in Figure 4 in the first Nuts & Volts article
in this series (December 2016). For commercial work,
better — and expensive — analyzers can purposely create
bus errors, record and play back bus frames, and so on.
Interrupts
Programmers often write code that runs in a loop and
regularly tests a flag bit or I/O pin to determine whether a
device needs attention and to take action if it does. The
period between these software tests might range from a
few milliseconds to hundreds of seconds depending on
other tasks an MCU must perform. People and machines
often require an almost-instant response, so most MCUs
incorporate one or more interrupts. When an MCU
receives an interrupt signal, it immediately stops any code
currently running and transfers control to a separate
program called an interrupt service routine (ISR). The ISR
code disables the interrupt request input, runs code
associated with the interrupting device, re-enables the
interrupt input, and returns program control to the main
program. Figure 2 illustrates the program flow along a
time axis. Because interrupts may occur at any time, ISRs
This final article explains how to use the interrupts and flags available in an
MCP2515 CAN controller IC, and describes how device-to-device
acknowledgments work on the bus. The acknowledgment bit in a CAN
frame plays a key part in successful communications and identification of
errors.
Take a CAN Bus
for a Spin
By Jon Titus KZ1G
Post comments on this article and find any associated files and/or
downloads at www.nutsvolts.com/magazine/article/April2017_CAN-
Bus_Controller-Interrupts-Flags.
April 2017 29
CAN controller
interrupts and flags
give us an edge.
Part 3
FIGURE 1. A
Microchip CAN
bus analyzer
comes with PC
software that
displays bus
communications
and sends CAN
frames. Courtesy
of Microchip
Technology.
FIGURE 2. Timing diagram for an interrupt service routine
(ISR). An interrupt can occur at any time, which can lead to
debugging problems. Aim to keep ISRs short, and eliminate
blocking code and calls to non-reentrant code.