如图是AT89S52驱动CAN的接口定义,程序和电路是没有问题的,根据需要换成了STC89C58RD+,封装都是LQFP44,但是芯片换了之后通讯老是调不通,由于用到了单片机的ALE和RD/RW资源不知道这两种芯片是否一样,下面是CAN 芯片的驱动程序
void init_sja1000d3(void)//can??????
{
uchar can_temp;
restcan=0; //can????
delay();
restcan=1;
SEL_CANd3=0x0;//????,???SJA1000?????
XBYTE[0x09]=0xaa;//??CAN??
can_temp=XBYTE[0x09];
if(can_temp!=0xaa)
{
SEL_CANd3=0x1;//????
return;
}
SEL_CANd3=0x0; //XBYTE[0xB0FF]=0x00;
XBYTE[MODE]=0x09; //??????
can_temp=XBYTE[MODE]&0x01;//???????
if(can_temp==0x00)
{
SEL_CANd3=0x1;//XBYTE[0xB0FF]=0x0FF;//????
return;
}
XBYTE[CDR] =0xC8 ;// c8??????? CDR CAN???????,RX1????????,??????B
// XBYTE[IER] =0x09 ;//????,????????????????? IER0x09
XBYTE[BTR0] =0x03 ;//????? ?????2,????16M,????16,????001c,500K 011c,250K BTR0 031c 125k
XBYTE[BTR1] =0x1c ;//???????1 BTR1
XBYTE[OCR] =0x1a ;// aa????????? OCR ??????
XBYTE[RXERR] =0x00 ;//RXERR ?0 ???????
XBYTE[TXERR] =0x00 ;//???????
XBYTE[ACR] =0x05 ;//?????,???ACR 0~3 BIT7=1 CAN???????;BIT3=1???8?
XBYTE[ACR+1] =0x00 ;
XBYTE[ACR+2] =0x80 ;
XBYTE[ACR+3] =0x00 ;
XBYTE[AMR] =0xfa ;//?????,bitx=1????????? AMR0~3
XBYTE[AMR+1] =0xff ;
XBYTE[AMR+2] =0xff ;
XBYTE[AMR+3] =0xff ;
XBYTE[0x1e] =0x00 ;//???????????
XBYTE[MODE]=0x08; //??????
// XBYTE[0x10]=0x88; //?????
XBYTE[IER] =0x09 ;//????,????????????????? IER0x09
SEL_CANd3=0x1;//XBYTE[0xB0FF]=0xFF;//????
SEL_CANd3=0x1;//XBYTE[0xB0FF]=0xFF;
return;
}
//******************************************
//**************************************************************************
void send_cand3(uchar ch)
{
uchar can_flag,can_dog;
dog_flag=0;
TH1=(65536-917)/256;
TL1=(65536-917)%256;
TR1=1;
SEL_CANd3=0x0;//XBYTE[0xB0FF]=0x00; //CAN????
can_flag=XBYTE[SR]&0x08; //??????,
while(can_flag==0x00) //????????,??
{
SEL_CANd3=0x1; //XBYTE[0xB0FF]=0xFF;//????
can_dog=dog_flag;//??????
if((can_dog&0x01)==0x01)
{
can_error=0x00;
SEL_CANd3=0x1;
return;
}
SEL_CANd3=0x0;// XBYTE[0xB0FF]=0x00;//CHANNEL_SJA1000=0;
XBYTE[CMR]=0x02;//????
can_flag=XBYTE[SR];
can_flag=can_flag&0x08; //??????,
}
SEL_CANd3=0x0;
can_flag=XBYTE[SR];
can_flag=can_flag&0x04; //??????
while(can_flag==0x00) //???????,??
{
SEL_CANd3=0x1; //XBYTE[0xB0FF]=0xFF;//????
can_dog=dog_flag;//??????
if((can_dog&0x01)==0x01)
{
can_error=0x00;
SEL_CANd3=0x1;
return;
}
SEL_CANd3=0x0;// XBYTE[0xB0FF]=0x00;// CHANNEL_SJA1000=0;
can_flag=XBYTE[SR];
can_flag=can_flag&0x04; //??????
}
SEL_CANd3=0x0;
switch(ch)
{
case 0x00: // ??100ms???????????
XBYTE[0x10]=can_send[0]; //?????
XBYTE[0x11]=can_send[1];//??????1 ???????????
XBYTE[0x12]=can_send[2];//??????2
XBYTE[0x13]=can_send[3];//??????3
XBYTE[0x14]=can_send[4];//??????4
XBYTE[0x15]=can_send[5];//???1??
XBYTE[0x16]=can_send[6];//???2??
XBYTE[0x17]=can_send[7];//???3??
XBYTE[0x18]=can_send[8];//???4??
XBYTE[0x19]=can_send[9];//???5??
XBYTE[0x1a]=can_send[10];//???6??
break;
case 0x01: //??????
XBYTE[0x10]=0x85; //?????
XBYTE[0x11]=0x18;//??????1 ???????????
XBYTE[0x12]=0xf0;//??????2
XBYTE[0x13]=0x78;//??????3
XBYTE[0x14]=0x76;//??????4
XBYTE[0x15]=zj_result;//0x11;//zj_result??????0x11,0x12????
break;
default: break;
}
XBYTE[CMR]=0x01;//????
SEL_CANd3=0x1;//XBYTE[0xB0FF]=0xFF;//????
can_error=0xff;
can_counter=0;
TR1=0;
TH1=(65536-917)/256;
TL1=(65536-917)%256;
can_flag=0x00;//?can???????????
return;
}转载
|