|
在自测试模式下,想实现从邮箱1发数据给邮箱3,但是运行程序发现TRS寄存器不能置位,信息发不出去,不知道怎么回事。
有没有人遇到过类似情况呢?
程序如下
void InitECan(void)
{
struct ECAN_REGS ECanaShadow;
EALLOW;
/*使能CAN模块时钟*/
SysCtrlRegs.PCLKCR.all=0x4000;
/*配置eCAN的RX,TX分别为eCAN的接受和发送引脚*/
ECanaRegs.CANTIOC.all=0x0008;
ECanaRegs.CANRIOC.all=0x0008;
/*配置eCAN工作在增强状态,32个邮箱全部可以访问,并支持定时邮递功能*/
//ECanaRegs.CANMC.bit.SCM=1;
/*配置位时序参数*/
ECanaRegs.CANMC.bit.CCR=1; //置CCR=1
while(ECanaRegs.CANES.bit.CCE!=1)
{} //并等待CCE位置位
ECanaRegs.CANBTC.bit.BRP=9;
ECanaRegs.CANBTC.bit.TSEG2=2;
ECanaRegs.CANBTC.bit.TSEG1=10;//由计算公式 比特率=SYSCLK/(BRP+1)×[(TSEG1reg+1)+(TSEG2reg+1)+1]?
//系统时钟为30MHZ,倍频为1/2,此时比特率为100Kbps.
//ECanaRegs.CANMC.all=0x00012440; //设置主控制器寄存器,STM=1,模块工作在自测试模式下
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
//ECanaShadow.CANMC.bit.STM = 1;
ECanaShadow.CANMC.bit.SCM = 1; /*配置eCAN工作在增强状态*/
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
/*初始化所有主设备控制区域为0,MCF(MSGCTRL)所有的所有位都初始化为0*/
ECanaMboxes.MBOX0.MCF.all=0x00000000;
ECanaMboxes.MBOX1.MCF.all=0x00000000;
ECanaMboxes.MBOX2.MCF.all=0x00000000;
ECanaMboxes.MBOX3.MCF.all=0x00000000;
ECanaMboxes.MBOX4.MCF.all=0x00000000;
ECanaMboxes.MBOX5.MCF.all=0x00000000;
ECanaMboxes.MBOX6.MCF.all=0x00000000;
ECanaMboxes.MBOX7.MCF.all=0x00000000;
ECanaMboxes.MBOX8.MCF.all=0x00000000;
ECanaMboxes.MBOX9.MCF.all=0x00000000;
ECanaMboxes.MBOX10.MCF.all=0x00000000;
ECanaMboxes.MBOX11.MCF.all=0x00000000;
ECanaMboxes.MBOX12.MCF.all=0x00000000;
ECanaMboxes.MBOX13.MCF.all=0x00000000;
ECanaMboxes.MBOX14.MCF.all=0x00000000;
ECanaMboxes.MBOX15.MCF.all=0x00000000;
ECanaMboxes.MBOX16.MCF.all=0x00000000;
ECanaMboxes.MBOX17.MCF.all=0x00000000;
ECanaMboxes.MBOX18.MCF.all=0x00000000;
ECanaMboxes.MBOX19.MCF.all=0x00000000;
ECanaMboxes.MBOX20.MCF.all=0x00000000;
ECanaMboxes.MBOX21.MCF.all=0x00000000;
ECanaMboxes.MBOX22.MCF.all=0x00000000;
ECanaMboxes.MBOX23.MCF.all=0x00000000;
ECanaMboxes.MBOX24.MCF.all=0x00000000;
ECanaMboxes.MBOX25.MCF.all=0x00000000;
ECanaMboxes.MBOX26.MCF.all=0x00000000;
ECanaMboxes.MBOX27.MCF.all=0x00000000;
ECanaMboxes.MBOX28.MCF.all=0x00000000;
ECanaMboxes.MBOX29.MCF.all=0x00000000;
ECanaMboxes.MBOX30.MCF.all=0x00000000;
ECanaMboxes.MBOX31.MCF.all=0x00000000;
//邮箱的ID都先设置为0
ECanaMboxes.MBOX0.MID.all=0x00000000;
ECanaMboxes.MBOX1.MID.all=0x00000000;
ECanaMboxes.MBOX2.MID.all=0x00000000;
ECanaMboxes.MBOX3.MID.all=0x00000000;
ECanaMboxes.MBOX4.MID.all=0x00000000;
ECanaMboxes.MBOX5.MID.all=0x00000000;
ECanaMboxes.MBOX6.MID.all=0x00000000;
ECanaMboxes.MBOX7.MID.all=0x00000000;
ECanaMboxes.MBOX8.MID.all=0x00000000;
ECanaMboxes.MBOX9.MID.all=0x00000000;
ECanaMboxes.MBOX10.MID.all=0x00000000;
ECanaMboxes.MBOX11.MID.all=0x00000000;
ECanaMboxes.MBOX12.MID.all=0x00000000;
ECanaMboxes.MBOX13.MID.all=0x00000000;
ECanaMboxes.MBOX14.MID.all=0x00000000;
ECanaMboxes.MBOX15.MID.all=0x00000000;
ECanaMboxes.MBOX16.MID.all=0x00000000;
ECanaMboxes.MBOX17.MID.all=0x00000000;
ECanaMboxes.MBOX18.MID.all=0x00000000;
ECanaMboxes.MBOX19.MID.all=0x00000000;
ECanaMboxes.MBOX20.MID.all=0x00000000;
ECanaMboxes.MBOX21.MID.all=0x00000000;
ECanaMboxes.MBOX22.MID.all=0x00000000;
ECanaMboxes.MBOX23.MID.all=0x00000000;
ECanaMboxes.MBOX24.MID.all=0x00000000;
ECanaMboxes.MBOX25.MID.all=0x00000000;
ECanaMboxes.MBOX26.MID.all=0x00000000;
ECanaMboxes.MBOX27.MID.all=0x00000000;
ECanaMboxes.MBOX28.MID.all=0x00000000;
ECanaMboxes.MBOX29.MID.all=0x00000000;
ECanaMboxes.MBOX30.MID.all=0x00000000;
ECanaMboxes.MBOX31.MID.all=0x00000000;
ECanaShadow.CANMD.all=ECanaRegs.CANMD.all;
ECanaShadow.CANMD.all=0x00000008;
ECanaRegs.CANMD.all=ECanaShadow.CANMD.all;//配置邮箱1为发送邮箱,3为接收邮箱,其它没有用到的暂定为发送邮箱
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 0;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while(ECanaRegs.CANES.bit.CCE!=0)
; //并等待CCE位清零,初始化结束,离开初始化模式
//ECanaRegs.CANME.all=0; //在写MSGID之前禁用所有邮箱
EDIS;
ECanaRegs.CANTA.all=0xFFFFFFFF;
ECanaRegs.CANRMP.all=0xFFFFFFFF;
#if CAN_INT
EALLOW;
ECanaRegs.CANMIM.all = 0xFFFFffff;
ECanaRegs.CANMIL.all = 0;
ECanaRegs.CANGIF0.all = 0xFFFFFFFF;
ECanaRegs.CANGIM.bit.I0EN = 1;
ECanaRegs.CANGIM.bit.I1EN = 1;
ECanaRegs.CANGIM.all = 0x3ff07;
EDIS;
PieCtrl.PIEIER9.bit.INTx6 = 1;
PieCtrl.PIEIER9.bit.INTx5 = 1;
#endif
}
然后我写了两个函数:
transmit1() //发送函数
{
//Uint16 i;
struct ECAN_REGS ECanaShadow;
//printf(\"input the times of the transmission n:\";//输入传送次数n
//scanf(\"%d\",&n);
//InitECan();
//ECanaShadow.CANTA.all=0;
//ECanaShadow.CANTA.bit.TA1=1;//通过向TA1写1来清除TA1位,清除中断
//ECanaRegs.CANTA.all=ECanaShadow.CANTA.all;
ECanaShadow.CANTRR.all=ECanaRegs.CANTRR.all;
ECanaShadow.CANTRR.bit.TRR1=1;//通过置位TRR位来清零TRS位
ECanaRegs.CANTRR.all=ECanaShadow.CANTRR.all;
ECanaMboxes.MBOX1.MID.all=0x9555AAA1;//标准标志符
ECanaMboxes.MBOX1.MCF.all=0x00000108;//数据长度为8,优先级为1,无远程帧请求
ECanaShadow.CANMD.all=ECanaRegs.CANMD.all;
ECanaShadow.CANMD.bit.MD1=0;
ECanaRegs.CANMD.all=ECanaShadow.CANMD.all;//配置邮箱1为发送邮箱
ECanaShadow.CANME.all=ECanaRegs.CANME.all;
ECanaShadow.CANME.bit.ME1=1;
ECanaRegs.CANME.all=ECanaShadow.CANME.all;//使能邮箱1
//for(i=1;i<=n;i++)
//{
//printf(\"input the datas:\";
//scanf(\"%32x,%32x\",&datal,&datah);
ECanaMboxes.MBOX1.MDRL.all=0x01234567;
ECanaMboxes.MBOX1.MDRH.all=0x89ABCDEF;//向邮箱RAM区写数据
//ECanaShadow.CANTRS.all=ECanaRegs.CANTRS.all;
//ECanaShadow.CANTRS.bit.TRS1=1;//TRS置位
ECanaRegs.CANTRS.all=0x00000002;//ECanaShadow.CANTRS.all;
while (ECanaRegs.CANTA.bit.TA1!=1) ;
ECanaRegs.CANTA.bit.TA1=1; //通过向TA1写1来清除TA1位,清除中断
//ECanaShadow.CANTRR.all=ECanaRegs.CANTRR.all;
//ECanaShadow.CANTRR.bit.TRR1=1;//通过置位TRR位来清零TRS位
//ECanaRegs.CANTRR.all=ECanaShadow.CANTRR.all;
//}
}
accept3() //接收函数
{
struct ECAN_REGS ECanaShadow;
//Uint32 a,b;
ECanaShadow.CANME.all=ECanaRegs.CANME.all;
ECanaShadow.CANME.bit.ME3=0;
ECanaRegs.CANME.all=ECanaShadow.CANME.all;//屏蔽邮箱3
ECanaMboxes.MBOX3.MID.all=0x9555AAA1;//写标志符到MSGID,使用接收屏蔽,AME为0。
//LAMREGS.LAM3.all=0x03C00000;
ECanaMboxes.MBOX3.MCF.all=0x00000108;//无远程帧请求,数据长度为8,优先级为1
ECanaShadow.CANMD.all=ECanaRegs.CANMD.all;
ECanaShadow.CANMD.bit.MD3=1;
ECanaRegs.CANMD.all=ECanaShadow.CANMD.all;//配置邮箱3为接收邮箱
ECanaShadow.CANOPC.all=ECanaRegs.CANOPC.all;
ECanaShadow.CANOPC.bit.OPC3=1;
ECanaRegs.CANOPC.all=ECanaShadow.CANOPC.all;//消息覆盖保护,该寄存器为32位读写的寄存器
ECanaShadow.CANME.all=ECanaRegs.CANME.all;
ECanaShadow.CANME.bit.ME3=1;
ECanaRegs.CANME.all=ECanaShadow.CANME.all;//使能邮箱3
while(ECanaRegs.CANRMP.bit.RMP3!=1) ;//等待接收消息挂起寄存器置位,表明接收到数据
ECanaRegs.CANRMP.bit.RMP3 =1;//对CANRMP写1来清零CANRMP的位
//a=ECanaMboxes.MBOX3.MDRL.all;
//b=ECanaMboxes.MBOX3.MDRH.all;
//printf(\"%32x,%32x/n\",a,b);
}
然后在主程序里就是
InitECan();
transmit1();
accept3();
发送邮箱1里数据是写进去了,但是TRS无法置位,邮箱3也接收不到任何东西。
帮忙看看吧,谢谢!!
|
|