RS485-CAN 轉(zhuǎn)換器的設(shè)計(jì)
圖4:RS485-CAN 協(xié)議模型
3.1CAN 接口軟件設(shè)計(jì)
CAN總線數(shù)據(jù)鏈路層結(jié)構(gòu)可分為邏輯鏈路控制(LLC)子層和媒體訪問(wèn)控制(MAC)子層。LLC子層主要完成幀接收濾波,超載通告和恢復(fù)管理,而MAC子層結(jié)構(gòu)功能模塊包括發(fā)送和接收兩個(gè)部分。對(duì)于CAN總線數(shù)據(jù)鏈路層的各種協(xié)議已經(jīng)被集成到SJA1000芯片中,應(yīng)用時(shí)只要對(duì)SJA1000芯片的控制寄存器進(jìn)行相應(yīng)的初始化即可進(jìn)行通信,而數(shù)據(jù)的接收和發(fā)送等也是通過(guò)讀寫(xiě)SJA1000內(nèi)部寄存器來(lái)實(shí)現(xiàn)的。這里只給出SJA1000的初始化源程序,其它程序可參考文獻(xiàn)[2]進(jìn)行編寫(xiě)。
SJA1000開(kāi)始工作之前,首先要判斷SJA1000與微處理器的連接是否正確,在確認(rèn)正確后開(kāi)始SJA1000的初始化,之后SJA1000進(jìn)入工作模式,然后才可以進(jìn)行數(shù)據(jù)的讀寫(xiě)。
typedef struct _CAN_PORTSTRUCT /*SJA1000初始化配置文件結(jié)構(gòu)體*/
{
FIL filter; /*濾波方式*/
LIS lisen; /*只聽(tīng)模式/正常模式*/
TES test; /*自測(cè)試模式/正常模式*/
SLE sleep; /*睡眠模式設(shè)置*/
BAUD baud_rate; /*波特率*/
MODEL workmodel ; /*工作模式*/
UCHAR mask1;
UCHAR mask2;
UCHAR mask3;
UCHAR mask4;
UCHAR code1;
UCHAR code2;
UCHAR code3;
UCHAR code4;
}CAN_PORT,*CANPORTPTR;
STATUS sja1000Init(CANPORTPTR pDev) /*SJA1000初始化*/
{
UCHAR value;
canBoardInit(); /*進(jìn)入復(fù)位模式*/
canBoard_canOutByte(SJA1000_MOD,MOD_RM); /*確定CAN控制器工作模式*/
value = canBoard_canInByte(SJA1000_CDR);
if(pDev->workmodel == Peli) value|= PeliCAN ;
else value &= ~PeliCAN;
/*設(shè)置模式寄存器(只在PeliCAN模式可訪問(wèn))*/
canBoard_canOutByte(SJA1000_CDR,value|0x48);
value = canBoard_canInByte(SJA1000_MOD);
if(pDev->sleep == SNORM) value &=~(MOD_SM); /*睡眠模式*/
else value|= MOD_SM; /*正常模式*/
if(pDev->lisen == LNORM) value&= ~(MOD_LOM); /*正常模式*/
else value|= MOD_LOM; /*只聽(tīng)模式*/
if(pDev->test == TNORM) value&= ~(MOD_STM); /*正常模式*/
else value|= MOD_STM; /*自檢測(cè)模式*/
if(pDev->filter == SINGLE) value|= MOD_AFM; /*單向驗(yàn)收濾波*/
else value &=(~MOD_AFM); /*雙向驗(yàn)收濾波*/
canBoard_canOutByte(SJA1000_MOD,value);
switch (pDev->baud_rate)
{
case B20: //波特率為20kpbs
canBoard_canOutByte(SJA1000_BTR0,0x07);
canBoard_canOutByte(SJA1000_BTR1,0x7F);
break;
case B80: //波特率為80kpbs
canBoard_canOutByte(SJA1000_BTR0,0x01);
canBoard_canOutByte(SJA1000_BTR1,0x7F);
break;
case B250: //波特率為250kpbs
canBoard_canOutByte(SJA1000_BTR0,0x00);
canBoard_canOutByte(SJA1000_BTR1,0x1C);
break;
case B500: //波特率為500kpbs
canBoard_canOutByte(SJA1000_BTR0,0x00);
canBoard_canOutByte(SJA1000_BTR1,0x14);
break;
default: //默認(rèn)波特率為20kpbs
canBoard_canOutByte(SJA1000_BTR0,0x07);
canBoard_canOutByte(SJA1000_BTR1,0x7F);
}
canBoard_canOutByte(SJA1000_CMR,0); /*設(shè)置命令寄存器*/
canBoard_canOutByte(SJA1000_EWLR,0x60); /*設(shè)置錯(cuò)誤報(bào)警限額寄存器*/
canBoard_canOutByte(SJA1000_IER,0); /*不使能所有中斷*/
canBoard_canOutByte(SJA1000_OCR,0xaa); /*輸出控制寄存器設(shè)置*/
canBoard_canOutByte(SJA1000_TXERR,0x0); /*設(shè)置發(fā)送錯(cuò)誤計(jì)數(shù)器*/
canBoard_canOutByte(SJA1000_RXERR,0x0); /*設(shè)置接受錯(cuò)誤計(jì)數(shù)器*/
canBoard_canOutByte(SJA1000_RMC,0); /*設(shè)置接收?qǐng)?bào)文計(jì)數(shù)器*/
canBoard_canOutByte(SJA1000_ACR0,pDev->code1);
canBoard_canOutByte(SJA1000_ACR1,pDev->code2); /*設(shè)置驗(yàn)收代碼寄存器*/
canBoard_canOutByte(SJA1000_ACR2,pDev->code3);
canBoard_canOutByte(SJA1000_ACR3,pDev->code4);
canBoard_canOutByte(SJA1000_AMR0,pDev->mask4);
canBoard_canOutByte(SJA1000_AMR1,pDev->mask4); /*設(shè)置驗(yàn)收屏蔽寄存器*/
canBoard_canOutByte(SJA1000_AMR2,pDev->mask4);
評(píng)論