User manual
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
317
ECAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) ‘ send initial message
while TRUE ‘ endless loop
Msg_Rcvd = ECAN1Read(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags) ‘ receive
message
if ((Rx_ID = ID_2nd) and (Msg_Rcvd <> 0)) <> 0 then ‘ if message received check id
PORTB = RxTx_Data[0] ‘ id correct, output data at PORTB
Inc(RxTx_Data[0])
Delay_ms(10)
ECAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) ‘ send incremented data back
end if
wend
end.
Code for the second ECAN node:
Copy Code To Clipboard
program ECAN_2nd
include ECAN_Defs
dim Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags as word ‘ can ags
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 ag
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 ag
if(C1INTF.TBIF <> 0) then ‘ was it tx interrupt?
C1INTF.TBIF = 0 ‘ if yes clear tx interrupt ag
end if
if(C1INTF.RBIF <> 0) then ‘ was it rx interrupt?
C1INTF.RBIF = 0 ‘ if yes clear rx interrupt ag
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