User manual

mikroC PRO for dsPIC
MikroElektronika
339
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
#include “ECAN_Defs.h”
unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can ags
unsigned int Rx_Data_Len; // received data length in bytes
char RxTx_Data[8]; // can rx/tx data buffer
char Msg_Rcvd; // reception ag
unsigned long Tx_ID, Rx_ID; // can rx and tx ID
void C1Interrupt(void) org 0x005A { // ECAN event iterrupt
IFS2bits.C1IF = 0; // clear ECAN interrupt ag
if(C1INTFbits.TBIF) { // was it tx interrupt?
C1INTFbits.TBIF = 0; // if yes clear tx interrupt ag
}
if(C1INTFbits.RBIF) { // was it rx interrupt?
C1INTFbits.RBIF = 0; // if yes clear rx interrupt ag
}
}
void main() {
// Set PLL : Fosc = ((Fin/PLLPRE)*PLLDIV)/PLLPOST ; (((10MHz/2)*32)/4) = 20MHz
// refer the pic24 family datasheet for more details
CLKDIV &= 0xFFE0; //CLKDIVbits.PLLPRE = 0;
PLLFBD = 0x1E; //PLLFBDbits.PLLDIV = 0x1E;
CLKDIV &= 0xFF3F; //CLKDIVbits.PLLPOST = 1;
CLKDIV |= 0x00C0;
AD1PCFGH = 0xFFFF; //
AD1PCFGL = 0xFFFF; // all ports digital I/O
AD2PCFGL = 0xFFFF; //
/* Clear Interrupt Flags */
IFS0=0;
IFS1=0;
IFS2=0;
IFS3=0;
IFS4=0;
/* Enable ECAN1 Interrupt */