User manual
mikroPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
313
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 rst ECAN node:
Copy Code To Clipboard
program ECan_1st;
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;
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; //
{* Clear Interrupt Flags *}
IFS0 := 0;
IFS1 := 0;
IFS2 := 0;