A brief about CAN bus. The modern automobile system may have as many as 70 electronic control unit (ECU) for various subsystems. CAN Bus (Controller Area Network) is the bus that is usually used to connect/wiring many controller devices on automobile system. It only requires 2 wire: CANH and CANL.
Typical CAN Bus architecture is shown below: (common termination res value is 100 Ohm or 120 Ohm)
CAN is a message-based protocol, it has two messages format: standard/base frame format and extended frame format. And there’re four CAN frame types: data frame, remote frame, error frame, and overload frame. CAN standard frame format is shown below: (reference from wikipedia.org)
The advantages of using CAN bus is speed (up to 1Mbps), flexible, low cost hardware, reliable on noisy environment, and less sensitive to electromagnetic interference.
PIC24F family doesn’t have CAN module, so to communicate with other controller on CAN Bus, additional module is needed. One of the choice is MCP2515 CAN controller.
CAN Bus requires 2 wire CANH and CANL which is a differential signal. The differential signal is generated by TJA1050 CAN transceiver chip, which already included on MCP2515 board. A twisted pair shielded cable should be used since it is recommended for differential signal. As for the type of connector being used, there’s no specific standard, but the cheap MCP2515 board contain two connectors that is commonly used for RS-485.
PIC24F communicates with MCP2515 board using SPI interface. MCP2515 has its own SPI instruction defined. Basically setting up the CAN baudrate/bit timing, check/clear status flag, load tx buffer, read rx buffer, everything is done by using SPI instruction.
In this post I’m going to share my experience demonstrating two devices communicating on CAN Bus. One device will act as main controller, while the other will act as node controller. A scheme like this is often found where one device at one node location retrieves data from sensor then send it to a bus, then a main controller located far away will receive it. When we hear “node”, then our intuition tell us that it could be a device with small footprint (well it must not be), therefore as a node controller I’ve ATtiny85 (small indeed only has 8 pin), it’s role is to read humidity & temperature data from SHT30, then send the data to CAN bus (to MCP2515). At the main controller sit PIC24F, that read sensor data from CAN bus (from MCP2515) then send it to PC running simple LabVIEW vi for display and monitoring. A picture speaks thousand than a word:
Tiny doesn’t has peripheral as caliber as PIC24F, but it’s just enough for the task. Tiny need to communicate with SHT30 through I2C interface, and it also need to talk to MCP2515 with SPI interface. Tiny has USI (Universal Serial Interface) that can be used for SPI. As for the I2C, we can use bit banging.
There’s an issue that should be addressed here, it is when we trying to connect a 3.3V to a 5V system. At the main controller, we need to connect 3.3V PIC24F to 5V MCP2515 (due to TJA1050 transceiver that need 5V supply). And at the node, we also need to connect 3.3V ATtiny to 5V MCP2515 (note: Tiny actually can operate at 5V, but I just prefer it operated at 3.3V along with SHT30). This issue is not a problem with PIC24F, eventhough it work at 3.3V but a direct SPI connection with 5V MCP2515 is okay, see below illustration:
It will work given that VOH of PIC24F > VIH min of MCP2515, and SDI pin must choose 5.5V compatible pin (the grey-shaded pin in PIC24F datasheet).
At the node, SPI connection between Tiny and MCP2515 may also work with direct connect scheme, connection from MCP2515 SO pin to Tiny DI pin will use resistor voltage divided because Tiny DI pin is not 5V compatible in this case. As for SS, USCK, and DO let’s use IC comparator, that can do the trick to level shift from 3.3V to 5V. I’m going to use 74HC32 Quad OR Gate as substitute, since I don’t have IC comparator. The scheme is shown below:
At CAN node controller: (transmitter)
At CAN main controller: (receiver)
PC running LabVIEW for displaying real time value of humidity and temperature, also plot those sensor values on charts:
See demonstration video below: