Summary of Connect CAN-SPI with PIC Controller
Summary: The article documents mikroC PRO for PIC's CANSPI library for interfacing PIC microcontrollers with mikroElektronika's CANSPI Add-on boards (MCP2515/MCP2510) over SPI. It describes CAN features, message formats, required global pins, library routines (initialize, mode, baudrate, masks/filters, read/write), usage requirements, examples, and notes that SPI-based CANSPI is slower than native CAN and requires proper termination and cabling.Parts used in the CANSPI Project:
- PIC microcontroller with SPI module
- mikroElektronika CANSPI Add-on board (MCP2515 or MCP2510)
- SPI interface (hardware SPI module on MCU)
- Chip Select connection (CanSpi_CS pin)
- Reset connection (CanSpi_Rst pin)
- Chip Select direction pin (CanSpi_CS_Direction)
- Reset direction pin (CanSpi_Rst_Direction)
- Shielded twisted pair CAN cable with proper termination
- Power supply for MCU and CANSPI board

- Consult the CAN standard about CAN bus termination resistance.
- An effective CANSPI communication speed depends on SPI and certainly is slower than “real” CAN.
- The library uses the SPI module for communication. User must initialize appropriate SPI module before using the CANSPI Library.
- For MCUs with two SPI modules it is possible to initialize both of them and then switch by using the SPI_Set_Active routine.
- CANSPI module refers to mikroElektronika's CANSPI Add-on board connected to SPI module of MCU.
Library Dependency Tree

External dependencies of CANSPI Library
| The following variables must be defined in all projects using CANSPI Library: | Description : | Example : |
|---|---|---|
extern sfr sbit CanSpi_CS; |
Chip Select line. | sbit CanSpi_CS at RC0_bit; |
extern sfr sbit CanSpi_Rst; |
Reset line. | sbit CanSpi_Rst at RC2_bit; |
extern sfr sbit CanSpi_CS_Direction; |
Direction of the Chip Select pin. | sbit CanSpi_CS_Direction at TRISC0_bit; |
extern sfr sbit CanSpi_Rst_Direction; |
Direction of the Reset pin. | sbit CanSpi_Rst_Direction at TRISC2_bit; |
Library Routines
- CANSPISetOperationMode
- CANSPIGetOperationMode
- CANSPIInitialize
- CANSPISetBaudRate
- CANSPISetMask
- CANSPISetFilter
- CANSPIRead
- CANSPIWrite
CANSPISetOperationMode
| Prototype | void CANSPISetOperationMode(char mode, char WAIT); |
|---|---|
| Returns | Nothing. |
| Description | Sets the CANSPI module to requested mode. Parameters :
|
| Requires | The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing
|
CANSPIGetOperationMode
| Prototype | char CANSPIGetOperationMode(); |
|---|---|
| Returns | Current operation mode. |
| Description | The function returns current operation mode of the CANSPI module. Check CANSPI_OP_MODE constants or device datasheet for operation mode codes. |
| Requires | The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing (2)
|
CANSPIInitialize
| Prototype | void CANSPIInitialize( char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CANSPI_CONFIG_FLAGS); |
|---|---|
| Returns | Nothing. |
| Description | Initializes the CANSPI module. Stand-Alone CAN controller in the CANSPI module is set to:
|
| Requires | Global variables :
must be defined before using this function. |
| Example |
C source (3)
|
CANSPISetBaudRate
| Prototype | void CANSPISetBaudRate( char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CANSPI_CONFIG_FLAGS); |
|---|---|
| Returns | Nothing. |
| Description | Sets the CANSPI module baud rate. Due to complexity of the CAN protocol, you can not simply force a bps value. Instead, use this function when the CANSPI module is in Config mode.SAM, SEG2PHTS and WAKFIL bits are set according to CANSPI_CONFIG_FLAGS value. Refer to datasheet for details.Parameters:
|
| Requires | The CANSPI module must be in Config mode, otherwise the function will be ignored. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing (4)
|
CANSPISetMask
| Prototype | void CANSPISetMask(char CANSPI_MASK, long val, char CANSPI_CONFIG_FLAGS); |
|---|---|
| Returns | Nothing. |
| Description | Configures mask for advanced filtering of messages. The parameter value is bit-adjusted to the appropriate mask registers.Parameters:
|
| Requires | The CANSPI module must be in Config mode, otherwise the function will be ignored. See CANSPISetOperationMode. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing (5)
|
CANSPISetFilter
| Prototype | void CANSPISetFilter(char CANSPI_FILTER, long val, char CANSPI_CONFIG_FLAGS); |
|---|---|
| Returns | Nothing. |
| Description | Configures message filter. The parameter value is bit-adjusted to the appropriate filter registers.Parameters:
|
| Requires | The CANSPI module must be in Config mode, otherwise the function will be ignored. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing (6)
|
CANSPIRead
| Prototype | char CANSPIRead(long *id, char *rd_data, char *data_len, char *CANSPI_RX_MSG_FLAGS); |
|---|---|
| Returns |
|
| Description | If at least one full Receive Buffer is found, it will be processed in the following way:
Parameters:
|
| Requires | The CANSPI module must be in a mode in which receiving is possible. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing (7)
|
CANSPIWrite
| Prototype | char CANSPIWrite(long id, char *wr_data, char data_len, char CANSPI_TX_MSG_FLAGS); |
|---|---|
| Returns |
|
| Description | If at least one empty Transmit Buffer is found, the function sends message in the queue for transmission. Parameters:
|
| Requires | The CANSPI module must be in mode in which transmission is possible. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
| Example |
Code listing (8)
|
For more detail: Connect CAN-SPI with PIC Controller
Quick Solutions to Questions related to CANSPI Project:
- What library provides CAN-over-SPI support for PIC in mikroC PRO?
The CANSPI library in mikroC PRO for PIC provides drivers for mikroElektronika's CANSPI Add-on boards (MCP2515 or MCP2510) via SPI. - Which MCU feature is required to use the CANSPI library?
An MCU with the SPI module is required. - What global variables must be defined to use the CANSPI library?
CanSpi_CS, CanSpi_Rst, CanSpi_CS_Direction, and CanSpi_Rst_Direction must be defined. - How do you set the CANSPI module operation mode?
Use CANSPISetOperationMode(mode, WAIT) where mode is a CANSPI_OP_MODE constant and WAIT controls blocking verification. - Can you set the baud rate while the CANSPI module is running normally?
No. CANSPISetBaudRate must be called while the module is in Config mode; otherwise it is ignored. - How do you configure message filtering and masking?
Use CANSPISetFilter and CANSPISetMask functions while the module is in Config mode to set filter and mask registers with CANSPI_CONFIG_FLAGS. - What does CANSPIRead return when a message is received?
It returns 0xFF if a Receive Buffer is full (message received) and 0 if nothing is received. - What does CANSPIWrite return if a transmit buffer is available?
It returns 0xFF if at least one Transmit Buffer is available and 0 if all are busy. - Are CAN data rates affected by cable length?
Yes. Higher bitrates like 1 Mbit/s are for short lengths (below ~40m); lower bitrates for longer distances; the standard lowest bitrate is 20 Kbit/s. - Is CANSPI communication as fast as native CAN?
No. Effective CANSPI communication speed depends on SPI and is slower than real CAN.
Related projects
CAN FD Node based on a PIC18 Microcontroller
The introduction sets the stage for an embedded systems project that involves developing a Embedded C CAN FD project using the MCP2517FD...
Communication & InterfacingLAN9662 Gigabit Switch: Optimized for Industrial Ethernet
Introduction It should come as little surprise therefore that one element of what is now more commonly referred to in some circles...
Communication & InterfacingPIC Microcontroller: Fundamentals & Applications for Students
Introduction Microcontrollers are the key components in the embedded systems market. A variety of embedded systems was formed and developed after the...
Communication & Interfacing10BASE-T1S Ethernet: Simplified OEM Design for Auto Devices
Introduction Cars these days are surrounded by technology. They come loaded with all sorts of electronic gadgets and sensors. This project wants...
Communication & InterfacingDesigning a Microcontroller Framework for PC-Based Control of Electrical Appliances
ABSTRACT The preference for personal computers as a platform for designing and implementing control algorithms is growing due to the ease of...
Communication & InterfacingEfficient Multichannel Data Acquisition with USB Connectivity using PIC Microcontroller
1. INTRODUCTION With the rapid advancements in embedded technology, there is a growing demand for a data acquisition system that combines fast...

