CANSPI Library

The SPI module is available with a number of the dsPIC30/33 and PIC24 MCUs. The mikroPascal PRO for dsPIC30/33 and PIC24 provides a library (driver) for working with mikroElektronika's CANSPI Add-on boards (with MCP2515 or MCP2510) via SPI interface.

The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.

Data transfer rates depend on distance. For example, 1 Mbit/s can be achieved at network lengths below 40m while 250 Kbit/s can be achieved at network lengths below 250m. The greater distance the lower maximum bitrate that can be achieved. The lowest bitrate defined by the standard is 200Kbit/s. Cables used are shielded twisted pairs.

CAN supports two message formats:

In the mikroPascal PRO for PIC, each routine of the CANSPI counterpart with identical syntax. For more information on Controller Area Network, consult the CAN Library. Note that an effective communication speed depends on SPI and certainly is slower than “real” CAN.

  Important :

Library Dependency Tree

CANSPI Library Dependency Tree

External dependencies of CANSPI Library

The following variables must be defined in all projects using CANSPI Library: Description : Example :
var CanSpi_CS : sbit; sfr; external; Chip Select line. var CanSpi_CS : sbit at RC0_bit;
var CanSpi_Rst : sbit; sfr; external; Reset line. var CanSpi_Rst : sbit at RC2_bit;
var CanSpi_CS_Direction : sbit; sfr; external; Direction of the Chip Select pin. var CanSpi_CS_Direction : sbit at TRISC0_bit;
var CanSpi_Rst_Direction : sbit; sfr; external; Direction of the Reset pin. var CanSpi_Rst_Direction : sbit at TRISC2_bit;

Library Routines

CANSPISetOperationMode

Prototype

procedure CANSPISetOperationMode(mode: byte; WAIT: byte);

Returns

Nothing.

Description

Sets the CANSPI module to requested mode.

Parameters :

  • mode: CANSPI module operation mode. Valid values: CANSPI_OP_MODE constants (see CANSPI constants).
  • WAIT: CANSPI mode switching verification request. If WAIT = 0, the call is non-blocking. The function does not verify if the CANSPI module is switched to requested mode or not. Caller must use CANSPIGetOperationMode to verify correct operation mode before performing mode specific operation. If WAIT != 0, the call is blocking – the function won’t “return” until the requested mode is set.

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
// set the CANSPI module into configuration mode (wait inside CANSPISetOperationMode until this mode is set)
CANSPISetOperationMode(_CANSPI_MODE_CONFIG, 0xFF);

CANSPIGetOperationMode

Prototype

function CANSPIGetOperationMode(): byte;

Returns

Current operation mode.

Description

The function returns current operation mode of the CANSPI module. Check CANSPI_OP_MODE constants (see CANSPI 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
// check whether the CANSPI module is in Normal mode and if it is do something.
if (CANSPIGetOperationMode() = _CANSPI_MODE_NORMAL) then
begin
  ...
end; 
  

CANSPIInitialize

Prototype

procedure CANSPIInitialize(SJW: byte; BRP: byte; PHSEG1: byte; PHSEG2: byte; PROPSEG: byte; CANSPI_CONFIG_FLAGS: byte);

Returns

Nothing.

Description

Initializes the CANSPI module.

Stand-Alone CAN controller in the CANSPI module is set to:

  • Disable CAN capture
  • Continue CAN operation in Idle mode
  • Do not abort pending transmissions
  • Fcan clock: 4*Tcy (Fosc)
  • Baud rate is set according to given parameters
  • CAN mode: Normal
  • Filter and mask registers IDs are set to zero
  • Filter and mask message frame type is set according to CANSPI_CONFIG_FLAGS value

SAM, SEG2PHTS, WAKFIL and DBEN bits are set according to CANSPI_CONFIG_FLAGS value.

Parameters:

  • SJW as defined in CAN controller's datasheet
  • BRP as defined in CAN controller's datasheet
  • PHSEG1 as defined in CAN controller's datasheet
  • PHSEG2 as defined in CAN controller's datasheet
  • PROPSEG as defined in CAN controller's datasheet
  • CANSPI_CONFIG_FLAGS is formed from predefined constants (see CANSPI constants)
Requires Global variables :
  • CanSpi_CS: Chip Select line
  • CanSpi_Rst: Reset line
  • CanSpi_CS_Direction: Direction of the Chip Select pin
  • CanSpi_Rst_Direction: Direction of the Reset pin
must be defined before using this function.


The CANSPI routines are supported only by MCUs with the SPI module.

The SPI module needs to be initialized. See the SPI1_Init and SPI1_Init_Advanced routines.

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
// CANSPI module connections
var CanSpi_CS    : sbit at RC0_bit;
    CanSpi_CS_Direction : sbit at TRISC0_bit;
    CanSpi_Rst : sbit at RC2_bit;
    CanSpi_Rst_Direction : sbit at TRISC2_bit;
// End CANSPI module connections  

var Can_Init_Flags: byte;
  ...  
  Can_Init_Flags := _CANCONFIG_SAMPLE_THRICE and  // form value to be used
                   _CANCONFIG_PHSEG2_PRG_ON and  // with CANSPIInitialize
                   _CANCONFIG_XTD_MSG       and
                   _CANCONFIG_DBL_BUFFER_ON and
                   _CANCONFIG_VALID_XTD_MSG;
  ...
  SPI1_Init();                                 // initialize SPI module
  CANSPIInitialize(1,3,3,3,1,Can_Init_Flags);  // initialize external CANSPI module

CANSPISetBaudRate

Prototype

procedure CANSPISetBaudRate(SJW: byte; BRP: byte; PHSEG1: byte; PHSEG2: byte; PROPSEG: byte; CANSPI_CONFIG_FLAGS: byte);

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:

  • SJW as defined in CAN controller's datasheet
  • BRP as defined in CAN controller's datasheet
  • PHSEG1 as defined in CAN controller's datasheet
  • PHSEG2 as defined in CAN controller's datasheet
  • PROPSEG as defined in CAN controller's datasheet
  • CANSPI_CONFIG_FLAGS is formed from predefined constants (see CANSPI constants)
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
// set required baud rate and sampling rules
var canspi_config_flags: byte;
...  
CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF);              // set CONFIGURATION mode (CANSPI module mast be in config mode for baud rate settings)
canspi_config_flags := _CANSPI_CONFIG_SAMPLE_THRICE and
                   _CANSPI_CONFIG_PHSEG2_PRG_ON and
                   _CANSPI_CONFIG_STD_MSG       and
                   _CANSPI_CONFIG_DBL_BUFFER_ON and
                   _CANSPI_CONFIG_VALID_XTD_MSG and
                   _CANSPI_CONFIG_LINE_FILTER_OFF;
CANSPISetBaudRate(1, 1, 3, 3, 1, canspi_config_flags);

CANSPISetMask

Prototype

procedure CANSPISetMask(CANSPI_MASK: byte; val: longint; CANSPI_CONFIG_FLAGS: byte);

Returns

Nothing.

Description

Configures mask for advanced filtering of messages. The parameter value is bit-adjusted to the appropriate mask registers.

Parameters:

  • CANSPI_MASK: CANSPI module mask number. Valid values: CANSPI_MASK constants (see CANSPI constants)
  • val: mask register value
  • CANSPI_CONFIG_FLAGS: selects type of message to filter. Valid values:
    • _CANSPI_CONFIG_ALL_VALID_MSG,
    • _CANSPI_CONFIG_MATCH_MSG_TYPE and _CANSPI_CONFIG_STD_MSG,
    • _CANSPI_CONFIG_MATCH_MSG_TYPE and _CANSPI_CONFIG_XTD_MSG.
    (see CANSPI constants)
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
// set the appropriate filter mask and message type value
CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF);              // set CONFIGURATION mode (CANSPI module must be in config mode for mask settings)

// Set all B1 mask bits to 1 (all filtered bits are relevant):
// Note that -1 is just a cheaper way to write 0xFFFFFFFF.
// Complement will do the trick and fill it up with ones.
CANSPISetMask(_CANSPI_MASK_B1, -1, _CANSPI_CONFIG_MATCH_MSG_TYPE and _CANSPI_CONFIG_XTD_MSG);

CANSPISetFilter

Prototype

procedure CANSPISetFilter(CANSPI_FILTER: byte; val: longint; CANSPI_CONFIG_FLAGS: byte);

Returns

Nothing.

Description

Configures message filter. The parameter value is bit-adjusted to the appropriate filter registers.

Parameters:

  • CANSPI_FILTER: CANSPI module filter number. Valid values: CANSPI_FILTER constants (see CANSPI constants)
  • val: filter register value
  • CANSPI_CONFIG_FLAGS: selects type of message to filter. Valid values:
    • _CANSPI_CONFIG_ALL_VALID_MSG,
    • _CANSPI_CONFIG_MATCH_MSG_TYPE and _CANSPI_CONFIG_STD_MSG,
    • _CANSPI_CONFIG_MATCH_MSG_TYPE and _CANSPI_CONFIG_XTD_MSG.
    (see CANSPI constants)
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
// set the appropriate filter value and message type
CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF);                  // set CONFIGURATION mode (CANSPI module must be in config mode for filter settings)

// Set id of filter B1_F1 to 3: 
CANSPISetFilter(_CANSPI_FILTER_B1_F1, 3, _CANSPI_CONFIG_XTD_MSG);

CANSPIRead

Prototype

function CANSPIRead(var id: longint; var rd_data: array[8] of byte; data_len: byte; var CANSPI_RX_MSG_FLAGS: byte): byte;

Returns

  • 0 if nothing is received
  • 0xFF if one of the Receive Buffers is full (message received)

Description

If at least one full Receive Buffer is found, it will be processed in the following way:

  • Message ID is retrieved and stored to location provided by the id parameter
  • Message data is retrieved and stored to a buffer provided by the rd_data parameter
  • Message length is retrieved and stored to location provided by the data_len parameter
  • Message flags are retrieved and stored to location provided by the CANSPI_RX_MSG_FLAGS parameter

Parameters:

  • id: message identifier storage address
  • rd_data: data buffer (an array of bytes up to 8 bytes in length)
  • data_len: data length storage address.
  • CANSPI_RX_MSG_FLAGS: message flags storage address
Requires

The CANSPI module must be in a mode in which receiving is possible. 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
// check the CANSPI module for received messages. If any was received do something. 
var msg_rcvd, rx_flags, data_len: byte;
 rd_data: array[8] of byte;
 msg_id: longint;
...
CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF);                  // set NORMAL mode (CANSPI module must be in mode in which receive is possible)
...
rx_flags := 0;                                                // clear message flags
if (msg_rcvd = CANSPIRead(msg_id, rd_data, data_len, rx_flags) 
begin
  ...
end;

CANSPIWrite

Prototype

function CANSPIWrite(id: longint; var wr_data: array[8] of byte; data_len: byte; CANSPI_TX_MSG_FLAGS: byte): byte;

Returns

  • 0 if all Transmit Buffers are busy
  • 0xFF if at least one Transmit Buffer is available

Description

If at least one empty Transmit Buffer is found, the function sends message in the queue for transmission.

Parameters:

  • id:CAN message identifier. Valid values: 11 or 29 bit values, depending on message type (standard or extended)
  • wr_data: data to be sent (an array of bytes up to 8 bytes in length)
  • data_len: data length. Valid values: 1 to 8
  • CANSPI_RX_MSG_FLAGS: message flags
Requires

The CANSPI module must be in mode in which transmission is possible. 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
// send message extended CAN message with the appropriate ID and data
var tx_flags: byte;
 rd_data: array[8] of byte;
 msg_id: longint;
...
CANSPISetOperationMode(_CANSPI_MODE_NORMAL, 0xFF);                  // set NORMAL mode (CANSPI must be in mode in which transmission is possible)

tx_flags := _CANSPI_TX_PRIORITY_0 and _CANSPI_TX_XTD_FRAME;             // set message flags
CANSPIWrite(msg_id, rd_data, 2, tx_flags);

CANSPI Constants

There is a number of constants predefined in the CANSPI library. You need to be familiar with them in order to be able to use the library effectively. Check the example at the end of the chapter.

CANSPI_OP_MODE

The CANSPI_OP_MODE constants define CANSPI operation mode. Function CANSPISetOperationMode expects one of these as it's argument:

const
      _CANSPI_MODE_BITS   = 0xE0;   // Use this to access opmode  bits
      _CANSPI_MODE_NORMAL = 0x00;
      _CANSPI_MODE_SLEEP  = 0x20;
      _CANSPI_MODE_LOOP   = 0x40;       
      _CANSPI_MODE_LISTEN = 0x60;
      _CANSPI_MODE_CONFIG = 0x80;

CANSPI_CONFIG_FLAGS

The CANSPI_CONFIG_FLAGS constants define flags related to the CANSPI module configuration. The functions CANSPIInitialize, CANSPISetBaudRate, CANSPISetMask and CANSPISetFilter expect one of these (or a bitwise combination) as their argument:

const
    _CANSPI_CONFIG_DEFAULT         = 0xFF;   // 11111111
                                         
    _CANSPI_CONFIG_PHSEG2_PRG_BIT  = 0x01;
    _CANSPI_CONFIG_PHSEG2_PRG_ON   = 0xFF;   // XXXXXXX1
    _CANSPI_CONFIG_PHSEG2_PRG_OFF  = 0xFE;   // XXXXXXX0
                                         
    _CANSPI_CONFIG_LINE_FILTER_BIT = 0x02;
    _CANSPI_CONFIG_LINE_FILTER_ON  = 0xFF;   // XXXXXX1X
    _CANSPI_CONFIG_LINE_FILTER_OFF = 0xFD;   // XXXXXX0X
                                         
    _CANSPI_CONFIG_SAMPLE_BIT      = 0x04;
    _CANSPI_CONFIG_SAMPLE_ONCE     = 0xFF;   // XXXXX1XX
    _CANSPI_CONFIG_SAMPLE_THRICE   = 0xFB;   // XXXXX0XX
                                         
    _CANSPI_CONFIG_MSG_TYPE_BIT    = 0x08;
    _CANSPI_CONFIG_STD_MSG         = 0xFF;   // XXXX1XXX
    _CANSPI_CONFIG_XTD_MSG         = 0xF7;   // XXXX0XXX
                                         
    _CANSPI_CONFIG_DBL_BUFFER_BIT  = 0x10;
    _CANSPI_CONFIG_DBL_BUFFER_ON   = 0xFF;   // XXX1XXXX
    _CANSPI_CONFIG_DBL_BUFFER_OFF  = 0xEF;   // XXX0XXXX
                                         
    _CANSPI_CONFIG_MSG_BITS        = 0x60;
    _CANSPI_CONFIG_ALL_MSG         = 0xFF;   // X11XXXXX
    _CANSPI_CONFIG_VALID_XTD_MSG   = 0xDF;   // X10XXXXX
    _CANSPI_CONFIG_VALID_STD_MSG   = 0xBF;   // X01XXXXX
    _CANSPI_CONFIG_ALL_VALID_MSG   = 0x9F;   // X00XXXXX

You may use bitwise and to form config byte out of these values. For example:

Copy Code To ClipboardCopy Code To Clipboard
init := _CANSPI_CONFIG_SAMPLE_THRICE    and
        _CANSPI_CONFIG_PHSEG2_PRG_ON    and
        _CANSPI_CONFIG_STD_MSG          and
        _CANSPI_CONFIG_DBL_BUFFER_ON    and
        _CANSPI_CONFIG_VALID_XTD_MSG    and
        _CANSPI_CONFIG_LINE_FILTER_OFF;
...
CANSPIInitialize(1, 1, 3, 3, 1, init);   // initialize CANSPI

CANSPI_TX_MSG_FLAGS

CANSPI_TX_MSG_FLAGS are flags related to transmission of a CANSPI message:

const
    _CANSPI_TX_PRIORITY_BITS = 0x03;
    _CANSPI_TX_PRIORITY_0    = 0xFC;   // XXXXXX00
    _CANSPI_TX_PRIORITY_1    = 0xFD;   // XXXXXX01
    _CANSPI_TX_PRIORITY_2    = 0xFE;   // XXXXXX10
    _CANSPI_TX_PRIORITY_3    = 0xFF;   // XXXXXX11
                                   
    _CANSPI_TX_FRAME_BIT     = 0x08;
    _CANSPI_TX_STD_FRAME     = 0xFF;   // XXXXX1XX
    _CANSPI_TX_XTD_FRAME     = 0xF7;   // XXXXX0XX
                                   
    _CANSPI_TX_RTR_BIT       = 0x40;
    _CANSPI_TX_NO_RTR_FRAME  = 0xFF;   // X1XXXXXX
    _CANSPI_TX_RTR_FRAME     = 0xBF;   // X0XXXXXX

You may use bitwise and to adjust the appropriate flags. For example:

Copy Code To ClipboardCopy Code To Clipboard
// form value to be used as sending message flag: 
send_config := _CANSPI_TX_PRIORITY_0    and
               _CANSPI_TX_XTD_FRAME     and
               _CANSPI_TX_NO_RTR_FRAME;
...
CANSPIWrite(id, data, 1, send_config);

CANSPI_RX_MSG_FLAGS

CANSPI_RX_MSG_FLAGS are flags related to reception of CANSPI message. If a particular bit is set then corresponding meaning is TRUE otherwise it will be FALSE.

const
    _CANSPI_RX_FILTER_BITS = 0x07;   // Use this to access filter bits
    _CANSPI_RX_FILTER_1    = 0x00;
    _CANSPI_RX_FILTER_2    = 0x01;
    _CANSPI_RX_FILTER_3    = 0x02;
    _CANSPI_RX_FILTER_4    = 0x03;
    _CANSPI_RX_FILTER_5    = 0x04;
    _CANSPI_RX_FILTER_6    = 0x05;

    _CANSPI_RX_OVERFLOW     = 0x08;  // Set if Overflowed else cleared
    _CANSPI_RX_INVALID_MSG  = 0x10;  // Set if invalid else cleared
    _CANSPI_RX_XTD_FRAME    = 0x20;  // Set if XTD message else cleared
    _CANSPI_RX_RTR_FRAME    = 0x40;  // Set if RTR message else cleared
    _CANSPI_RX_DBL_BUFFERED = 0x80;  // Set if this message was hardware double-buffered

You may use bitwise and to adjust the appropriate flags. For example:

Copy Code To ClipboardCopy Code To Clipboard
if (MsgFlag and _CANSPI_RX_OVERFLOW <> 0) then
begin
  ...
  // Receiver overflow has occurred.
  // We have lost our previous message.
end;

CANSPI_MASK

The CANSPI_MASK constants define mask codes. Function CANSPISetMask expects one of these as it's argument:

const
     _CANSPI_MASK_B1 = 0;
     _CANSPI_MASK_B2 = 1;


CANSPI_FILTER

The CANSPI_FILTER constants define filter codes. Functions CANSPISetFilter expects one of these as it's argument:

const
    _CANSPI_FILTER_B1_F1 = 0;
    _CANSPI_FILTER_B1_F2 = 1;
    _CANSPI_FILTER_B2_F1 = 2;
    _CANSPI_FILTER_B2_F2 = 3;
    _CANSPI_FILTER_B2_F3 = 4;
    _CANSPI_FILTER_B2_F4 = 5;

Library Example

This is a simple demonstration of CANSPI Library routines usage. First node initiates the communication with the second node by sending some data to its address. The second node responds by sending back the data incremented by 1. First node then does the same and sends incremented data back to second node, etc.

Code for the first CANSPI node:

Copy Code To ClipboardCopy Code To Clipboard
program Can_Spi_1st;

var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags : byte;   // can flags
    Rx_Data_Len : byte;                                     // received data length in bytes
    RxTx_Data   : array[8] of byte;                         // can rx/tx data buffer
    Msg_Rcvd : byte;                                        // reception flag
    Tx_ID, Rx_ID : longint;                                 // can rx and tx ID

// CANSPI module connections
var CanSpi_CS : sbit at  RC0_bit;
    CanSpi_CS_Direction : sbit at TRISC0_bit;
    CanSpi_Rst : sbit at RC2_bit;
    CanSpi_Rst_Direction : sbit at TRISC2_bit;
// End CANSPI module connections

begin

  ANSEL  := 0;                       // Configure AN pins as digital I/O
  ANSELH := 0;
  PORTB := 0;
  TRISB := 0;

  Can_Init_Flags := 0;                                      //
  Can_Send_Flags := 0;                                      // clear flags
  Can_Rcv_Flags  := 0;                                      //

  Can_Send_Flags := _CANSPI_TX_PRIORITY_0 and               // form value to be used
                    _CANSPI_TX_XTD_FRAME and                //     with CANSPIWrite
                    _CANSPI_TX_NO_RTR_FRAME;

  Can_Init_Flags := _CANSPI_CONFIG_SAMPLE_THRICE and        // form value to be used
                    _CANSPI_CONFIG_PHSEG2_PRG_ON and        // with CANSPIInit
                    _CANSPI_CONFIG_XTD_MSG and
                    _CANSPI_CONFIG_DBL_BUFFER_ON and
                    _CANSPI_CONFIG_VALID_XTD_MSG;

  SPI1_Init();                                                      // initialize SPI module
  CANSPIInitialize(1,3,3,3,1,Can_Init_Flags);                       // Initialize external CANSPI module
  CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF);                 // set CONFIGURATION mode
  CANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG);         // set all mask1 bits to ones
  CANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG);         // set all mask2 bits to ones
  CANSPISetFilter(_CANSPI_FILTER_B2_F4,3,_CANSPI_CONFIG_XTD_MSG);   // set id of filter B2_F4 to 3

  CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF);                 // set NORMAL mode

  RxTx_Data[0] := 9;                                                // set initial data to be sent

  Tx_ID := 12111;                                                   // set transmit ID

  CANSPIWrite(Tx_ID, RxTx_Data, 1, Can_Send_Flags);                 // send initial message
  while (TRUE) do
    begin                                                                       // endless loop
      Msg_Rcvd := CANSPIRead(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags);   // receive message
      if ((Rx_ID = 3) and Msg_Rcvd) then                                        // if message received check id
        begin
          PORTB := RxTx_Data[0];                                                // id correct, output data at PORTB
          Inc(RxTx_Data[0]) ;                                                   // increment received data
          Delay_ms(10);
          CANSPIWrite(Tx_ID, RxTx_Data, 1, Can_Send_Flags);                     // send incremented data back
        end;
    end;
end.

Code for the second CANSPI node:

Copy Code To ClipboardCopy Code To Clipboard
program Can_Spi_2nd;

var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags : byte;    // can flags
    Rx_Data_Len : byte;                                      // received data length in bytes
    RxTx_Data    : array[8] of byte;                         // CAN rx/tx data buffer
    Msg_Rcvd : byte;                                         // reception flag
    Tx_ID, Rx_ID : longint;                                  // can rx and tx ID

// CANSPI module connections
var CanSpi_CS : sbit at RC0_bit;
    CanSpi_CS_Direction : sbit at TRISC0_bit;
    CanSpi_Rst : sbit at RC2_bit;
    CanSpi_Rst_Direction : sbit at TRISC2_bit;
// End CANSPI module connections

begin

  ANSEL  := 0;                                                 // Configure AN pins as digital I/O
  ANSELH := 0;
  PORTB := 0;                                                  // clear PORTC
  TRISB := 0;                                                  // set PORTC as output

  Can_Init_Flags := 0;                                         //
  Can_Send_Flags := 0;                                         // clear flags
  Can_Rcv_Flags  := 0;                                         //

  Can_Send_Flags := _CANSPI_TX_PRIORITY_0 and                  // form value to be used
                    _CANSPI_TX_XTD_FRAME and                   //  with CANSPIWrite
                    _CANSPI_TX_NO_RTR_FRAME;

  Can_Init_Flags := _CANSPI_CONFIG_SAMPLE_THRICE and           // Form value to be used
                    _CANSPI_CONFIG_PHSEG2_PRG_ON and           //  with CANSPIInit
                    _CANSPI_CONFIG_XTD_MSG and
                    _CANSPI_CONFIG_DBL_BUFFER_ON and
                    _CANSPI_CONFIG_VALID_XTD_MSG and
                    _CANSPI_CONFIG_LINE_FILTER_OFF;

  SPI1_Init();                                                                 // initialize SPI1 module
  CANSPIInitialize(1,3,3,3,1,Can_Init_Flags);                                  // initialize external CANSPI module
  CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF);                            // set CONFIGURATION mode
  CANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG);                    // set all mask1 bits to ones
  CANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG);                    // set all mask2 bits to ones
  CANSPISetFilter(_CANSPI_FILTER_B2_F3,12111,_CANSPI_CONFIG_XTD_MSG);          // set id of filter B2_F3 to 3
  CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF);                            // set NORMAL mode
  Tx_ID := 3;                                                                  // set tx ID

  while (TRUE) do                                                              // endless loop
    begin
      Msg_Rcvd := CANSPIRead(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags);  // receive message
      if ((Rx_ID = 12111) and Msg_Rcvd) then                                   // if message received check id
        begin
          PORTB := RxTx_Data[0];                                               // id correct, output data at PORTB
          Inc(RxTx_Data[0]) ;                                                  // increment received data
          CANSPIWrite(Tx_ID, RxTx_Data, 1, Can_Send_Flags);                    // send incremented data back
        end;
    end;
end.

HW Connection

Example of interfacing CAN transceiver MCP2510 with MCU and bus

Example of interfacing CAN transceiver MCP2510 with MCU via SPI interface

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code