控制/MCU
基于C8051F040单片机的CAN总线通信 硬件部分
硬件部分电路结构如下:
程序参考自童长飞编著的《C8051F系列单片机开发与C语言编程》例程12-1。
1.基本设置
int n; //看门狗禁止 WDTCN = 0x07; WDTCN = 0xDE; WDTCN = 0xAD; SFRPAGE = 0x0F; //交叉开关使能,但没有进行外围设备配置 XBR0 = 0x00; XBR1 = 0x00; XBR2 = 0x40; XBR3 = 0x00; //管脚输出配置,P0口为开漏输出,其中P0.6接上拉电阻,P0为数字输入口 SFRPAGE = 0x0F; P0MDOUT = 0x00; P1MDIN = 0xFF; //晶振配置 OSCXCN = 0x77;//选择外部晶振22.1MHz。 //系统时钟为外部时钟二分频:22.1 MHz / 2 = 11.05 MHz for (n = 0; n < 255; n++); while ((OSCXCN & 0x80) == 0); CLKSEL |= 0x01;2.CAN消息对象清零
void clear_msg_objects (void) //将所有消息清零 { uchar i; SFRPAGE = CAN0_PAGE; CAN0ADR = IF1CMDMSK; CAN0DATL = 0xFF; for (i=1;i<33;i++) { CAN0ADR = IF1CMDRQST; CAN0DATL = i; } }3.CAN发送初始化
void init_msg_object_TX (char MsgNum,uint id) { uint temp; SFRPAGE = CAN0_PAGE; CAN0ADR = IF1CMDMSK; //指向IF1 Command Mask Registers CAN0DAT = 0x00b3; /* IF1 Command Mask Registers =0x00b3 WR/RD=1,Mask=0,Arb=1,Control=1,ClrIntPnd=0,TxRqst=0,DataA=1,DataB=1,一次发送8字节数据*/ CAN0ADR = IF1ARB1; CAN0DAT = 0x0000; /*IF1 Arbitration Registers1 =0x0000,即ID15-0=0*/ temp=id<<2;//标准id为ID28-ID18,所以要左移2位 temp&=0x1fff; temp|=0xa000; CAN0DAT = temp; //地址自增,指向IF1 Arbitration Registers2 /*IF1 Arbitration Registers2=101(id)00b MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=1,为发送*/ CAN0DAT = 0x0088; /*IF1 Message Control Registers=0x0088 NewDat=0,MsgLst=0,IntPnd=0,UMask=0,TxIE=0,RxIE=0,RmtEn=0,TxRqst=0 EoB=1,DLC3-0=1000,即数据长度为8*/ CAN0ADR = IF1CMDRQST; CAN0DAT = MsgNum; /*IF1 Command Request Registers=MsgNum,将以上配置写入MsgNum号消息*/ }4.CAN接收初始化
void init_msg_object_RX (char MsgNum,uchar id) { uint temp; SFRPAGE = CAN0_PAGE; CAN0ADR = IF2CMDMSK; CAN0DAT = 0x00fb; /* IF2 Command Mask Registers =0x00fb WR/RD=1,Mask=1,Arb=1,Control=1,ClrIntPnd=1,TxRqst=0,DataA=1,DataB=1,一次接收8字节数据*/ CAN0ADR = IF2MSK1; CAN0DAT = 0x0000; CAN0DAT = 0x0000; CAN0ADR = IF2ARB1; CAN0DAT = 0x0000; /*IF2 Arbitration Registers1 =0x0000,即ID15-0=0*/ temp=id<<2;//标准id为ID28-ID18,所以要左移2位 temp&=0x1fff; temp|=0x8000; CAN0DAT = temp; /*IF2 Arbitration Registers2=100(id)00b MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=0,为接收*/ CAN0DAT = 0x0488; /*IF2 Message Control Registers=0x0488 NewDat=0,MsgLst=0,IntPnd=0,UMask=0,TxIE=0,RxIE=1,接收中断使能;RmtEn=0,TxRqst=0 EoB=1,DLC3-0=1000,即数据长度为8*/ CAN0ADR = IF2CMDRQST; CAN0DATL = MsgNum; /*IF2 Command Request Registers=MsgNum,将以上配置写入MsgNum号消息*/ }5.CAN波特率设置
Calculation of the CAN bit timing :
System clock
f_sys = 22.1184 MHz/2 = 11.0592 MHz.
System clock period t_sys = 1/f_sys = 90.422454 ns.
CAN time quantum
tq = t_sys (at BRP = 0)
Desired bit rate is 1 MBit/s, desired bit time is 1000 ns.
Actual bit time = 11 tq = 996.65ns ~ 1000 ns
Actual bit rate is 1.005381818 MBit/s = Desired bit rate+0.5381%
CAN bus length = 10 m, with 5 ns/m signal delay time.
Propagation delay time : 2*(transceiver loop delay + bus line delay) = 400 ns
(maximum loop delay between CAN nodes)
Prop_Seg = 5 tq = 452 ns ( >= 400 ns).
Sync_Seg = 1 tq
Phase_seg1 + Phase_Seg2 = (11-6) tq = 5 tq
Phase_seg1 <= Phase_Seg2, => Phase_seg1 = 2 tq and Phase_Seg2 = 3 tq
SJW = (min(Phase_Seg1, 4) tq = 2 tq
TSEG1 = (Prop_Seg + Phase_Seg1 - 1) = 6
TSEG2 = (Phase_Seg2 - 1)
= 2
SJW_p = (SJW - 1)
= 1
Bit Timing Register = BRP + SJW_p*0x0040 = TSEG1*0x0100 + TSEG2*0x1000 = 2640
Clock tolerance df :
A: df < min(Phase_Seg1, Phase_Seg2) / (2 * (13*bit_time - Phase_Seg2))
B: df < SJW / (20 * bit_time)
A: df < 2/(2*(13*11-3)) = 1/(141-3) = 1/138 = 0.7246%
B: df < 2/(20*11)
= 1/110 = 0.9091%
Actual clock tolerance is 0.7246% - 0.5381% = 0.1865% (no problem for quartz)
SFRPAGE = CAN0_PAGE; CAN0CN=0X41; CAN0ADR=BITREG; CAN0DAT=0x2640;//调波特率6.发送函数
void transmit (char MsgNum) { uchar num; SFRPAGE = CAN0_PAGE; CAN0ADR = IF1CMDMSK; CAN0DAT = 0x0087; /* IF1 Command Mask Registers =0x0087 WR/RD=1,Mask=0,Arb=0,Control=0,ClrIntPnd=0,TxRqst=1,DataA=1,DataB=1,一次发送8字节数据*/ CAN0ADR = IF1DATA1; /*将8字节数据写入IF1*/ for(num=0;num<4;num++) { CAN0DATH=sdata[2*num+1]; CAN0DATL=sdata[2*num]; } CAN0ADR = IF1CMDRQST; CAN0DATL = MsgNum; //将以上配置写入MsgNum号CAN消息 }7.接收函数
void receive_data (uchar MsgNum) { uchar i; SFRPAGE = CAN0_PAGE; CAN0ADR = IF2CMDMSK; CAN0DATL = 0x0f; /* IF1 Command Mask Registers =0xxx0f WR/RD=0,Mask=0,Arb=0,Control=0,ClrIntPnd=1,NewDat=1,DataA=1,DataB=1,一次发送8字节数据*/ CAN0ADR = IF2CMDRQST; CAN0DATL = MsgNum; //指向MsgNum号消息 CAN0ADR = IF2DATA1; for(i=0;i<4;i++) rdata[i].tempval=CAN0DAT;//接收数据 isnewdata=1; } 最后现象:单片机发送成功,PC端能成功接收发送的数据。而单片机接收CAN总线数据时异常。TJA1050芯片的RXD引脚能检测到电压变化,而单片机Status Register寄存器中RxOk位为‘1’,但没有产生接收中断,接收消息对象中的数据也未改变。
全部0条评论
快来发表一下你的评论吧 !