//初始化CAN通讯
void Init_Can(void)
{
/**复位MCP2515**/
SPIReset();
delay(20);
/**设置为配置模式**/
//CAN_Set_Var(0x0F,0XE0,0X80);
CAN_Write_SS(RXM0SIDH, 0x00);
CAN_Write_SS(RXM0SIDL,0x00);
CAN_Write_SS(RXF0SIDH,0x00);
CAN_Write_SS(RXF0SIDL,0x00);
/**设置通讯速率 晶振16M 速率125K**/
//CAN_Write_SS(0x2A,0X03);
//CAN_Write_SS(0x29,0XB9);
//CAN_Write_SS(0x28,0X04);
//设置波特率为125Kbps
//set CNF1,SJW=00,长度为1TQ,BRP=49,TQ=[2*(BRP+1)]/Fsoc=2*50/8M=12.5us,16M
CAN_Write_SS(CNF1,CAN_125Kbps);//CNF1=0X03
//set CNF2,SAM=0,在采样点对总线进行一次采样,PHSEG1=(2+1)TQ=3TQ,PRSEG=(0+1)TQ=1TQ
CAN_Write_SS(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);//3,1;6,2
//set CNF3,PHSEG2=(2+1)TQ=3TQ,同时当CANCTRL.CLKEN=1时设定CLKOUT引脚为时间输出使能位
CAN_Write_SS(CNF3,PHSEG2_3TQ);//3;7
//set TXB0,设置发送缓冲器0的标识符和发送的数据,以及发送的数据长度
CAN_Write_SS(TXB0SIDH,0x06);//设置发送缓冲器0的标准标识符,待修改***
CAN_Write_SS(TXB0SIDL,0x00);//用到标准标识符
CAN_Write_SS(TXB0DLC,DLC_8);//设置发送数据的长度为8个字节,DLC_1
//CAN_Write_SS(TXB0D0,0x1E);//有待修改及确定是否使用
//CAN_Write_SS(TXB0D1,0x10);//有待修改及确定是否使用
/*set TXB1
CAN_Write_SS(TXB1SIDH,0x50); //Set TXB0 SIDH
CAN_Write_SS(TXB1SIDL,0x00); //Set TXB0 SIDL
CAN_Write_SS(TXB1DLC,0x40 | DLC_8); //Set DLC = 3 bytes and RTR bit*/
//设置接收缓冲器0的标识符和初始化数据
CAN_Write_SS(RXB0SIDH,0x00);//设置接收缓冲器0的标准标识符,待修改***
CAN_Write_SS(RXB0SIDL,0x60);//用到标准标识符
CAN_Write_SS(RXB0CTRL,0x60);//仅仅接收标准标识符的有效信息,FIILHIT0=0表示RXB0 ,采用FILHIT0
CAN_Write_SS(RXB0DLC,DLC_8);//设置接收数据的长度为8个字节
CAN_Write_SS(RXF0SIDH,0xFF);//初始化接收滤波器0,待修改***
CAN_Write_SS(RXF0SIDL,0xE0);
CAN_Write_SS(RXM0SIDH,0xFF);//初始化接收屏蔽器0,待修改***
CAN_Write_SS(RXM0SIDL,0xE0);
//设置接收缓冲器0中断
CAN_Write_SS(CANINTF,0x00);//清空中断标志位
CAN_Write_SS(CANINTE,0x01);//接收缓冲器0满中断使能位
CAN_Write_SS(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//设置正常模式
delay(10);
dummy=CAN_Read_SS(CANSTAT);
if (OPMODE_NORMAL != (dummy && 0xE0))
CAN_Write_SS(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//判断进入正常工作模式
//CAN_Write_SS(CANCTRL,0x00);//选定正常工作模式
////0x00 仅接收标准或扩展标识符
////0x60 关闭接收所有数据
//CAN_Write_SS(0x60,0X60);
//
////滤波
//CAN_Write_SS(0x00,0);
//CAN_Write_SS(0x01,0);
//CAN_Write_SS(0x02,0);
//CAN_Write_SS(0x03,0);
//
////屏蔽
//CAN_Write_SS(0x20,0);
//CAN_Write_SS(0x21,0);
//CAN_Write_SS(0x22,0);
//
////接收数据产生中断
//CAN_Write_SS(0x2B,0X01);
//
////环回模式
////CAN_Set_Var(0x0F,0XE0,0X40);
////工作模式
//CAN_Set_Var(0x0F,0XE0,0);
}
|