// Configure the eCAN for self test mode
// Enable the enhanced features of the eCAN.
EALLOW;
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.STM = 0; // Configure CAN for self-test mode
ECanbShadow.CANMC.bit.SCB = 1; // eCAN mode (reqd to access 32 mailboxes)
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;
// Write to the MSGID field of RECEIVE mailboxes MBOX16
ECanbMboxes.MBOX16.MSGID.all = 0x9555AAA0;
/* // Configure Mailboxes 0-15 as Tx, 16-31 as Rx
// Since this write is to the entire register (instead of a bit
// field) a shadow register is not required.
ECanbRegs.CANMD.all = 0xFFFF0000;
*/
//ÓÊÏä0ΪTX
ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
ECanbShadow.CANMD.bit.MD0 =0;
ECanbShadow.CANMD.bit.MD16 =1;
ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;
/* // Enable all Mailboxes
// Since this write is to the entire register (instead of a bit
// field) a shadow register is not required.
ECanbRegs.CANME.all = 0xFFFFFFFF;
*/
// Specify that 8 bits will be sent/received
ECanbMboxes.MBOX0.MSGCTRL.bit.DLC = 8;
// No remote frame is requested ûÓÐÔ¶·½Ó¦´ðÖ¡
// Since RTR bit is undefined upon reset,
// it must be initialized to the proper value
ECanbMboxes.MBOX0.MSGCTRL.bit.RTR = 0;
// Write to the mailbox RAM field of MBOX0
ECanbMboxes.MBOX0.MDL.all = 0x01010101;
ECanbMboxes.MBOX0.MDH.all = 0x01010101;
// Since this write is to the entire register (instead of a bit
// field) a shadow register is not required.
EALLOW;
ECanbRegs.CANMIM.all = 0xFFFFFFFF;//ʹÄÜÓÊÏäÖжÏ
// Request permission to change the configuration registers
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 1;
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;
// Wait until the CPU has been granted permission to change the
// configuration registers
// Wait for CCE bit to be set..
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 1 );
// Configure the eCAN timing
EALLOW;
ECanbShadow.CANBTC.all = ECanbRegs.CANBTC.all;
ECanbShadow.CANBTC.bit.BRPREG = 9; // (BRPREG + 1) = 10 feeds a 15 MHz CAN clock
ECanbShadow.CANBTC.bit.TSEG2REG = 5 ; // to the CAN module. (150 / 10 = 15) 5 7
ECanbShadow.CANBTC.bit.TSEG1REG = 7; // Bit time = 15 7 15
ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;//1Mbps 600
// Wait until the CPU no longer has permission to change the
// configuration registers
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 0 );
ECanbRegs.CANTRS.all = 0x00000001; // Set TRS for all transmit mailboxes
// while(ECanbRegs.CANTA.all == 0x0000FFFF ) {} // Wait for all TAn bits to be set..
while(ECanbRegs.CANTA.all == 0){}
ECanbRegs.CANTA.all = 0x00000001; // Clear Mailbox0 TAn