pinda_ 发表于 2014-2-20 15:03
用的是主芯片自带的,不知道,你调试的出的什么问题,能不能说明,SJA1000,没用过,不过看了下,应该差 ...
void init_SJA1000(void)
{
//SJA1000 的初始化
// Uint8 Status;
// DINT;
CAN_Loop_Num=0;
do
{// .0=1---reset MODRe,进入复位模式,以便设置相应的寄存器,防止未进入复位模式,重复写入
SJA_Wr_CMD=RM_BIT|AFM_BIT;
Wr_SJA1_CMD (SJA_MOD_ADR);
Wr_SJA2_CMD (SJA_MOD_ADR);
Read_SJA1_Status (SJA_MOD_ADR);
CAN_Loop_Num++;
}
while((!(SJA1_Status & RM_BIT))&&(CAN_Loop_Num<20));
delay(100);
SJA_Wr_CMD=CANMode_BIT|CLKOff_BIT;// CDR.3=1--时钟关闭, .7=0---basic CAN, .7=1---Peli CAN
Wr_SJA1_CMD (SJA_CDR_ADR);
Wr_SJA2_CMD (SJA_CDR_ADR);
SJA_Wr_CMD=0x82;//0x41;//0x03;=2M,BRP+1=3
Wr_SJA1_CMD (SJA_BTR0_ADR);
Wr_SJA2_CMD (SJA_BTR0_ADR);
//tSCL=2 tCLK (32 BRP.5+16 BRP.4+8 BRP.3+4 BRP.2+2 BRP.1+BRP.0+1)
//BTR0,0~5,BRP;6\7=SJW0\1
//BTR1,0~3,Tseg1,4~6Tseg2,7=SAM,采样次数=1时,三次
//Tseg1+1=4,Tseg2+1=3
SJA_Wr_CMD=0xA3;//0x1C;//0x08;//12M晶振,波特率250Kbps;0x1C;//16M晶振,波特率125Kbps
Wr_SJA1_CMD (SJA_BTR1_ADR);
Wr_SJA2_CMD (SJA_BTR1_ADR);
SJA_Wr_CMD=RIE_BIT;// .0=1--接收中断使能; .1=0--关闭发送中断使能
Wr_SJA1_CMD (SJA_IER_ADR);
Wr_SJA2_CMD (SJA_IER_ADR);
SJA_Wr_CMD = NormalMode|Tx0PshPull|Tx1Float;//NormalMode|Tx0PullDn|OCPOL1_BIT|Tx1PullUp;// 配置输出控制寄存器
Wr_SJA1_CMD (SJA_OCR_ADR);
Wr_SJA2_CMD (SJA_OCR_ADR);
SJA_Wr_CMD = RRB_BIT;//释放接收缓冲器
Wr_SJA1_CMD (SJA_CMR_ADR);
Wr_SJA2_CMD (SJA_CMR_ADR);
SJA_Wr_CMD = 0x11;
Wr_SJA1_CMD (SJA_ACR0_ADR);
Wr_SJA2_CMD (SJA_ACR0_ADR);
SJA_Wr_CMD = 0x22;
Wr_SJA1_CMD (SJA_ACR1_ADR);
Wr_SJA2_CMD (SJA_ACR1_ADR);
SJA_Wr_CMD = 0x33;
Wr_SJA1_CMD (SJA_ACR2_ADR);
Wr_SJA2_CMD (SJA_ACR2_ADR);
SJA_Wr_CMD = 0x44;//初始化标示码
Wr_SJA1_CMD (SJA_ACR3_ADR);
Wr_SJA2_CMD (SJA_ACR3_ADR);
SJA_Wr_CMD = 0xff;//初始化掩码
Wr_SJA1_CMD (SJA_AMR0_ADR);
Wr_SJA2_CMD (SJA_AMR0_ADR);
SJA_Wr_CMD = 0xff;
Wr_SJA1_CMD (SJA_AMR1_ADR);
Wr_SJA2_CMD (SJA_AMR1_ADR);
SJA_Wr_CMD = 0xff;
Wr_SJA1_CMD (SJA_AMR2_ADR);
Wr_SJA2_CMD (SJA_AMR2_ADR);
SJA_Wr_CMD = 0xff;
Wr_SJA1_CMD (SJA_AMR3_ADR);
Wr_SJA2_CMD (SJA_AMR3_ADR);
CAN_Loop_Num=0;
do
{//确保进入自接收模式
SJA_Wr_CMD=AFM_BIT;//0=正常模式;AFM_BIT=滤波器模式;//STM_BIT=自接漳J?
Wr_SJA1_CMD (SJA_MOD_ADR);
Wr_SJA2_CMD (SJA_MOD_ADR);
Read_SJA1_Status (SJA_MOD_ADR);
CAN_Loop_Num++;
//SJA_MOD = STM_BIT;
//Status = SJA_MOD;
}
while(( SJA1_Status & 0x01)&&(CAN_Loop_Num<20)); //& AFM_BIT) );
delay(100);
// EINT;
}
void write_sja1(void)
{
// DINT;
//初始化标示码头信息
TX1_buffer[0] = 0x88;//.7=1扩展帧;.6=0数据帧; 0~3数据长度
TX1_buffer[1] = 0x0;//本节点地址,标识符,扩展帧,16bit
TX1_buffer[2] = 0x00;//本节点地址,标识符,标准帧,11bit
TX1_buffer[3] = 0x0;//本节点地址,标识符,扩展帧,13位
TX1_buffer[4] = 0xf8;//本节点地址,标识符,扩展帧,bit0~2没用
//初始化发送数据单元
TX1_buffer[5] = 0x11;
TX1_buffer[6] = 0x22;
TX1_buffer[7] = 0x33;
TX1_buffer[8] = 0x44;//
TX1_buffer[9] = 0x55;//
TX1_buffer[10] = 0x66;//
TX1_buffer[11] = 0x77;//
TX1_buffer[12] = SJA_Test_Num;//0x88;//
SJA_Test_Num=SJA_Test_Num+1;
CAN_Loop_Num=0;
do
{
Read_SJA1_Status (SJA_SR_ADR);
CAN_Loop_Num++;
//Status = SJA_SR;
}
while((SJA1_Status & RS_BIT)&&(CAN_Loop_Num<10)); //SR.4=1 正在接收,等待
CAN_Loop_Num=0;
do
{
//Status = SJA_SR;
Read_SJA1_Status (SJA_SR_ADR);
CAN_Loop_Num++;
}
while((!(SJA1_Status & TCS_BIT))&&(CAN_Loop_Num<10)); //SR.3=0,发送请求未处理完,等待
CAN_Loop_Num=0;
do
{
Read_SJA1_Status (SJA_SR_ADR);
CAN_Loop_Num++;
//Status = SJA_SR;
}
while((!(SJA1_Status & TBS_BIT))&&(CAN_Loop_Num<10)); //SR.2=0,发送缓冲器被锁。等待
//SJA_Wr_CMD = 0xff;
//Wr_SJA1_CMD (SJA_AMR2_ADR);
for(SJA1_Tx_num=0;SJA1_Tx_num<13;SJA1_Tx_num++)
{
SJA_Wr_CMD=TX1_buffer[SJA1_Tx_num];
Wr_SJA1_CMD (SJA_TBSR0_ADR+SJA1_Tx_num);
}
SJA_Wr_CMD = TR_BIT;//发送请求位
Wr_SJA1_CMD (SJA_CMR_ADR);
// EINT;
}
void write_sja2(void)
{
// DINT;
//初始化标示码头信息
TX2_buffer[0] = 0x88;//.7=0扩展帧;.6=0数据帧; 0~3数据长度
TX2_buffer[1] = 0x0;//本节点地址,标识符,扩展帧,16bit
TX2_buffer[2] = 0x00;//本节点地址,标识符,标准帧,11bit
TX2_buffer[3] = 0x0;//本节点地址,标识符,扩展帧,13位
TX2_buffer[4] = 0xf8;//本节点地址,标识符,扩展帧,bit0~2没用
//初始化发送数据单元
TX2_buffer[5] = 0x99;
TX2_buffer[6] = 0x22;
TX2_buffer[7] = 0x33;
TX2_buffer[8] = 0x44;//
TX2_buffer[9] = 0x55;//
TX2_buffer[10] = 0x66;//
TX2_buffer[11] = 0x77;//
TX2_buffer[12] = 0x88;//
CAN_Loop_Num=0;
do
{
Read_SJA2_Status (SJA_SR_ADR);
CAN_Loop_Num++;
//Status = SJA_SR;
}
while((SJA2_Status & RS_BIT)&&(CAN_Loop_Num<20)); //SR.4=1 正在接收,等待
CAN_Loop_Num=0;
do
{
//Status = SJA_SR;
CAN_Loop_Num++;
Read_SJA2_Status (SJA_SR_ADR);
}
while((!(SJA2_Status & TCS_BIT))&&(CAN_Loop_Num<20)); //SR.3=0,发送请求未处理完,等待
CAN_Loop_Num=0;
do
{
Read_SJA2_Status (SJA_SR_ADR);
CAN_Loop_Num++;
//Status = SJA_SR;
}
while((!(SJA2_Status & TBS_BIT))&&(CAN_Loop_Num<20)); //SR.2=0,发送缓冲鞅凰5却
//SJA_Wr_CMD = 0xff;
//Wr_SJA2_CMD (SJA_AMR2_ADR);
for(SJA2_Tx_num=0;SJA2_Tx_num<13;SJA2_Tx_num++)
{
SJA_Wr_CMD=TX2_buffer[SJA2_Tx_num];
Wr_SJA2_CMD (SJA_TBSR0_ADR+SJA2_Tx_num);
}
SJA_Wr_CMD = SRR_BIT;
Wr_SJA2_CMD (SJA_CMR_ADR);
//EINT;
}
Uint8 Read_SJA1_Status ( Uint16 Read_ADR )
{
//INTEL MODE
GpioDataRegs.GPDCLEAR.bit.GPIOD0= 1; //CS1=GPIOD0,164245需要OE
GpioDataRegs.GPDSET.bit.GPIOD1 = 1; //WRITE ADRESS ALE1 =1=GPIOD1
SJA_BASE_ADR_PTR= Read_ADR; //先送出地址,ALE=1
GpioDataRegs.GPDCLEAR.bit.GPIOD1 = 1; //WRITE ADRESS ALE1 CLEAR
GpioDataRegs.GPDSET.bit.GPIOD0 =1; //WRITE ADRESS SJA1
GpioDataRegs.GPDCLEAR.bit.GPIOD0= 1; //CS1
SJA1_Status=SJA_BASE_ADR_PTR;//按照地址读出状态信息 //然后再读数据,ALE0,CS=0,RD=0
GpioDataRegs.GPDSET.bit.GPIOD0=1 ;//READ ADRESS SJA1
return SJA1_Status;
}
Uint8 Read_SJA2_Status ( Uint16 Read_ADR )
{
//INTEL MODE
// GpioDataRegs.GPACLEAR.bit.GPIOA11= 1; //CS2
GpioDataRegs.GPACLEAR.bit.GPIOA11= 1; //CS2,164245需要OE
GpioDataRegs.GPASET.bit.GPIOA12 = 1; //WRITE ADRESS ALE2 =1
SJA_BASE_ADR_PTR= Read_ADR; //先送出地址,ALE=1
GpioDataRegs.GPACLEAR.bit.GPIOA12 = 1; //WRITE ADRESS ALE2 CLEAR
//asm(" RPT #3 || NOP");
GpioDataRegs.GPASET.bit.GPIOA11=1 ;//WRITE ADRESS SJA2
GpioDataRegs.GPACLEAR.bit.GPIOA11= 1; //CS2
SJA2_Status=SJA_BASE_ADR_PTR;//按照地址读出状态信息 //然后再读数据,ALE0,CS=0,RD=0
GpioDataRegs.GPASET.bit.GPIOA11=1 ;//WRITE ADRESS SJA2
return SJA2_Status;
}
void Wr_SJA1_CMD ( Uint16 Wr_ADR )
{
//INTEL MODE,SJACS1=GPIOD0;SJA_ALE1=GPIOD1 ;SJACS2=GPIOA11;SJA_ALE2=GPIOA12
//GPIOD0=CS1,GPIOD1 =ALE1
GpioDataRegs.GPDCLEAR.bit.GPIOD0= 1; //CS1=GPIOD0,164245需要OE
GpioDataRegs.GPDSET.bit.GPIOD1 = 1; //WRITE ADRESS ALE1 =1=GPIOD1
SJA_BASE_ADR_PTR= Wr_ADR; //先送出地址,ALE=1
GpioDataRegs.GPDCLEAR.bit.GPIOD1 = 1; //WRITE ADRESS ALE1 CLEAR
GpioDataRegs.GPDSET.bit.GPIOD0 =1; //WRITE ADRESS SJA1
GpioDataRegs.GPDCLEAR.bit.GPIOD0= 1; //CS1
SJA_BASE_ADR_PTR= SJA_Wr_CMD;//按照地址写入状态信息 //然后再读数据,ALE0,CS=0,RD=0
GpioDataRegs.GPDSET.bit.GPIOD0=1 ;//WRITE ADRESS SJA2
}
void Wr_SJA2_CMD ( Uint16 Wr_ADR )
{
//INTEL MODE,SJACS1=GPIOD0;SJA_ALE1=GPIOD1 ;SJACS2=GPIOA11;SJA_ALE2=GPIOA12
GpioDataRegs.GPACLEAR.bit.GPIOA11= 1; //CS2,164245需要OE
GpioDataRegs.GPASET.bit.GPIOA12 = 1; //WRITE ADRESS ALE2 =1
SJA_BASE_ADR_PTR= Wr_ADR; //先送出地址,ALE=1
GpioDataRegs.GPACLEAR.bit.GPIOA12 = 1; //WRITE ADRESS ALE2 CLEAR
GpioDataRegs.GPASET.bit.GPIOA11=1 ;//WRITE ADRESS SJA2
GpioDataRegs.GPACLEAR.bit.GPIOA11= 1; //CS2
SJA_BASE_ADR_PTR= SJA_Wr_CMD;//按照地址写入状态信息 //然后再读数据,ALE0,CS=0,RD=0
GpioDataRegs.GPASET.bit.GPIOA11=1 ;//WRITE ADRESS SJA2
}
interrupt void PeliCan1_RXD_isr(void)
{
// DINT;//关CPU中断
Read_SJA1_Status (SJA_IR_ADR);
//Status = SJA_IR;
if(SJA1_Status & RI_BIT)
{//IR.0 = 1 接收中断
RX1_buffer[0] = Read_SJA1_Status ( SJA_RBSR0_ADR );//SJA_RBSR0;// SJA1_RBSR0_ADR
RX1_buffer[1] = Read_SJA1_Status ( SJA_RBSR1_ADR );
RX1_buffer[2] = Read_SJA1_Status ( SJA_RBSR2_ADR );
RX1_buffer[3] = Read_SJA1_Status ( SJA_RBSR3_ADR );
RX1_buffer[4] = Read_SJA1_Status ( SJA_RBSR4_ADR );
RX1_buffer[5] = Read_SJA1_Status ( SJA_RBSR5_ADR );
RX1_buffer[6] = Read_SJA1_Status ( SJA_RBSR6_ADR );
RX1_buffer[7] = Read_SJA1_Status ( SJA_RBSR7_ADR );
RX1_buffer[8] = Read_SJA1_Status ( SJA_RBSR8_ADR );
RX1_buffer[9] = Read_SJA1_Status ( SJA_RBSR9_ADR );
RX1_buffer[10] = Read_SJA1_Status ( SJA_RBSR10_ADR );
RX1_buffer[11] = Read_SJA1_Status ( SJA_RBSR11_ADR );
RX1_buffer[12] = Read_SJA1_Status ( SJA_RBSR12_ADR );
//SJA_CMR = RRB_BIT;
//Status = SJA_ALC;//释放仲裁随时捕捉寄存器
//Status = SJA_ECC;//释放错误代码捕捉寄存器
}
// SJA_IER = RIE_BIT;// .0=1--接收中断使能;
SJA_Wr_CMD = RRB_BIT;//释放接收缓冲器位
Wr_SJA1_CMD (SJA_CMR_ADR);
Read_SJA1_Status (SJA_ALC_ADR);//释放仲裁随时蹲郊拇嫫?
Read_SJA1_Status (SJA_ECC_ADR);//释放错误代码捕捉寄存器
SJA_Wr_CMD = RIE_BIT;// .0=1--接收中断使能
Wr_SJA1_CMD (SJA_IER_ADR);
PieCtrl.PIEACK.all|=0x001; // Issue PIE ACK
//CAN转发回计算机
for (PeliCan1_Num=0;PeliCan1_Num<13;PeliCan1_Num++)
{
TX1_buffer[PeliCan1_Num]=RX1_buffer[PeliCan1_Num];
}
write_sja1();
// EINT;//打开CPU中断
}
interrupt void PeliCan2_RXD_isr(void)
{//接收数据函数,在中断服务程序中调用
// DINT;//关CPU中断
Read_SJA2_Status (SJA_IR_ADR);
//Status = SJA_IR;
if(SJA2_Status & RI_BIT)
{//IR.0 = 1 接收中断
RX2_buffer[0] = Read_SJA2_Status ( SJA_RBSR0_ADR );//SJA_RBSR0;// SJA1_RBSR0_ADR
RX2_buffer[1] = Read_SJA2_Status ( SJA_RBSR1_ADR );
RX2_buffer[2] = Read_SJA2_Status ( SJA_RBSR2_ADR );
RX2_buffer[3] = Read_SJA2_Status ( SJA_RBSR3_ADR );
RX2_buffer[4] = Read_SJA2_Status ( SJA_RBSR4_ADR );
RX2_buffer[5] = Read_SJA2_Status ( SJA_RBSR5_ADR );
RX2_buffer[6] = Read_SJA2_Status ( SJA_RBSR6_ADR );
RX2_buffer[7] = Read_SJA2_Status ( SJA_RBSR7_ADR );
RX2_buffer[8] = Read_SJA2_Status ( SJA_RBSR8_ADR );
RX2_buffer[9] = Read_SJA2_Status ( SJA_RBSR9_ADR );
RX2_buffer[10] = Read_SJA2_Status ( SJA_RBSR10_ADR );
RX2_buffer[11] = Read_SJA2_Status ( SJA_RBSR11_ADR );
RX2_buffer[12] = Read_SJA2_Status ( SJA_RBSR12_ADR );
//SJA_CMR = RRB_BIT;
//Status = SJA_ALC;//释放仲裁随时捕捉寄存器
//Status = SJA_ECC;//释放错误代码捕捉寄存器
}
// SJA_IER = RIE_BIT;// .0=1--接收中断使能;
SJA_Wr_CMD = RRB_BIT;//释放接收缓冲器位
Wr_SJA2_CMD (SJA_CMR_ADR);
Read_SJA2_Status (SJA_ALC_ADR);//释放仲裁随时捕捉寄存器
Read_SJA2_Status (SJA_ECC_ADR);//释放错误代码捕捉寄存器
SJA_Wr_CMD = RIE_BIT;// .0=1--接收中断使能
Wr_SJA2_CMD (SJA_IER_ADR);
PieCtrl.PIEACK.all|=0x001; // Issue PIE ACK
//CAN转发回计算机
for (PeliCan2_Num=0;PeliCan2_Num<13;PeliCan2_Num++)
{
TX2_buffer[PeliCan2_Num]=RX2_buffer[PeliCan2_Num];
}
write_sja2();
// EINT;//打开CPU中断
}
感觉说的话,一下说不清楚,能加下我Q吗 1342605270 谢谢了 |