Mikroelektronika MIKROE-724 データシート

ページ / 726
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
315
Library Example
The example demonstrates ECAN protocol. The 1st 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. The 1st node then does 
the same and sends incremented data back to the 2nd node, etc.
Code for the first ECAN node:
Copy Code To Clipboard
program ECan_1st
include ECAN_Defs
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 word                              ‘ reception flag
    Rx_ID        as longint
const ID_1st as longint = 12111
const ID_2nd as longint = 3                               ‘ node IDs
sub procedure C1Interrupt() org 0x005A                    ‘ ECAN event iterrupt
  IFS2.C1IF = 0                   ‘ clear ECAN interrupt flag
  if(C1INTF.TBIF <> 0) then       ‘ was it tx interrupt?
    C1INTF.TBIF = 0               ‘ if yes clear tx interrupt flag
  end if
  if(C1INTF.RBIF <> 0) then       ‘ was it rx interrupt?
    C1INTF.RBIF = 0               ‘ if yes clear rx interrupt flag
  end if
end sub
main:
  ‘ Set PLL : Fosc = ((Fin/PLLPRE)*PLLDIV)/PLLPOST ; (((10MHz/2)*32)/4) = 20MHz
  ‘ refer the pic33 family datasheet for more details
  CLKDIV = CLKDIV and 0xFFE0  ‘ CLKDIVbits.PLLPRE = 0
  PLLFBD = 0x1E               ‘ PLLFBDbits.PLLDIV = 0x1E
  CLKDIV = CLKDIV and 0xFF3F  ‘ CLKDIVbits.PLLPOST = 1
  CLKDIV = CLKDIV or 0x00C0
  AD1PCFGH = 0xFFFF                               ‘
  AD1PCFGL = 0xFFFF                               ‘ all ports digital I/O
  AD2PCFGL = 0xFFFF                               ‘
‘ Clear Interrupt Flags
  IFS0 = 0
  IFS1 = 0