您的位置 首页 电子

根据SJA1000的CAN总线测验程序

代码如下:/*************定义地址指针,指向基址***********/unsignedcharxdata*SJA_base_Adr=SJA_BaseAdr;/*************定

代码如下:

/*************界说地址指针,指向基址***********/
unsigned char xdata *SJA_base_Adr = SJA_BaseAdr;

/*************界说SJA1000读写缓冲区的数据结构***********/
struct BASICCAN_BUFStruct{
unsigned char IDH8;
unsigned char IDL_R_DLC;
unsigned char Frame_Data[2];
}receive_BUF,send_BUF;

/****************************************************************
*函数功用:写SJA1000的寄存器
*进口参数:地址 SJAREG_ADR,数据 setting
*出口参数:无
****************************************************************/
void Write_SJA1000(unsigned char SJAREG_ADR, unsigned char setting)
{
*(SJA_base_Adr+SJAREG_ADR)=setting;
}

/****************************************************************
*函数功用:读SJA1000的寄存器
*进口参数:地址 SJAREG_ADR,数据 setting
*出口参数:无
****************************************************************/
unsigned char Read_SJA1000(unsigned char SJAREG_ADR)
{
unsigned char SJAREG_data;

SJAREG_data=*(SJA_base_Adr+SJAREG_ADR);

return(SJAREG_data);
}

/****************************************************************
*函数功用:测验SJA1000是否正常衔接
*进口参数:无
*出口参数:connect_OK
****************************************************************/
bit Judge_SJA1000_connect(void)
{
bit connect_OK=0;

Write_SJA1000(REG_TEST,0xAA); //写AA到测验寄存器(地址09)

if(Read_SJA1000(0x09)==0xAA)
{
connect_OK=1; //衔接正常
}
else
{
connect_OK=0; //衔接毛病
}
return(connect_OK);
}

/****************************************************************
*函数功用:设置SJA1000为作业形式
*进口参数:无
*出口参数:SJA_mode
****************************************************************/
bit Setting_SJA1000_workingmode(void)
{
bit SJA_mode;
unsigned char CONTROL_REGdata;

CONTROL_REGdata=Read_SJA1000(REG_CONTROL);
CONTROL_REGdata&=0xFE;

Write_SJA1000(REG_CONTROL,CONTROL_REGdata);

if((Read_SJA1000(REG_CONTROL)&0x01)!=0x01)
{
SJA_mode=1; //置作业形式成功
}
else
{
SJA_mode=0; //置作业形式失利
}

return(SJA_mode);
}

/****************************************************************
*函数功用:设置SJA1000为复位形式
*进口参数:无
*出口参数:SJA_mode
****************************************************************/
bit Setting_SJA1000_resetmode(void)
{
bit SJA_mode;
unsigned char CONTROL_REGdata;

CONTROL_REGdata=Read_SJA1000(REG_CONTROL);
CONTROL_REGdata|=0x01;

Write_SJA1000(REG_CONTROL,CONTROL_REGdata);

if((Read_SJA1000(REG_CONTROL)&0x01)==0x01)
{
SJA_mode=1; //置复位形式成功
}
else
{
SJA_mode=0; //置复位形式失利
}

return(SJA_mode);
}

/****************************************************************
*函数功用:设置SJA1000波特率
*进口参数:无
*出口参数:setting_success
****************************************************************/
bit Setting_SJA1000_rate(void)
{
bit setting_success;

while(Setting_SJA1000_resetmode()==0)
{
Setting_SJA1000_resetmode(); //设置SJA作业在复位形式
}

Write_SJA1000(REG_BTR0,SJA_BTR0);
Write_SJA1000(REG_BTR1,SJA_BTR1);

if((Read_SJA1000(REG_BTR0)==SJA_BTR0)&(Read_SJA1000(REG_BTR1)==SJA_BTR1))
{
setting_success=1; //波特率设置成功
}
else
{
setting_success=0; //波特率设置失利
}

return(setting_success);
}

/****************************************************************
*函数功用:设置SJA1000的滤波寄存器和屏蔽寄存器
*进口参数:无
*出口参数:setting_success
****************************************************************/
bit Setting_SJA1000_dataselect(void)
{
bit setting_success;

while(Setting_SJA1000_resetmode()==0)
{
Setting_SJA1000_resetmode(); //设置SJA作业在复位形式
}

Write_SJA1000(REG_ACR,SJA_ACR);
Write_SJA1000(REG_AMR,SJA_AMR);

if((Read_SJA1000(REG_ACR)==SJA_ACR)&(Read_SJA1000(REG_AMR)==SJA_AMR))
{
setting_success=1; //滤波器设置成功
}
else
{
setting_success=0; //滤波器设置失利
}

return(setting_success);
}

/****************************************************************
*函数功用:设置SJA1000的时钟分频器
*进口参数:无
*出口参数:setting_success
****************************************************************/
bit Setting_SJA1000_CDR(void)
{
bit setting_success;

while(Setting_SJA1000_resetmode()==0)
{
Setting_SJA1000_resetmode(); //设置SJA作业在复位形式
}

Write_SJA1000(REG_CDR,SJA_CDR);

if(Read_SJA1000(REG_CDR)==SJA_CDR)
{
setting_success=1; //滤波器设置成功
}
else
{
setting_success=0; //滤波器设置失利
}

return(setting_success);
}

/****************************************************************
*函数功用:设置SJA1000的输出操控寄存器
*进口参数:无
*出口参数:setting_success
****************************************************************/
bit Setting_SJA1000_OCR(void)
{
bit setting_success;

while(Setting_SJA1000_resetmode()==0)
{
Setting_SJA1000_resetmode(); //设置SJA作业在复位形式
}

Write_SJA1000(REG_OCR,SJA_OCR);

if(Read_SJA1000(REG_OCR)==SJA_OCR)
{
setting_success=1; //滤波器设置成功
}
else
{
setting_success=0; //滤波器设置失利
}

return(setting_success);
}

/****************************************************************
*函数功用:初始化SJA1000
*进口参数:无
*出口参数:setting_success
****************************************************************/
void Initial_SJA1000(void)
{
while(Judge_SJA1000_connect==0);

do
{
Setting_SJA1000_resetmode();
}
while(Setting_SJA1000_resetmode()==0);

while(Setting_SJA1000_CDR()==0);
while(Setting_SJA1000_rate()==0);
while(Setting_SJA1000_OCR()==0);
while(Setting_SJA1000_dataselect()==0)

do
{
Setting_SJA1000_workingmode();
}
while(Setting_SJA1000_workingmode()==0);

}

/****************************************************************
*函数功用:初始化发送数据
*进口参数:无
*出口参数:setting_success,指示初始化是否成功
****************************************************************/
bit Initial_transmitter_BUF(void)
{
bit setting_success=0;

send_BUF.IDH8=0x30;
send_BUF.IDL_R_DLC=0x22; //IDL_R_DLC。4即为RTR位,数据=0,长途=1;
send_BUF.Frame_Data[0]=0x10;
send_BUF.Frame_Data[1]=0x00;

setting_success = 1;

return(setting_success);
}

/****************************************************************
*函数功用:对SJA1000发送数据
*进口参数:无
*出口参数:无
****************************************************************/
void Transmitter_SJA1000(void)
{
unsigned char state;
unsigned char i;

do
{
Setting_SJA1000_workingmode();
}
while(Setting_SJA1000_workingmode()==0);

while(Initial_transmitter_BUF()==0); //初始化发送数据

do
{
state = Read_SJA1000(REG_STATUS);//REG_STATUS为SJA1000的状况寄存器
}
//查询SJA1000是否处于接纳状况,发送结束状况,或许发送缓存器被锁;
while( ((state&0x10)==0x10) & ((state&0x08)!=0x08) & ((state&0x04)!=0x04) );

Write_SJA1000(REG_TxBuffer1,send_BUF.IDH8);
Write_SJA1000(REG_TxBuffer2,send_BUF.IDL_R_DLC);
for(i=0;i<2;i++)
{
Write_SJA1000(REG_TxBuffer3+i,send_BUF.Frame_Data[i]);
}

Write_SJA1000(REG_COMMAND,TR_CMD);

do
{
state = Read_SJA1000(REG_STATUS);//REG_STATUS为SJA1000的状况寄存器
}
while( (state&0x08)!=0x08);//查询SJA1000是否发送结束状况
}

/***************************************************
***************************************************/

void main(void)
{
unsigned char i;
Initial_SJA1000();
delay1ms(1);

while(1)
{
if((P1_0==0)|(P1_1==0)|(P1_2==0))
{
delay1ms(5);

if((P1_0==0)|(P1_1==0)|(P1_2==0))
{
while((P1_0==0)|(P1_1==0)|(P1_2==0));
for(i=0;i<100;i++)
{
Transmitter_SJA1000();
}
}
}
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/dianzi/276344.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部