本帖最后由 煜笛 于 2018-11-26 16:37 编辑
代码main()
{
InitECanGpio();
DINT; //置1,关中端
IER = 0x0000;// Disable and clear all CPU interrupts
IFR = 0x0000;
InitPieCtrl();
InitPieVectTable();// Initialize PIE vector table:
InitECana(); // Initialize eCAN-A module
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.SCB = 1;
ECanaShadow.CANMC.bit.DBO = 0; //接收保存和读发送的低字节在前,高字节在后
ECanaShadow.CANMC.bit.STM = 0; //NORMAL MODE
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
// Write to the MSGID field of TRANSMIT mailboxes MBOX0
ECanaShadow.CANMD.all = ECanaRegs.CANMD.all ;
ECanaRegs.CANMD.bit.MD0 = 1; //Milbox0(send)
ECanaRegs.CANMD.all = ECanaShadow.CANMD.all ;
//ENABLE MAILBOX0
ECanaShadow.CANME.all = ECanaRegs.CANME.all ;
ECanaRegs.CANME.bit.ME0 = 1; //ENABLE MAILBOX0
ECanaRegs.CANME.all = ECanaShadow.CANME.all ;
//ENABLE interrupts for Mailbox0
ECanaShadow.CANMIM.all = ECanaRegs.CANMIM.all;
ECanaShadow.CANMIM.bit.MIM0 = 1; // Enable interrupts for Mailbox0.
ECanaRegs.CANMIM.all = ECanaShadow.CANMIM.all;
//CONFIG MAILBOX0
ECanaMboxes.MBOX0.MSGID.all = 0x0A000000; //000 010 1000 0000 000 上行数据非自动应答远程帧,遥传ID 0x80
ECanaMboxes.MBOX0.MDL.all = 0x04030201; // Specify that 8 bits will be sent/received
ECanaMboxes.MBOX0.MDH.all = 0x09080706;
ECanaMboxes.MBOX0.MSGCTRL.bit.DLC = 8; //The send data length code
// Write to the MSGID field of RECEIVE mailboxe MBOX31
ECanaShadow.CANMD.all = ECanaRegs.CANMD.all ;
ECanaRegs.CANMD.bit.MD31 = 1; // MAILBOX31(receive)
ECanaRegs.CANMD.all = ECanaShadow.CANMD.all ;
//ENABLE MAILBOX31
ECanaShadow.CANME.all = ECanaRegs.CANME.all ;
ECanaRegs.CANME.bit.ME31 = 1; //ENABLE MAILBOX31
ECanaRegs.CANME.all = ECanaShadow.CANME.all ;
//CONFIG MAILBOX31
ECanaMboxes.MBOX31.MSGID.all = 0x44CC0000; //010 001 0011 0011 000 下行接收数据,接收屏蔽,声波ID 0x33
ECanaMboxes.MBOX31.MSGCTRL.bit.DLC = 8; //The receive data length code
// locally mask any identifier bits of the incoming message.
ECanaLAMRegs.LAM31.all = 0x8001FFFF; //LAMI=1邮箱31接收屏蔽设置
//LAM[28:0]=0 the incoming bit value must match identically to
//the corresponding bit in the message identifier.
//Configure CAN interrupts
EALLOW;
ECanaShadow.CANMIM.all = ECanaRegs.CANMIM.all;
ECanaShadow.CANMIM.bit.MIM31 = 1; // Enable interrupts for Mailbox31.
ECanaRegs.CANMIM.all = ECanaShadow.CANMIM.all;
ECanaShadow.CANMIL.all = ECanaRegs.CANMIL.all;
ECanaShadow.CANMIL.bit.MIL31 = 1; //The mailbox31 Interrupts asserted on eCAN1INT
ECanaRegs.CANMIL.all = ECanaShadow.CANMIL.all;
ECanaShadow.CANGIM.all = ECanaRegs.CANGIM.all;
ECanaShadow.CANGIM.bit.I1EN = 1; // Enable eCAN1INT
ECanaRegs.CANGIM.all = ECanaShadow.CANGIM.all;
ECanaRegs.CANGIF0.all = 0xFFFFFFFF;
EDIS;
EALLOW;
PieVectTable.ECAN1INTA = &ecan1_isr; //接收后中断函数
EDIS;
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable vector fetching from PIE block
PieCtrlRegs.PIEIER9.bit.INTx6 = 1; //使能ECAN1INTAEINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
while(1)
{
if(remoteflag==1)
{
read_data();
Frame_data();
ECanaShadow.CANTRS.all = ECanaRegs.CANTRS.all;
ECanaRegs.CANTRS.bit.TRS0 = 1;
ECanaRegs.CANTRS.all = ECanaShadow.CANTRS.all;
while(ECanaRegs.CANTA.bit.TA0 != 1 ) {} // Wait for TA0 bits to be set..
ECanaRegs.CANTA.bit.TA0 = 1; // Clear TA0
}
__interrupt void ecan1_isr(void)
{
m=m+1;
if(ECanaRegs.CANRMP.bit.RMP31==1)//RX get after flag and int BOX31
{
if(ECanaMboxes.MBOX31.MSGCTRL.bit.RTR==1)
{
remoteflag=1;
}
else
{
testcmdflag=1;
//收到的数据在接收邮箱Mbox31
Rec_l = ECanaMboxes.MBOX31.MDL.all;
Rec_h = ECanaMboxes.MBOX31.MDH.all;
}
ECanaRegs.CANRMP.bit.RMP31=1;
}
PieCtrlRegs.PIEACK.bit.ACK9 = 1;
EINT;
}
调试发现ECanaRegs.CANRMP.bit.RMP31并没有置位。
|