English
Language : 

70353C Datasheet, PDF (48/76 Pages) Microchip Technology – Section 21. Enhanced Controller Area Network
dsPIC33E/PIC24E Family Reference Manual
Example 21-5: Code Example for Filtering Extended Data Frame
#include <p33Exxxx.h>
/* This code example demonstrates a method to configure the ECAN module to receive Extended
ID CAN messages. EID Messages with EID range 0x3FFC0-0x3FFFF will be accepted. */
/* Include fuse configuration code here. Optionally the fuse configuration can be specified
via MPLAB IDE Menu operations. */
FUSE_CONFIGURATION_MACROS_COME_HERE
#define NUM_OF_ECAN_BUFFERS 32
/* This is the ECAN message buffer declaration. Note the buffer alignment. */
unsigned int ecan1MsgBuf[NUM_OF_ECAN_BUFFERS][8]
__attribute__((aligned(NUM_OF_ECAN_BUFFERS * 16)));
int main(void)
{
unsigned long address;
/* Place code to set device speed here. For this example the device speed should be set at
40 MHz (i.e., the device is operating at 40 MIPS). */
ConfigureDeviceClockFor40MIPS();
/* The dsPIC33E device features I/O remap. This I/O remap configuration for the ECAN
module can be performed here. */
SetIORemapForECANModule();
/* Set up the ECAN1 module to operate at 250 kbps. The ECAN module should be first placed
in configuration mode. */
C1CTRL1bits.REQOP = 4;
while(C1CTRL1bits.OPMODE != 4);
C1CTRL1bits.WIN = 1;
/* Set up the CAN module for 250 kbps speed with 10 Tq per bit. */
C1CFG1 = 0x47;
// BRP = 8 SJW = 2 Tq
C1CFG2 = 0x2D2;
C1FCTRL = 0xC01F; // No FIFO, 32 Buffers
/* Configure Acceptance Filter Mask 1
* register to mask EID<5:0>
* Mask Bits (29-bits) : 0b1 1111 1111 1111 1111 1111 1100 0000
* SID<10:0> : 0b11111111111 ..SID<10:0> or EID<28:18>
* EID<17:16> : 0b11 ..EID<17:16>
* EID<15:0> : 0b1111111111000000 ..EID<15:0> */
C1RXM1SID = 0xFFEB;
C1RXM1EID = 0xFFC0;
/* Configure Acceptance Filter 2 to match extended identifier
* Filter Bits (29-bits) : 0b0 0000 0000 0011 1111 1111 11xx xxxx
* SID<10:0> : 0b00000000000 ..SID<10:0> or EID<28:18>
* EID<17:16> : 0b11 ..EID<17:16>
* EID<15:0> : 0b1111111111xxxxxx ..EID<15:0> */
C1RXF2SID = 0xB;
C1RXF2EID = 0xFFFF;
/* Acceptance Filter 2 to use Message Buffer 6 to store message */
C1BUFPNT1bits.F2BP = 0x6;
/* Filter 2 enabled for Identifier match with incoming message */
C1FEN1 = 0;
C1FEN1bits.FLTEN2 = 0x1;
DS70353C-page 21-48
© 2008-2011 Microchip Technology Inc.