本人在用飞思卡尔单片机做CAN通信,奇怪的是发送和接收数据段都正确,ID段都是不对,请指教 本人QQ 378819233
//时钟配置
/*********************************/
/*函数功能:总线时钟倍频到24MHz */
/*********************************/
void CLK_init(void)
{
CAN0CTL1 = 0x80;/* 进入初始化阶段,CAN时钟采用晶振源输入 */
// CAN0BTR0 = 0xc1; /* 预分频因子 1+1 = 2 */
CAN0BTR0 = 0xc0; /* 预分频因子 1+1 = 2 2011.7-9.13 */
CAN0BTR1 = 0x3A; /*采样数,TSEG1及TSEG2 */
//以上已经初步设定CAN速率为 250KBps
CAN3CTL1 = 0x80;
CAN3BTR0 = 0xc0;
CAN3BTR1 = 0x3A;
}
//初始化
void mscan0_init(void)
{
CAN0CTL0 = 0x01; // 进入初始化模式 INITRQ 1:初始化模式请求 0:正常模式
while (!(CAN0CTL1&0x01)){};
// CAN3IDAC = 0x00;
CAN0CTL1_LISTEN=0;
CAN0IDAC = 0x10; // 2个32位验收过滤器,过滤器0命中
// 2011.9.7 IDAM1 IDAM0 0 0 Two 32-bit acceptance ?lters
//-----------------
//ID验收寄存器
CAN0IDAR0 = 0;
CAN0IDAR1 = 0;
CAN0IDAR2 = 0;
CAN0IDAR3 = 0;
CAN0IDAR4 = CAN0IDAR0;
CAN0IDAR5 = CAN0IDAR1;
CAN0IDAR6 = CAN0IDAR2;
CAN0IDAR7 = CAN0IDAR3;
//ID验收屏蔽寄存器 关闭过滤器
CAN0IDMR0 = 0xFF;
CAN0IDMR1 = 0xFF;
CAN0IDMR2 = 0xFF;
CAN0IDMR3 = 0xFF;
CAN0IDMR4 = 0xFF;
CAN0IDMR5 = 0xFF;
CAN0IDMR6 = 0xFF;
CAN0IDMR7 = 0xFF;
CAN0CTL1 = 0xC0; //CAN使能,CAN采用总线时钟 1:采用总线时钟 0:晶振时钟
CAN0CTL0 = 0x00; // 退出初始化模式
CAN0RIER = 0x00; //接收缓冲区满中断使能 2011.9.15 禁止发送中断
CAN0TIER = 0x00;
while ((CAN0CTL1&0x01) != 0){}; //等待进入正常模式
while (CAN0CTL0_SYNCH == 0){};
//CAN0RIER = 0x01; //接收缓冲区满中断使能
//can_variable_init();
}
/*******************************************/
/*----CAN0发送程序----*/
/*******************************************/
int CAN0_Transmit(INT32U ID3,INT8U * DataBUF)
{
INT8U bufbit = 0xFF; //空闲区号
// INT8U addr[4];
INT8U cansel;
//查找空闲的发送缓冲区
if ((CAN0T标志寄存器 & 0x07) == 0x00)
return bufbit;//返回空闲区号
cansel = CAN0T标志寄存器;
CAN0TBSEL = cansel; //选择缓冲区
cansel = CAN0TBSEL;
CAN0TXIDR0 = 0b01111000; //ID28-ID21
CAN0TXIDR1 = 0b00011001; //ID20-ID18,SRR=1,IDE=1,ID17-ID15,扩展模式
CAN0TXIDR2 = 0b11111000; //ID14-ID07
CAN0TXIDR3 = 0b10001000; //ID06-ID00,RTR=0,数据帧 */
CAN0TXDSR0 = DataBUF[0];
CAN0TXDSR1 = DataBUF[1];
CAN0TXDSR2 = DataBUF[2];
CAN0TXDSR3 = DataBUF[3];
CAN0TXDSR4 = DataBUF[4];
CAN0TXDSR5 = DataBUF[5];
CAN0TXDSR6 = DataBUF[6];
CAN0TXDSR7 = DataBUF[7];
CAN0TXDLR=0x08; //设置发送缓冲区的数据长度,8个字节
CAN0TXTBPR=0x00;
CAN0T标志寄存器=cansel; //发送
return (1);
} |