我使用的是HCS12D64,在使用CAN时,CAN只能接收不能发送,发送时测量CANL和地、CANH和地之间的信号,发现信号不对,一旦发送一次,这种不对的信号一直存在,为什么呢?程序如下:
初始化:
void initMSCAN(void) { //MSCAN初始化 CAN0CTL0_INITRQ = 1; while(CAN0CTL1_INITAK!=1); CAN0CTL1_CANE = 1; // MODRR=02; MODRR_MODRR0=0; MODRR_MODRR1=1; CAN0CTL1_CLKSRC = 0; /* 选择晶振频率 */ #ifdef LoopMode CAN0CTL1_LOOPB = 1; #else CAN0CTL1_LOOPB = 0; #endif CAN0CTL1_LISTEN = 0; /* 用4M晶振125 kb rate */ CAN0BTR0=0x01; CAN0BTR1=0x1C; CAN0IDAC = 0; /* two 32bit acceptance filter*/ /*下面仅配置为接受ID=02的扩展贞格式,此时Hit 0 命中,但对于ID=0的标准贞也接受,此时Hit 1命中*/ CAN0IDAR0 =0x00; CAN0IDAR1 =0x00; CAN0IDAR2 =0x00; CAN0IDAR3 =0x04; //接受ID=2 CAN0IDMR0 = 0x00; CAN0IDMR1 =0x18;//屏蔽SRR和IDE CAN0IDMR2 = 0x00; CAN0IDMR3 = 0x03;//屏蔽RTR CAN0IDAR4 =0x00;//注意:如果标准贞ID设为1000,则也可接收到,因为1000已超出11位,所以此时的ID也为0 CAN0IDAR5 =0x00; CAN0IDAR6 =0x00; CAN0IDAR7 =0x00; CAN0IDMR4 = 0x00; CAN0IDMR5 = 0x00; CAN0IDMR6 = 0x00; CAN0IDMR7 = 0x00;
CAN0CTL0_INITRQ = 0; while(CAN0CTL1_INITAK!=0); while ((CAN0CTL1&0x01) != 0){}; /* Wait for Normal Mode */ CAN0RIER_RXFIE = 1; /* 接收中断使能,禁止发送中断 */ }
发送函数
void CAN_Transmit(uchar id)
{ //CAN发送 unsigned char mask; mask = CAN0TFLG; CAN0TBSEL= mask; CAN0TXIDR0 = 0xff; CAN0TXIDR1 = 0xff; //扩展桢 CAN0TXIDR2 = 0xff; CAN0TXIDR3 = 1; //发送ID=2 CAN0TXDSR0 = 200; CAN0TXDSR1 = 212; CAN0TXDSR2 = 111; CAN0TXDSR3 = 23; CAN0TXDSR4 = 89; CAN0TXDSR5 = 155; CAN0TXDSR6 = 199; CAN0TXDSR7 = 0; CAN0TXDLR = 0x08; CAN0TXTBPR = 0x00; CAN0TFLG = mask; }
|