User manual
mikroPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
315
while TRUE do // endless loop
begin
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
begin
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;
end;
end.
Code for the second ECAN node:
Copy Code To Clipboard
program ECAN_2nd;
uses ECan_Defs;
var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags : word; // can ags
Rx_Data_Len : word; // received data length in bytes
RxTx_Data : array[8] of byte; // can rx/tx data buffer
Msg_Rcvd : word; // reception ag
Rx_ID : longint; // can rx and tx ID
const ID_1st : longint = 12111;
const ID_2nd : longint = 3; // node IDs
procedure C1Interrupt(); org 0x005A; // ECAN event iterrupt
begin
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
if(C1INTF.RBIF <> 0) then // was it rx interrupt?
C1INTF.RBIF := 0; // if yes clear rx interrupt ag
end;
begin
// Set PLL : Fosc = ((Fin/PLLPRE)*PLLDIV)/PLLPOST ; (((10MHz/2)*32)/4) = 20MHz
// refer the 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; //