int main(void)
{
unsigned char index,fifoSize;
/* 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(); // FCY = 40MIPS
/* The dsPIC33CH device features I/O remap. This I/O remap configuration for the CAN FD
module can be performed here. */
SetIORemapForECANModule();
/* Set up the CAN clock generater for 40 MIPS and enable the CAN clock generator. */
ConfigureCanfdClockFor40MIPS();
/* Enable the CANFD module */
C1CONLbits.CON = 1;
/* Place CAN module in configuration mode */
C1CONHbits.REQOP = 4;
while(C1CONHbits.OPMOD != 4);
/* Initialize the C1FIFOBA with the start address of the CAN FIFO message buffer area. */
C1FIFOBAL = (unsigned int) &CanTxBuffer;
/* Set up the CANFD module for 1Mbps of Nominal bit rate speed and 2Mbps of Data bit rate. */
/* Set up the CANFD module for 1Mbps of Nominal bit rate speed and 2Mbps of Data bit rate. */
C1NBTCFGH = 0x003E;
C1NBTCFGL = 0x0F0F;
C1DBTCFGH = 0x001E;
C1DBTCFGL = 0x0707;
C1TDCH = 0x0002; //TDCMOD is Auto
C1TDCL = 0x1F00;
/* Configure CANFD module to save transmitted messages in TEF and BRS*/
C1CONLbits.BRSDIS = 0x0;
C1CONHbits.STEF = 0x1;
C1CONHbits.TXQEN = 0x0; // Disable TXQ
/* Configure TEF to save 5 messages*/
C1TEFCONHbits.FSIZE = 0x4; // save 5 messages
C1TEFCONLbits.TEFTSEN = 1;
/* Configure FIFO1 to transmit 5 messages*/
C1FIFOCON1Hbits.FSIZE = 0x4; //5 messages
C1FIFOCON1Hbits.PLSIZE = 0x7; //64 bytes of data
C1FIFOCON1Lbits.TXEN = 0x1; // Set TXEN bit ,transmit fifo
/* Place the CAN module in Normal mode. */
C1CONHbits.REQOP = 0;
while(C1CONHbits.OPMOD != 0);
/* Get the address of the message buffer to write to. Load the buffer and */
/* then set the UINC bit. Set the TXREQ bit to send the message. */
CANFD_TX_MSGOBJ *txObj;
/* Transmit 5 messages from FIFO 1 - CANFD base frame with BRS*/
/* SID = 0x300 , 64 bytes of data */
for (fifoSize= 0; fifoSize < 5; fifoSize++)
{