首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

请教高手CAN通信的问题!!!

请教高手CAN通信的问题!!!

我用的单片机是9s12dp256b,CAN初始化之后,总线同步标志CAN2CTL0_SYNCH一直是“0”,是怎么回事啊?是波特率设的不对吗?16M晶振,CAN2BTR0=0x47;CAN2BTR1=0x23;这样设置之后波特率是250Kb/s吗?


[此贴子已经被作者于2005-9-23 11:35:40编辑过]

void MSCAN0Init(void) { CAN0CTL1 = 0x87; CAN0BTR0 = 0x01; /* set SJW=1, Prescaler value=2*/ CAN0BTR1 = 0x65; /* set TSEG2=7,TSEG1=6 Bit Rate=500k*/ //CAN0BTR0 = 0x4F; /*CAN0BTR0 = 0x4F; set SJW=2, Prescaler value=16*/ //CAN0BTR1 = 0xE7; /*CAN0BTR1 = 0xE7; set TSEG2=7,TSEG1=8 Bit Rate=62.5k*/ CAN0IDMR0 = 0xFF; /*no filter,define to accept all coming message*/ CAN0IDMR1 = 0xFF; CAN0IDMR2 = 0xFF; CAN0IDMR3 = 0xFF; CAN0IDMR4 = 0xFF; CAN0IDMR5 = 0xFF; CAN0IDMR6 = 0xFF; CAN0IDMR7 = 0xFF; CAN0CTL0_INITRQ = 0; /*enable normal operation mode*/ while(CAN0CTL1_INITAK != 0) { } CAN0CTL0_TIME = 1; /*enable internal timer*/ CAN0CTL0_WUPE = 1; /*WAKE UP enable*/ CAN0RIER = 0x01; /*only enable receive buffer full interrupt*/ } /*************************************************************************************** Function Name : MSCAN0Trans Programmer : syding Date : 2004.07.05 Arguments : none Returns : none Notes : This function sends message onto the CAN bus. ****************************************************************************************/ void MSCAN0Trans(byte * msgout) { unsigned char cansel; CAN0TBSEL = CAN0TFLG; /*select one available transmit buffer*/ cansel = CAN0TBSEL; CAN0TXIDR0 = 0x11; /*DP256:0x11; AG60:0x33; GZ16:0x22; MPC555:0x44*/ CAN0TXIDR1 = 0xff; /*Extended ID*/ CAN0TXIDR2 = 0xff; CAN0TXIDR3 = 0xfe; CAN0TXDSR0 = *msgout; CAN0TXDSR1 = *(msgout+1); CAN0TXDSR2 = *(msgout+2); CAN0TXDSR3 = *(msgout+3); CAN0TXDSR4 = *(msgout+4); CAN0TXDSR5 = *(msgout+5); CAN0TXDSR6 = *(msgout+6); CAN0TXDSR7 = *(msgout+7); CAN0TXDLR = 8; CAN0TXTBPR = 0; if (CAN0TXDSR4 != 0xff){ *ID = 0x11; *(ID+1) = 0xff; *(ID+2) = 0xff; *(ID+3) = 0xFE; //SCI0_TRAN_MSCAN(ID,msgout,0); } CAN0TFLG = cansel; /*clearing TxE to lauch transmission*/ PTM_PTM2 = 1; time_delay_can(); PTM_PTM2 = 0; } /*************************************************************************************** Function Name : MSCAN0Rec Programmer : syding Date : 2004.08.17 Arguments : none Returns : 1/0 Notes : This function receives message from the CAN bus. ****************************************************************************************/ #pragma CODE_SEG __NEAR_SEG NON_BANKED #pragma TRAP_PROC void interrupt MSCAN0Rec(void) { if(index<100) { ID0[index] = CAN0RXIDR0; ID1[index] = CAN0RXIDR1; ID2[index] = CAN0RXIDR2; ID3[index] = CAN0RXIDR3; Data0[index] = CAN0RXDSR0; Data1[index] = CAN0RXDSR1; Data2[index]= CAN0RXDSR2; Data3[index] = CAN0RXDSR3; Data4[index] = CAN0RXDSR4; Data5[index] = CAN0RXDSR5; Data6[index] = CAN0RXDSR6; Data7[index] = CAN0RXDSR7; Length[index] = CAN0RXDLR; index=index+1; } else { SCI0_TRAN_MSCAN(); } CAN0RFLG = 0x01; /*clearing RXF to free the RxFG*/ } #pragma CODE_SEG DEFAULT
**         Protocol
**             Time segment 1          : 6
**             Time segment 2          : 7
**             RSJ                     : 1
**             16 Mhz
CAN0CTL0 = 0x01;                     /* Set the control register */
CAN0CTL1 = 0x80;                     /* Set the control register */

CAN0BTR0 = 0x43;                     /* Set the device timing register */
CAN0BTR1 = 0x76;                     /* Set the device timing register */
CAN0CTL1_CLKSRC = 0x00;              /* Select the clock source from crystal */
if (CAN0TXDSR4 != 0xff){
*ID = 0x11;
*(ID+1) = 0xff;
*(ID+2) = 0xff;
*(ID+3) = 0xFE;
//SCI0_TRAN_MSCAN(ID,msgout,0);
}
这部分语句不明白,可以解释一下吗
发送成功后,向串口发送CAN的ID和报文。这是用来调试的。不用管它
PTM_PTM2 = 1;
在上面的语句中,PTM2是CAN1的RxCAN,我觉得应该是PTM_PTM0 = 1;是这样子吗
如何判断CAN发送成功与否
hi  thank you for your help
发送成功后,CAN发送标志寄存器的缓冲区空的标志位置1
如果失败,发送错误加1,硬件会重新发送
PTM_PTM2 = 1;
在上面的语句中,PTM2是CAN1的RxCAN,我觉得应该是PTM_PTM0 = 1;是这样子吗
发送数据后,我得发送标志寄存器一直为0,就是说没发送出去,请问这是什么原因呢,我用的是9s12dt128
返回列表