TI中文支持网
TI专业的中文技术问题搜集分享网站

28335的CAN模块无法收发数据

我用的MCU是DSP28335,原例程在自测模式下可以收发,根据原例程进行修改,在使用CAN模块进行数据发送时,程序总是卡在while(ECanbRegs.CANTA.bit.TA0!=1 ) {}判断上,用示波器测量发送和接收引脚,均能显示到波形,上位机的CANtest软件收不到数据,下面是配置程序,恳请协助解决。

EALLOW;
ECanbShadow.CANTIOC.all = ECanbRegs.CANTIOC.all;
ECanbShadow.CANTIOC.bit.TXFUNC = 1; //CANTX引脚 为CAN发送功能
ECanbRegs.CANTIOC.all = ECanbShadow.CANTIOC.all;

ECanbShadow.CANRIOC.all = ECanbRegs.CANRIOC.all;
ECanbShadow.CANRIOC.bit.RXFUNC = 1; //CANRX引脚 为CAN接收功能
ECanbRegs.CANRIOC.all = ECanbShadow.CANRIOC.all;
EDIS;
ECanbRegs.CANME.all = 0; //设置邮箱的ID号,先屏蔽掉邮箱,再设置

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.SCB = 1; //eCAN模式,邮箱0-31可用
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
// Mailboxs can be written to 16-bits or 32-bits at a time
// Write to the MSGID field of TRANSMIT mailboxes MBOX0 – 15 ECanbMboxes.MBOX0.MSGID.all = 0x0000AAA0;
// Write to the MSGID field of RECEIVE mailboxes MBOX16 – 31
ECanbMboxes.MBOX16.MSGID.all = 0x0000AAA1;

// 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.
ECanbShadow.CANMD.bit.MD0 = 0; //0号邮箱为发送邮箱
ECanbShadow.CANMD.bit.MD16 = 1;//16号邮箱为接收邮箱

ECanbRegs.CANME.all = 0x00010001; //使能2个邮箱

ECanbMboxes.MBOX0.MSGCTRL.all = 0x00000000; //初始化所有主设备控制区域为0,MCF所有的位都初始化为0
ECanbMboxes.MBOX16.MSGCTRL.all = 0x00000000;

// Specify that 8 bits will be sent/received
ECanbMboxes.MBOX0.MSGCTRL.bit.DLC = 8; //数据长度 8个BYTE
ECanbMboxes.MBOX16.MSGCTRL.bit.DLC = 8;

ECanbMboxes.MBOX0.MSGCTRL.bit.RTR= 0; //RTR要被初始化一个适当的值
ECanbMboxes.MBOX16.MSGCTRL.bit.RTR= 0;

ECanbMboxes.MBOX0.MSGCTRL.bit.TPL = 0; //设置发送优先级
ECanbMboxes.MBOX16.MSGCTRL.bit.TPL = 1;

ECanbMboxes.MBOX0.MDL.all = 0x0000000F;
ECanbMboxes.MBOX0.MDH.all = 0x000000FF;

EALLOW;
ECanbRegs.CANMIM.all = 0xFFFFFFFF;

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all; //邮箱屏蔽使能
ECanbShadow.CANMC.bit.CCR = 1; // 请求更改配置寄存器的权限
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 1 ); //等待数据发送结束之后CCE位被置位
EALLOW; //配置CAN的时序
ECanbShadow.CANBTC.all = ECanbRegs.CANBTC.all;
//波特率1M
ECanbShadow.CANBTC.bit.BRPREG = 9; // (BRPREG + 1) = 10 feeds a 15 MHz CAN clock
ECanbShadow.CANBTC.bit.TSEG2REG = 2; // to the CAN module. (150 / 10 = 15)5,1
ECanbShadow.CANBTC.bit.TSEG1REG = 10; // Bit time = 15,7,6
ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;
//ECanbShadow.CANBTC.bit.SAM = 1;
//ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 0;
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;

do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 0 );

EALLOW;
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.STM = 0; // Configure CAN for self-test mode,0是正常模式
ECanbShadow.CANMC.bit.SCB = 1; // eCAN mode (reqd to access 32 mailboxes)
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;

ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.bit.ME0=1;
ECanbShadow.CANME.bit.ME16=1;
ECanbRegs.CANME.all=ECanbShadow.CANME.all;
// Begin transmitting
for(;;)
{
ECanbShadow.CANTRS.all=ECanbRegs.CANTRS.all;
ECanbShadow.CANTRS.bit.TRS0 = 0x00000001; // Set TRS for all transmit mailboxes
ECanbRegs.CANTRS.all=ECanbShadow.CANTRS.all;

while(ECanbRegs.CANTA.bit.TA0!=1 ) {} // Wait for all TAn bits to be set
ECanbRegs.CANTA.bit.TA0 =1; // Clear all TAn

ECanbShadow.CANTA.all=ECanbRegs.CANTA.all;
ECanbShadow.CANTA.all = 0x00000001; // Clear all TAn
ECanbRegs.CANTA.all=ECanbShadow.CANTA.all;
}
}

代码急用,但是找不到问题所在,请您多协助,谢谢。

HeiHei:

是不是你的can还是工作在自测模式?设置改了吗?

我用的MCU是DSP28335,原例程在自测模式下可以收发,根据原例程进行修改,在使用CAN模块进行数据发送时,程序总是卡在while(ECanbRegs.CANTA.bit.TA0!=1 ) {}判断上,用示波器测量发送和接收引脚,均能显示到波形,上位机的CANtest软件收不到数据,下面是配置程序,恳请协助解决。

EALLOW;
ECanbShadow.CANTIOC.all = ECanbRegs.CANTIOC.all;
ECanbShadow.CANTIOC.bit.TXFUNC = 1; //CANTX引脚 为CAN发送功能
ECanbRegs.CANTIOC.all = ECanbShadow.CANTIOC.all;

ECanbShadow.CANRIOC.all = ECanbRegs.CANRIOC.all;
ECanbShadow.CANRIOC.bit.RXFUNC = 1; //CANRX引脚 为CAN接收功能
ECanbRegs.CANRIOC.all = ECanbShadow.CANRIOC.all;
EDIS;
ECanbRegs.CANME.all = 0; //设置邮箱的ID号,先屏蔽掉邮箱,再设置

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.SCB = 1; //eCAN模式,邮箱0-31可用
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
// Mailboxs can be written to 16-bits or 32-bits at a time
// Write to the MSGID field of TRANSMIT mailboxes MBOX0 – 15 ECanbMboxes.MBOX0.MSGID.all = 0x0000AAA0;
// Write to the MSGID field of RECEIVE mailboxes MBOX16 – 31
ECanbMboxes.MBOX16.MSGID.all = 0x0000AAA1;

// 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.
ECanbShadow.CANMD.bit.MD0 = 0; //0号邮箱为发送邮箱
ECanbShadow.CANMD.bit.MD16 = 1;//16号邮箱为接收邮箱

ECanbRegs.CANME.all = 0x00010001; //使能2个邮箱

ECanbMboxes.MBOX0.MSGCTRL.all = 0x00000000; //初始化所有主设备控制区域为0,MCF所有的位都初始化为0
ECanbMboxes.MBOX16.MSGCTRL.all = 0x00000000;

// Specify that 8 bits will be sent/received
ECanbMboxes.MBOX0.MSGCTRL.bit.DLC = 8; //数据长度 8个BYTE
ECanbMboxes.MBOX16.MSGCTRL.bit.DLC = 8;

ECanbMboxes.MBOX0.MSGCTRL.bit.RTR= 0; //RTR要被初始化一个适当的值
ECanbMboxes.MBOX16.MSGCTRL.bit.RTR= 0;

ECanbMboxes.MBOX0.MSGCTRL.bit.TPL = 0; //设置发送优先级
ECanbMboxes.MBOX16.MSGCTRL.bit.TPL = 1;

ECanbMboxes.MBOX0.MDL.all = 0x0000000F;
ECanbMboxes.MBOX0.MDH.all = 0x000000FF;

EALLOW;
ECanbRegs.CANMIM.all = 0xFFFFFFFF;

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all; //邮箱屏蔽使能
ECanbShadow.CANMC.bit.CCR = 1; // 请求更改配置寄存器的权限
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 1 ); //等待数据发送结束之后CCE位被置位
EALLOW; //配置CAN的时序
ECanbShadow.CANBTC.all = ECanbRegs.CANBTC.all;
//波特率1M
ECanbShadow.CANBTC.bit.BRPREG = 9; // (BRPREG + 1) = 10 feeds a 15 MHz CAN clock
ECanbShadow.CANBTC.bit.TSEG2REG = 2; // to the CAN module. (150 / 10 = 15)5,1
ECanbShadow.CANBTC.bit.TSEG1REG = 10; // Bit time = 15,7,6
ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;
//ECanbShadow.CANBTC.bit.SAM = 1;
//ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 0;
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;

do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 0 );

EALLOW;
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.STM = 0; // Configure CAN for self-test mode,0是正常模式
ECanbShadow.CANMC.bit.SCB = 1; // eCAN mode (reqd to access 32 mailboxes)
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;

ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.bit.ME0=1;
ECanbShadow.CANME.bit.ME16=1;
ECanbRegs.CANME.all=ECanbShadow.CANME.all;
// Begin transmitting
for(;;)
{
ECanbShadow.CANTRS.all=ECanbRegs.CANTRS.all;
ECanbShadow.CANTRS.bit.TRS0 = 0x00000001; // Set TRS for all transmit mailboxes
ECanbRegs.CANTRS.all=ECanbShadow.CANTRS.all;

while(ECanbRegs.CANTA.bit.TA0!=1 ) {} // Wait for all TAn bits to be set
ECanbRegs.CANTA.bit.TA0 =1; // Clear all TAn

ECanbShadow.CANTA.all=ECanbRegs.CANTA.all;
ECanbShadow.CANTA.all = 0x00000001; // Clear all TAn
ECanbRegs.CANTA.all=ECanbShadow.CANTA.all;
}
}

代码急用,但是找不到问题所在,请您多协助,谢谢。

Yong Zhang8:

回复 HeiHei:

我也有同样的问题,自测模式已经改了。用示波器看,也确实有波形出来,但波形明显不是我发送的数据,并且程序会一直停留在while(ECanaRegs.CANTA.all != 0x0000FFFF ) {}// Wait for all TAn bits to be set上。
我的问题是,1. 为什么波形和数据不符,存在哪些可能的错误。2. CAN总线上只有发送节点,没有接收节点,是否可以正确发送?

赞(0)
未经允许不得转载:TI中文支持网 » 28335的CAN模块无法收发数据
分享到: 更多 (0)