devices such as commercial CAN based
valves, sensor modules, and motor controls
have factory-set IDs, or they could have DIP
switches users can set for an address.
Before your application software can
respond to a specific address, you must
load address registers — called filters — with
the ID or IDs you choose for a device.
An eight-bit PIC18F66K80 MCU, for
example, includes a CAN controller that
holds six ID “filters,” so you could create six
IDs for the controller. Why have more than
one address? A designer might assign an
address for commands, another address for
calibration values, and another address for
I/O data. Before a controller saves any data
bytes, the 11-bit ID in the received frame must
match one of the IDs saved in a filter. CAN
controllers also include 11-bit masks that let you
ignore individual filter and ID bits. You would use
mask bits if you want a CAN controller to accept addresses
within a given range. A logic 0 in a mask means the
controller ignores the corresponding filter bit and ID bit. A
logic 1 in a mask bit forces the filter to exactly match the
corresponding ID bit in a received frame.
Figure 6 provides an example for five bits. In examples
a and b, the logic 1 mask bits do not affect the filter bits.
The received ID in a matches the filter bits, so the CAN
controller saves the received data. The b example shows a
mismatch between the filter and the ID bits, so the
controller saves no data. Example c shows a logic 0 at bit
position ID0 in the mask, which means the corresponding
filter bit gets ignored.
Either a logic 1 or a logic 0 at position ID0 in a frame’s
ID causes the controller to save the received data. If you
had set both mask bits ID1 and ID0 to logic 0 (...111002),
what ID range would the controller accept?
For the mask ...111002, the CAN controller could
accept data in frames with four addresses: ...11100;
...11101; ...11110; and ...11111. Unless you need a range
of continuous addresses for a controller, set all mask bits to
logic 1 and set the CAN controller’s ID filters as needed.
(Revision 2 of the CAN standard provides for an additional
18 ID bits — a 29-bit ID — for over 500 million addresses.
This type of addressing goes beyond the scope of this
article.)
Test CANbus Software
Now let’s find out how to transmit and receive CAN
frames. I used a Parallax Propeller MCU on a QuickStart
board for the following examples because I’m familiar with
the Propeller and with its Spin language. You can program
it in C or assembly language too. The Propeller IC
comprises eight identical processor cores (called cogs) that
run independently. One or more cogs can handle CAN
related tasks, while other cogs process results or prepare
transmissions. Although the Propeller does not have a built-in CAN controller, software libraries offer standard CAN
operations, and programmers have created code that
works at data rates as high as 1 Mbit/sec.
The test circuit connected the CAN TX output (pin
P24) to the CAN RX input (P25) on the same Propeller IC.
A 10K ohm resistor from either pin to + 3.3V completed
the circuit (Figure 7). For a loopback test, you do not need
a complete bus as shown earlier in Figure 4. (Many MCU
suppliers connect CAN controllers to specific pins. The
Propeller lets you assign any available pins you choose as
CAN TX and CAN RX.)
The online free Propeller Object Exchange (OBEX) lists
several CAN files with methods that send and receive CAN
frames (go to http://obex.parallax.com/object/745). This
folder includes a test program, “CANbus Loopback
demo.spin” that transmits and receives data frames to test
December 2016 31
FIGURE 4. Circuit diagram for a portion of a CAN bus. Note the 120
ohm termination resistors at each end. Always limit stub lengths to one
foot (0.3 m) or less.
FIGURE 5. A timing diagram for the simultaneous transmission
from the CAN1 controller addressed to a device with
hexadecimal address 4D1 and from the CAN2 controller
addressed to a device with ID 5A0. The frame sent from CAN1
has a higher priority (lower value), so it controls the bus.