void SJA1000_Init(void)
{
uchar i,a,b,i_temp=0,TempData,j_temp=0;
CLI();
SJA100HardWareRest(); //硬件复位
//Read_SJA1000(CAN_IR); //读中断寄存器,清除中断位
SJAEntryResetMode(); //进入复位模式
DelayUs(5);
SR_status = Read_SJA1000(CAN_SR); //读状态
//进入Pelican模式
Write_SJA1000(CAN_CDR,0xc0); //选择Pelican模式,CBP=1,激活RX0; 关闭TX1中断与时钟输出。
SR_status = Read_SJA1000(CAN_SR); //读状态
DelayUs(5);
//自检模式,自发自收检测
//Write_SJA1000(CAN_MOD,0x04); //配置模式寄存器,双滤波,自发自收
//接收所有报文
Write_SJA1000(CAN_AMR0,0xff); //配置验收屏蔽AMR0=0FFH
Write_SJA1000(CAN_AMR1,0xff); //配置验收屏蔽AMR1=0FFH
Write_SJA1000(CAN_AMR2,0xff); //配置验收屏蔽AMR2=0FFH
Write_SJA1000(CAN_AMR3,0xff); //配置验收屏蔽AMR3=0FFH
Write_SJA1000(CAN_ACR0,0x00); //配置验收代码ACR1=0:
Write_SJA1000(CAN_ACR1,0x00); //配置验收代码ACR1=0:
Write_SJA1000(CAN_ACR2,0x00); //配置验收代码ACR1=0:
Write_SJA1000(CAN_ACR3,0x00); //配置验收代码ACR3=0;
Write_SJA1000(CAN_TXERR,0x00);
Write_SJA1000(CAN_RXERR,0x00);
Write_SJA1000(CAN_ECC,0x00);
Write_SJA1000(CAN_RBSA,0x00);
//设置波特率(16MHZ晶振,125Kb)
Write_SJA1000(CAN_BTR0,0x03); //配置总线定时--1255kbps 0x6e 03
Write_SJA1000(CAN_BTR1,0x1C); // 0xff af
Write_SJA1000(CAN_OCR,0xaa); //配置输出控制
Write_SJA1000(CAN_EWLR,0xff); //配置错误报警限制为255
Write_SJA1000(CAN_CMR,0x0c); // 清楚数据溢出和释放接收缓冲器
// 退出复位模式,进入工作模式
SJAQuitResetMode();
SR_status = Read_SJA1000(CAN_SR); //读状态
//Write_SJA1000(CAN_TXB+4,ID3); //配置发送缓冲区的ID3-
// Write_SJA1000(CAN_IER,0x01); //配置SJA10000中断-错误报警/发送/接收中断 0x07
Write_SJA1000(CAN_IER,0x81); //使能SJA1000接收中断位,和总线错误中断位
SEI();
}
//******************************SJA1000发送程序*********************************
void send_sja1000(void)
{
uchar s,i,canstatus,canstatus1,canstatus2,canstatus3,canstatus4;
//CLI();
canstatus = Read_SJA1000(CAN_SR); //读状态
canstatus1 = Read_SJA1000(CAN_MOD); //读模式
canstatus2 = Read_SJA1000(15); //TX错误计数
canstatus3 = Read_SJA1000(14); //RX错误计数
canstatus4 = Read_SJA1000(12); //错误代码捕捉
for( i=0;i<5;i++ );
{
canstatus = Read_SJA1000(CAN_SR);
}
if((canstatus & 0x0C)== 0x0C) //查询SJA1000是否处于接收状态,当SJA1000不处于接收状态时才可继续执行
{ //SR.3=0,发送请求未处理完,等待直到SR.3=1
goto SJA1000; //SR.2=0,发送缓冲器被锁
}
else
{
LEDSysMode = 0;
return;
}
SJA1000:
//发送数据写进发送缓冲区中
WriteSJARegBlock(16,Send_CAN_Info_ID,5); //扩展帧,向发送缓冲区写入5个识别码
Write_SJA1000(21,DataText[0]); //扩展帧,向发送缓冲区写入8个数据
Write_SJA1000(22,DataText[1]);
Write_SJA1000(23,DataText[2]);
Write_SJA1000(24,DataText[3]);
Write_SJA1000(25,DataText[4]);
Write_SJA1000(27,DataText[6]);
Write_SJA1000(26,DataText[5]);
Write_SJA1000(28,DataText[7]);
// 启动发送
Write_SJA1000(CAN_CMR,0x01);//使能发送请求,发送数据。(0x03,单次发送,有错误不重新发送;0x01发送请求命令,当错误时可重发;0x10产生一次自接收性质的报文发送)
SEI();
DataText[7]=0x00;
while((canstatus&0x08) ==0) // 检测是否发送完毕
{
canstatus = Read_SJA1000(CAN_SR); //读状态
}
LEDSysMode = 1;
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |