有哪位 大神帮我看看 程序,目前初始化 过了,但是SJA1000不能发送数据。
#include<string.h>
#include<REG52.h>
#include<intrins.h>
#define SJA1000_BASE 0xf700 //定义sja1000的片选基址
#define CONTROL SJA1000_BASE+0x00 //内部控制寄存器
#define COMMAND SJA1000_BASE+0x01 //命令寄存器
#define STATUS SJA1000_BASE+0x02 //状态寄存器
#define INTERRUPT SJA1000_BASE+0x03 //中断寄存器
//#define ACR SJA1000_BASE+0x04 //验收代码寄存器
//#define AMR SJA1000_BASE+0x05 //验收屏蔽寄存器
//#define BTR0 SJA1000_BASE+0x06 //总线定时寄存器0
//#define BTR1 SJA1000_BASE+0x07 //总线定时寄存器1
#define OCR SJA1000_BASE+0x08 //输出控制寄存器
#define TEST SJA1000_BASE+0x09 //测试寄存器
/*****************************************/
#define TxBuffer1 SJA1000_BASE+0x0A //发送缓冲区1
#define TxBuffer2 SJA1000_BASE+0x0B //发送缓冲区2
#define TxBuffer3 SJA1000_BASE+0x0C //发送缓冲区3
#define TxBuffer4 SJA1000_BASE+0x0D //发送缓冲区4
#define TxBuffer5 SJA1000_BASE+0x0E //发送缓冲区5
#define TxBuffer6 SJA1000_BASE+0x0F //发送缓冲区6
#define TxBuffer7 SJA1000_BASE+0x10 //发送缓冲区7
#define TxBuffer8 SJA1000_BASE+0x11 //发送缓冲区8
#define TxBuffer9 SJA1000_BASE+0x12 //发送缓冲区9
#define TxBuffer10 SJA1000_BASE+0x13 //发送缓冲区10
/****************************************/
#define RxBuffer1 SJA1000_BASE+0x14 //接收缓冲区1
#define RxBuffer2 SJA1000_BASE+0x15 //接收缓冲区2
#define RxBuffer3 SJA1000_BASE+0x16 //接收缓冲区3
#define RxBuffer4 SJA1000_BASE+0x17 //接收缓冲区4
#define RxBuffer5 SJA1000_BASE+0x18 //接收缓冲区5
#define RxBuffer6 SJA1000_BASE+0x19 //接收缓冲区6
#define RxBuffer7 SJA1000_BASE+0x1A //接收缓冲区7
#define RxBuffer8 SJA1000_BASE+0x1B //接收缓冲区8
#define RxBuffer9 SJA1000_BASE+0x1C //接收缓冲区9
#define RxBuffer10 SJA1000_BASE+0x1D //接收缓冲区10
#define CDR SJA1000_BASE+0x1F //时钟分频寄存器
/****************************************
定义CAN地址指针
***************************************/
unsigned char xdata *SJA1000_Address;
unsigned char xdata BTR0 _at_ 0xf706;
unsigned char xdata BTR1 _at_ 0xf707;
unsigned char xdata ACR _at_ 0xf704;
unsigned char xdata AMR _at_ 0xf705;
/**************************
定义SJA1000操作的命令字
******************************/
#define TR_order 0x01 //发送请求命令
#define AT_order 0x02 //终止发送命令
#define RRB_order 0x04 //释放接受缓存区
#define CDO_order 0x08 //清除数据溢出
#define GTS_order 0x10 //进入顺眠状态命令
/************
CAN
通信基本函数
**************/
bit enter_RST (void);//进入复为工作模式函数
bit quit_RST (void);//退出复为工作模式函数
bit set_rate (unsigned char CAN_rate_num); //设置CAN的通信波特率函数
bit set_ACR_AMR(unsigned char ACR_DATA,unsigned char AMR_DATA);//设置验收代码寄存器和接收屏蔽寄存器
bit set_CLK (unsigned charSJA_OUT_MODE,unsigned char SJA_Clock_Out); //设置输出控制器和时钟分频寄存器
bit SJA_send_data(unsigned char * senddatabuf);//CAN总线发送数据函数
bit SJA_rcv_data(unsigned char * rcvdatabuf); //CAN总线接收数据函数
bit SJA_command_control(unsigned char order);//SJA1000控制命令函数
/*************/
/************/
bit create_communica
tion(void)
{
SJA1000_Address=TEST; //访问SJA1000的测试寄存器
*SJA1000_Address=0xaa;//写入测试值0xaa
if(*SJA1000_Address==0xaa)
{return 0;}
else
{return 1;}
}
/**********************/
/************************/
bit enter_RST(void)
{
unsigned char MID_DATA;//定义一个字节变量,用与储存SJA1000控制寄存器读出的数据
SJA1000_Address=CONTROL;//访问地址指向SJA1000的控制寄存器
MID_DATA=*SJA1000_Address;//保存原始值
*SJA1000_Address=(MID_DATA|0x01);//设置复位请求
if((*SJA1000_Address&0x01)==1) //读取SJA1000的控制寄存器数值
//判断复位是否有效
{return 0;}
else
{return 1;}
}
/************************
*************************/
bit quit_RST(void)
{
unsigned char MID_DATA;//定义一个字节变量,用于储存从SJA1000控制寄存器读取数据
SJA1000_Address=CONTROL; //访问地址指向SJA1000控制寄存器
MID_DATA=*SJA1000_Address;//保持原始值
*SJA1000_Address=(MID_DATA&0xfe);//清除复位请求
if((*SJA1000_Address&0x01)==0)//读取SJA1000的控制寄存器数值,判断清除复位请求是否有效
{return 0;}
else
{return 1;}
}
/**********************************/
//unsigned char code rate_tab[]={
// 0x53,0x2F, //20 kbit/s 预设值
// 0x87,0xFF, //40
// 0x47,0x2F, //50
// 0x83,0xFF, //80
// 0x43,0x2f, //100
// 0x03,0x1c, //125
// 0x81,0xfa, //200
// 0x01,0x1c, //250
// 0x80,0xfa, //400
// 0x00,0x1c, //500
// 0x80,0xb6, //666
// 0x00,0x16, //800
// 0x00,0x14, //1000
//};
///*****************/
//bit set_rate(unsigned char CAN_rate_num)
//{
// bit wrong_flag=1; //定义错误标志
// unsigned char BTR0_data,BTR1_data; //两字节变量 用于储存从波特率 数组中读出的数值
// unsigned char wrong_count=32; //32次错误报告
// if(rate_tab>12) //设置CAN通信波特率的数组列表中的序列 范围0~12
// {wrong_flag=1;} //如果超出范围,则报错,波特率设置失败
// else{
// while(--wrong_count) //最多32次设置SJA1000内部寄存器BTR0 BTR1数值
// {
// BTR0_data=rate_tab[CAN_rate_num*2];
// BTR1_data=rate_tab[CAN_rate_num*2+1]; //数组中读出波特率预设值
// SJA1000_Address=BTR0; //访问地址指向CAN总线定时寄存器0
// *SJA1000_Address=BTR0; //写入参数
// if(*SJA1000_Address!=BTR0_data)continue;//校验写入值
// wrong_flag=0;
// break;
// } //while语句结束
// }
// return wrong_flag;
//}
///************************/
//bit set_ARC_AMR(unsigned char ACR_DATA,unsigned char AMR_DATA)
// {
// SJA1000_Address=ACR; //访问地址指向SJA1000验收码寄存器
// *SJA1000_Address=ACR_DATA;//写入设置的ACR参数值
// if(*SJA1000_Address!=ACR_DATA)
// {return 1;}
// return 0;
//}
/********************/
bit set_CLK(unsigned char SJA_OUT_MODE,unsigned char SJA_Clock_Out)
{
SJA1000_Address=OCR;
*SJA1000_Address=SJA_OUT_MODE;
if(*SJA1000_Address!=SJA_OUT_MODE)
{return 1;}
SJA1000_Address=CDR;
*SJA1000_Address=SJA_Clock_Out;
return 0;
}
/****************/
bit SJA_send_data(unsigned char * senddatabuf)
{
unsigned char send_num,STATUS_data;
SJA1000_Address=STATUS;
STATUS_data=*SJA1000_Address;
if(STATUS_data & 0x10)
{return 1;}
if((STATUS_data&0x04)==0)
{return 1;}
if((STATUS_data&0x08)==0)
{return 1;}
SJA1000_Address=TxBuffer1;
if((senddatabuf[1]&0x10)==0)
{
send_num=(senddatabuf[1]&0x0f)+2;
}
else
{
send_num=2;
}
memcpy (SJA1000_Address,senddatabuf,send_num);
return 0;
}
/****************/
bit SJA_rcv_data(unsigned char * rcvdatabbuf)
{
unsigned char rcv_num,STATUS_data;
SJA1000_Address=STATUS;
STATUS_data= * SJA1000_Address;
if((STATUS_data&0x01)==0)
{return 1;}
SJA1000_Address = RxBuffer2;
if(( * SJA1000_Address&0x10)==0)
{
rcv_num=(*SJA1000_Address&0x0f)+2;
}
else
{rcv_num=2;}
SJA1000_Address=RxBuffer1;
memcpy(rcvdatabbuf,SJA1000_Address,rcv_num);
return 0;
}
/****************/
bit SJA_command_control(unsigned char order)
{
unsigned char STATUS_data;
SJA1000_Address=COMMAND;
*SJA1000_Address=order;
switch(order)
{
case TR_order:
return 0;
break;
case AT_order:
SJA1000_Address=STATUS;
STATUS_data=*SJA1000_Address;
if((STATUS_data & 0x20)==0)
{return 0;}
else
{return 1;}
break;
case RRB_order:
SJA1000_Address=STATUS;
STATUS_data=*SJA1000_Address;
if((STATUS_data & 0x10)==1)
{return 1;}
else
{return 0;}
break;
case CDO_order:
SJA1000_Address=STATUS;
STATUS_data=*SJA1000_Address;
if((STATUS_data & 0x02)==0)
{return 0;}
else
{return 1;}
break;
case GTS_order:
return 0;
break ;
default:
return 1;
break;
}
}
/*******************************
******************************
*******************************/
void Init_T0(void);
bit SJA_1000_Init(void);
void Delay(unsigned int x);
void read_p1(void);
void InitCPU(void);
void Can_DATA_Rcv(void);
void Can_DATA_Send(void);
void Can_error(void);
void Can_DATA_OVER(void);
//************************************
bit send_flag;
unsigned char data send_data[10],rcv_data[10];
unsigned char TIME_data;
unsigned char DATA_CHANGE;
unsigned char bdata Can_INT_DATA;
sbit rcv_flag=Can_INT_DATA^0;
sbit err_flag=Can_INT_DATA^2;
sbit Over_Flag=Can_INT_DATA^3;
sbit CAN_RESET=P2^0;
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_4=P2^4;
/***延时*****
************/
void Delay(unsigned int x)
{
unsigned int j;
while(x--)
{
for(j=0;j<125;j++)
{;}
}
}
//***************
void ex0_int(void) interrupt 0 using 1
{
SJA1000_Address=INTERRUPT;
Can_INT_DATA=* SJA1000_Address;
}
void T0_int(void) interrupt 1 using 2
{
TR0=0;
TIME_data--;
if(TIME_data==0)
{
TIME_data=30;
TH0=0x80;
TL0=0x60;
send_flag=1;
DATA_CHANGE++;
}
TR0=1;
}
/*************************/
void Init_T0(void)
{
TMOD=0x01;
TH0=0x80;
TL0=0x60;
TR0=1;
TIME_data=30;
DATA_CHANGE=0x00;
ET0=1;
}
/****************************/
void read_p1(void)
{
if(P2_0==0)
{send_data[4]=0X02;}
else{send_data[4]=0X03;}
if(P2_1==0)
{send_data[5]=0X02;}
else{send_data[5]=0X03;}
if(P2_2==0)
{send_data[6]=0X02;}
else{send_data[6]=0X03;}
if(P2_4==0)
{send_data[7]=0X02;}
else{send_data[7]=0X03;}
}
/*********************c初始化*******/
bit SJA_1000_Init(void)
{
if(enter_RST())
{return 1;}
if(create_communication())
{return 1;}
// if(set_rate(0x06))
// {return 1;}
// if(set_ACR_AMR(0xac,0x00))
// {return 1;}
BTR0=0x53;
BTR1=0x2f;
ACR=0x0A;
AMR=0x00;
if(set_CLK(0x1a,0x40)) //0xaa,0x40
{return 1;}
if(quit_RST())
{return 1;}
SJA1000_Address=CONTROL;
*SJA1000_Address|=0x1e;
return 0;
}
/********************初始化cpu**/
void InitCPU(void)
{
EA=1;
IT0=1;
EX0=1;
PX0=1;
Init_T0();
}
/******错误中断*/
void Can_error()
{
bit sja_status1;
do{
Delay(6);
sja_status1=Sja_1000_Init();
}
while(sja_status1);
}
/***************溢出中断处理、**/
void Can_DATA_OVER(void)
{
SJA_command_control(CDO_order);
SJA_command_control(RRB_order);
}
/***************/
void Can_DATA_Rcv()
{
SJA_rcv_data(rcv_data);
SJA_command_control(0x04);
}
/**************/
void Can_DATA_Send()
{
send_data[0]=0xAA;
send_data[1]=0x08;
send_data[2]=0x05;
send_data[3]=DATA_CHANGE;
SJA_send_data(send_data);
SJA_command_control(0x01);
}
/*******************/
void main(void)
{
bit sja_status;
Delay(1);
CAN_RESET=0;
do{
Delay(6);
LED2=~LED2;
sja_status=SJA_1000_Init();
}while(sja_status);
InitCPU();
LED0=0;
Can_INT_DATA=0x00;
while(1)
{
read_p1();
if(_testbit_(rcv_flag))
{Can_DATA_Rcv();}
if(_testbit_(send_flag))
{
Can_DATA_Send();
LED1=~LED1;
}
if(_testbit_(Over_Flag))
{Can_DATA_OVER();}
if(_testbit_(err_flag))
{
LED0=1 ;
Can_error();
LED0=0;
}
}
}
一周热门 更多>