Mikroelektronika MIKROE-724 データシート

ページ / 726
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
275
Library Example
The code is a simple demonstration of CANSPI protocol. This node initiates the communication with the 2nd node by 
sending some data to its address. The 2nd node responds by sending back the data incremented by 1. This (1st) node 
then does the same and sends incremented data back to the 2nd node, etc.
Code for the first CANSPI node:
Copy Code To Clipboard 
program Can_Spi_1st
const ID_1st as longint = 12111
const ID_2nd as longint = 3
dim Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags as word   ‘ can flags
    Rx_Data_Len as word                             ‘ received data length in bytes
    RxTx_Data   as byte[8]                                  ‘ can rx/tx data buffer
    Msg_Rcvd as byte                                        ‘ reception flag
    Tx_ID, Rx_ID as longword                                ‘ can rx and tx ID
‘ CANSPI module connections
dim CanSpi_CS            as sbit at LATF0_bit
    CanSpi_CS_Direction  as sbit at TRISF0_bit
    CanSpi_Rst           as sbit at LATF1_bit
    CanSpi_Rst_Direction as sbit at TRISF1_bit
‘ End CANSPI module connections
  ADPCFG = 0xFFFF                                 ‘ Configure AN pins as digital I/O
  PORTB = 0                                       ‘ clear PORTB
  TRISB = 0                                       ‘ set PORTB 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
‘ Initialize SPI1 module
  SPI1_Init()
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,ID_2nd,_CANSPI_CONFIG_XTD_MSG)  ’set  id  of  filter 
B2_F4 to 2nd node ID