can总线多机通讯 发送报文
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include"SJA1000REG_1.h"
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<intrins.h>
#define STD_FRAMEID_LENTH 2//标准帧ID长度
#define EXT_FRAMEID_LENTH 4//扩展帧ID长度
temp=temp&(~BitValue);
WriteSJAReg(RegAdr,temp);
if(ReadSJAReg(RegAdr)==temp)
{
status=1;
}
else
{
status=0;
}
return(status);
}
char WriteSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len)//连续写多个寄存器RegAdr寄存器起始地址len要连续写入的寄存器数
UserSendData.CanData[6]=UserData[6];
UserSendData.CanData[7]=UserData[7];
}
void timerInit(void)//串口初始化
{
TMOD&=~T0_MASK;//清除旧的设置(#define T0_MASK 0x0f)
TMOD|=0x01;
{
status=0;
}
else
{
*databuf=ReadSJAReg(REG_CAN_RXFMINFO);
dlc=(*databuf&0x0f);
if(dlc>8)
{
dlc=8;
}
switch(*databuf&0xC0)
{
case 0x00:
unsigned char UserData[8];
typedef struct//结构定义
{
unsigned longID;
unsigned charFF;
unsigned charRTR;
unsigned charDLC;
unsigned charCanData[8];
}
CanFrame_t;
CanFrame_t UserSendData;
break;
case 0xc0:
len=EXT_FRAMEID_LENTH+1;
break;
default:
len=0;
status=0;
break;
}
if(len>0)
{
WriteSJARegBlock(REG_CAN_TXFMINFO,databuf,len);
SetSJASendCmd(cmd);
//void SJA1000_Config_Filter(char mode,char *Filter);//配置滤波器模式和滤波参数mode大于0为双滤波器,其他为单滤波
unsigned char SJA_CAN_Filter[8]={ 0xea,0x40,0x00,0x00,0x00,0x0f,0xff,0xff};
#define T0_MASK 0x0f
sbit LED=P2^5;
sbit SJA1000_RST=P2^6;
unsigned char xdata *SJA_CS_Point=(unsigned char xdata*)SJA_BASE_ADDR;
void SJA1000_Init(unsigned char btr0,unsigned char btr1,unsigned char *filter);
char ReadSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len);//连续读寄存器
char SetSJASendCmd(unsigned char cmd);//发送模式cmd 0为正常发送1为单次发送2为自发自收
#define LOW_BYTE(x) (unsigned char)(x)
#define HIGH_BYTE(x) (unsigned char)((unsigned int)(x)>>8)
#define OSCCLK 11059200UL
#define CPUCLK (OSCCLK/12)
#define SJA_BASE_ADDR 0X7E00
{
unsigned char i;
if(len!=0)
{
for(i=0;i<len;i++)
{
WriteSJAReg(RegAdr+i,ValueBuf[i]);
}
}
return len;
}
char ReadSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len)//连续读多个寄存器RegAdr寄存器起始地址len要读取的寄存器数
WriteSJAReg(REG_CAN_OCR,0x1A);//配置输出引脚TX0与RX0,推挽输出
ClearBitMask(REG_CAN_MOD,RM_RR_BIT);//推出复位模式
}
void WriteSJAReg(unsigned char RegAdr,unsigned char Value)//写SJA1000寄存器
void UserDataDeal()
{
UserData[0]=0x55;
}
void NumDataDeal()
{
UserSendData.ID=0x251;
UserSendData.FF=0;
UserSendData.RTR=0;
UserSendData.DLC=8;
UserSendData.CanData[0]=UserData[0];
}
return(status);
}
char ClearBitMask(unsigned char RegAdr,unsigned char BitValue)//将指定寄存器的指定位清零
{
char status=0;
unsigned char temp;
temp=ReadSJAReg(RegAdr);
status=1;
}
}
return(status);
}
char SJARcvData(unsigned char *databuf)//SJA1000接收函数读取接收缓冲区的CAN报文
{
char status=1;
unsigned char len;
unsigned char dlc;
if((ReadSJAReg(REG_CAN_SR)&RBS_BIT)==0)
{
unsigned char ret;
switch(cmd)
{
default:
case 0:
ret=SetBitMask(REG_CAN_CMR,TR_BIT);
break;
case 1:
ret=SetBitMask(REG_CAN_CMR,TR_BIT|AT_BIT);
break;
case 2:
ret=SetBitMask(REG_CAN_CMR,TR_BIT|SRR_BIT);
UserSendData.CanData[1]=UserData[1];
UserSendData.CanData[2]=UserData[2];
UserSendData.CanData[3]=UserData[3];
UserSendData.CanData[4]=UserData[4];
UserSendData.CanData[5]=UserData[5];
{
*(SJA_CS_Point+RegAdr)=ValuΒιβλιοθήκη Baidu;
return;
}
unsigned char ReadSJAReg(unsigned char RegAdr)//读SJA1000寄存器
{
return(*(SJA_CS_Point+RegAdr));
}
char SetBitMask(unsigned char RegAdr,unsigned char BitValue)//设置指定寄存器指定位为1
{
char status=0;
unsigned char temp;
temp=ReadSJAReg(RegAdr);
temp=temp|BitValue;
WriteSJAReg(RegAdr,temp);
if(ReadSJAReg(RegAdr)==temp)
{
status=1;
}
else
{
status=0;
char SJASendData(unsigned char *databuf,unsigned char cmd);//sja1000发送函数,将数据写入sja1000发送缓冲区,再将其发到总线上
char SJARcvData(unsigned char *databuf);//SJA1000接收函数读取接收缓冲区的CAN报文
char ClearBitMask(unsigned char RegAdr,unsigned char BitValue);//清除寄存器特定位
char WriteSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len);//连续写寄存器
{
unsigned char i;
if(len!=0)
{
for(i=0;i<len;i++)
{
ValueBuf[i]=ReadSJAReg(RegAdr+i);
}
}
return len;
}
char SetSJASendCmd(unsigned char cmd)//发送模式cmd 0为正常发送1为单次发送2为自发自收
void WriteSJAReg(unsigned char RegAdr,unsigned char Value);//写寄存器
unsigned char ReadSJAReg(unsigned char RegAdr);//读寄存器
char SetBitMask(unsigned char RegAdr,unsigned char BitValue);//设置寄存器特定位
break;
case 0xff:
ret=SetBitMask(REG_CAN_CMR,AT_BIT);
break;
}
return ret;
}
char SJASendData(unsigned char *databuf,unsigned char cmd)//sja1000发送函数,将数据写入sja1000发送缓冲区,再将其发到总线上
}
void timerDelay(unsigned int n)//延时0.01×n
{
do{
TL0=LOW_BYTE(65536UL-CPUCLK/100);
TH0=HIGH_BYTE(65536UL-CPUCLK/100);
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}
while(--n!=0);
{
char status=1;
unsigned char len;
unsigned char dlc;
if((ReadSJAReg(REG_CAN_SR)&(TBS_BIT|TCS_BIT))!=(TBS_BIT|TCS_BIT))
{
status=0;
}
else
{
dlc=(*databuf&0x0f);
WriteSJAReg(REG_CAN_MOD,0x09);//配置模式寄存器,选择单滤波、正常模式
WriteSJARegBlock(REG_CAN_ACR0,filter,8);//配置验收代码/屏蔽寄存器
WriteSJAReg(REG_CAN_BTR0,btr0);//配置总线定时器0
WriteSJAReg(REG_CAN_BTR1,btr1);//配置总线定时器1
}
void SJA1000_Init(unsigned char btr0,unsigned char btr1,unsigned char *filter)
{
SetBitMask(REG_CAN_MOD,RM_RR_BIT);//进入复位模式
WriteSJAReg(REG_CAN_CDR,0x48);//配置时钟分频,选择peliCAN模式
if(dlc>8)
{
dlc=8;
}
switch(*databuf&0xc0)
{
case 0x00:
len=STD_FRAMEID_LENTH+dlc+1;
break;
case 0x40:
len=STD_FRAMEID_LENTH+1;
break;
case 0x80:
len=EXT_FRAMEID_LENTH+dlc+1;
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<intrins.h>
#define STD_FRAMEID_LENTH 2//标准帧ID长度
#define EXT_FRAMEID_LENTH 4//扩展帧ID长度
temp=temp&(~BitValue);
WriteSJAReg(RegAdr,temp);
if(ReadSJAReg(RegAdr)==temp)
{
status=1;
}
else
{
status=0;
}
return(status);
}
char WriteSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len)//连续写多个寄存器RegAdr寄存器起始地址len要连续写入的寄存器数
UserSendData.CanData[6]=UserData[6];
UserSendData.CanData[7]=UserData[7];
}
void timerInit(void)//串口初始化
{
TMOD&=~T0_MASK;//清除旧的设置(#define T0_MASK 0x0f)
TMOD|=0x01;
{
status=0;
}
else
{
*databuf=ReadSJAReg(REG_CAN_RXFMINFO);
dlc=(*databuf&0x0f);
if(dlc>8)
{
dlc=8;
}
switch(*databuf&0xC0)
{
case 0x00:
unsigned char UserData[8];
typedef struct//结构定义
{
unsigned longID;
unsigned charFF;
unsigned charRTR;
unsigned charDLC;
unsigned charCanData[8];
}
CanFrame_t;
CanFrame_t UserSendData;
break;
case 0xc0:
len=EXT_FRAMEID_LENTH+1;
break;
default:
len=0;
status=0;
break;
}
if(len>0)
{
WriteSJARegBlock(REG_CAN_TXFMINFO,databuf,len);
SetSJASendCmd(cmd);
//void SJA1000_Config_Filter(char mode,char *Filter);//配置滤波器模式和滤波参数mode大于0为双滤波器,其他为单滤波
unsigned char SJA_CAN_Filter[8]={ 0xea,0x40,0x00,0x00,0x00,0x0f,0xff,0xff};
#define T0_MASK 0x0f
sbit LED=P2^5;
sbit SJA1000_RST=P2^6;
unsigned char xdata *SJA_CS_Point=(unsigned char xdata*)SJA_BASE_ADDR;
void SJA1000_Init(unsigned char btr0,unsigned char btr1,unsigned char *filter);
char ReadSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len);//连续读寄存器
char SetSJASendCmd(unsigned char cmd);//发送模式cmd 0为正常发送1为单次发送2为自发自收
#define LOW_BYTE(x) (unsigned char)(x)
#define HIGH_BYTE(x) (unsigned char)((unsigned int)(x)>>8)
#define OSCCLK 11059200UL
#define CPUCLK (OSCCLK/12)
#define SJA_BASE_ADDR 0X7E00
{
unsigned char i;
if(len!=0)
{
for(i=0;i<len;i++)
{
WriteSJAReg(RegAdr+i,ValueBuf[i]);
}
}
return len;
}
char ReadSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len)//连续读多个寄存器RegAdr寄存器起始地址len要读取的寄存器数
WriteSJAReg(REG_CAN_OCR,0x1A);//配置输出引脚TX0与RX0,推挽输出
ClearBitMask(REG_CAN_MOD,RM_RR_BIT);//推出复位模式
}
void WriteSJAReg(unsigned char RegAdr,unsigned char Value)//写SJA1000寄存器
void UserDataDeal()
{
UserData[0]=0x55;
}
void NumDataDeal()
{
UserSendData.ID=0x251;
UserSendData.FF=0;
UserSendData.RTR=0;
UserSendData.DLC=8;
UserSendData.CanData[0]=UserData[0];
}
return(status);
}
char ClearBitMask(unsigned char RegAdr,unsigned char BitValue)//将指定寄存器的指定位清零
{
char status=0;
unsigned char temp;
temp=ReadSJAReg(RegAdr);
status=1;
}
}
return(status);
}
char SJARcvData(unsigned char *databuf)//SJA1000接收函数读取接收缓冲区的CAN报文
{
char status=1;
unsigned char len;
unsigned char dlc;
if((ReadSJAReg(REG_CAN_SR)&RBS_BIT)==0)
{
unsigned char ret;
switch(cmd)
{
default:
case 0:
ret=SetBitMask(REG_CAN_CMR,TR_BIT);
break;
case 1:
ret=SetBitMask(REG_CAN_CMR,TR_BIT|AT_BIT);
break;
case 2:
ret=SetBitMask(REG_CAN_CMR,TR_BIT|SRR_BIT);
UserSendData.CanData[1]=UserData[1];
UserSendData.CanData[2]=UserData[2];
UserSendData.CanData[3]=UserData[3];
UserSendData.CanData[4]=UserData[4];
UserSendData.CanData[5]=UserData[5];
{
*(SJA_CS_Point+RegAdr)=ValuΒιβλιοθήκη Baidu;
return;
}
unsigned char ReadSJAReg(unsigned char RegAdr)//读SJA1000寄存器
{
return(*(SJA_CS_Point+RegAdr));
}
char SetBitMask(unsigned char RegAdr,unsigned char BitValue)//设置指定寄存器指定位为1
{
char status=0;
unsigned char temp;
temp=ReadSJAReg(RegAdr);
temp=temp|BitValue;
WriteSJAReg(RegAdr,temp);
if(ReadSJAReg(RegAdr)==temp)
{
status=1;
}
else
{
status=0;
char SJASendData(unsigned char *databuf,unsigned char cmd);//sja1000发送函数,将数据写入sja1000发送缓冲区,再将其发到总线上
char SJARcvData(unsigned char *databuf);//SJA1000接收函数读取接收缓冲区的CAN报文
char ClearBitMask(unsigned char RegAdr,unsigned char BitValue);//清除寄存器特定位
char WriteSJARegBlock(unsigned char RegAdr,unsigned char *ValueBuf,unsigned char len);//连续写寄存器
{
unsigned char i;
if(len!=0)
{
for(i=0;i<len;i++)
{
ValueBuf[i]=ReadSJAReg(RegAdr+i);
}
}
return len;
}
char SetSJASendCmd(unsigned char cmd)//发送模式cmd 0为正常发送1为单次发送2为自发自收
void WriteSJAReg(unsigned char RegAdr,unsigned char Value);//写寄存器
unsigned char ReadSJAReg(unsigned char RegAdr);//读寄存器
char SetBitMask(unsigned char RegAdr,unsigned char BitValue);//设置寄存器特定位
break;
case 0xff:
ret=SetBitMask(REG_CAN_CMR,AT_BIT);
break;
}
return ret;
}
char SJASendData(unsigned char *databuf,unsigned char cmd)//sja1000发送函数,将数据写入sja1000发送缓冲区,再将其发到总线上
}
void timerDelay(unsigned int n)//延时0.01×n
{
do{
TL0=LOW_BYTE(65536UL-CPUCLK/100);
TH0=HIGH_BYTE(65536UL-CPUCLK/100);
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}
while(--n!=0);
{
char status=1;
unsigned char len;
unsigned char dlc;
if((ReadSJAReg(REG_CAN_SR)&(TBS_BIT|TCS_BIT))!=(TBS_BIT|TCS_BIT))
{
status=0;
}
else
{
dlc=(*databuf&0x0f);
WriteSJAReg(REG_CAN_MOD,0x09);//配置模式寄存器,选择单滤波、正常模式
WriteSJARegBlock(REG_CAN_ACR0,filter,8);//配置验收代码/屏蔽寄存器
WriteSJAReg(REG_CAN_BTR0,btr0);//配置总线定时器0
WriteSJAReg(REG_CAN_BTR1,btr1);//配置总线定时器1
}
void SJA1000_Init(unsigned char btr0,unsigned char btr1,unsigned char *filter)
{
SetBitMask(REG_CAN_MOD,RM_RR_BIT);//进入复位模式
WriteSJAReg(REG_CAN_CDR,0x48);//配置时钟分频,选择peliCAN模式
if(dlc>8)
{
dlc=8;
}
switch(*databuf&0xc0)
{
case 0x00:
len=STD_FRAMEID_LENTH+dlc+1;
break;
case 0x40:
len=STD_FRAMEID_LENTH+1;
break;
case 0x80:
len=EXT_FRAMEID_LENTH+dlc+1;