#ifndef BASICCANCONF_H #define BASICCANCONF_H #include <AT89X51.H> #include <SJA CANCONF.H> #include <SJA CANFUNC.C> #include <display_s.h> void Init Cpu(void); void Init TO(void); void Sja_1000_Init(void); void disp_ok(void); void disp_err(void); bit flag_sec; unsigned char data a[5]; unsigned char data send_data[10],rcv_data[10l; unsigned char databuf,sec_data,second; unsigned char data Tmod_data; unsigned char bdata flag_init; sbit rcv_flag=flag_init0; sbit err_flag=flag_init'2; void main(void) { Sja_1000_Init(); Init_T0(); Init Cpu(); flag_init=0x00; while(1) { if (rcv_f lag) t rcv_flag=0; CAN_DATA_RECEIVE(rcv_data); CAN_CMD_PRG(Ox04); disp_ok(); } if(flag_sec) { flag_sec=0; send_data[0l~Oxaa; send_data[l〕=0x08; send_dataE2]=second; CAN_DATA_WRITE(send_data); CAN_CMDLPRG(0x01); } if (err_f lag) { err_flag=0; disp_err(); Sja_1000_Init(); } display(a); } } void ex0_int(void) interrupt 0 using 1 { SJ A_CANAdr= REG_INTERRUPT; flag_init= SJA_CANAdr; } void T0_int(void) interrupt 1 using 2 { TR0=0; sec_data--; if (sec_data==0) } sec_data=0x10; TH0=0x80; TL0=0x60; flag_sec= 1; second_f++; } TR0=1; } void Init Cpu(void) { PX0=1; IT0=1; EX0=1; ET0=1; EA=1; sec_data=0x10; } void Init TO(void) { Tmod data=TMOD; Tmod_data&=0xf0; Tmod_data!=0x01; TMOD=Tmod-data; TH0=0x80; TL0=0x60; TR0=1; sec_data=0x20; second=0x00; } void Sja_1000_Init(void) { CAN_ENTER_RETMODEL(); CAN_SET_BANDRATE(0x04); CAN_SET_OBJECT(0xaa,0xff); CAN_SET_OUTCLK(0xAA,0x48); CAN_QUIT_RETMODEL(); SJ A_CANAdr= REG_CONTROL; SJA_CANAdr|=0x1e; } void disp_ok(void) { a[0]=0x00; a[1]=0x11; a[2]=0x10; databuf= rcv_data[2]; databuf>>=0x04; a[3]=databuf; databuf= rcv_data[2]; databuf=databuf &Oxof; a[4]=databuf; } void disp_err(void) { a[0]=0x00; a[1]=0x0e; a[2]=0x10; a[3]=0x0f; a[4]=0x0c; } #include "SJA_CANCONF. H" #include <string. H> bit CAN_CREATE_COMMUNATION(void) { SJA_CANAdr=REG_TEST; SJA_CANAdr=0xAA; if(SJA_CANXdi= 0AA) { return 0; } else{ return I; } } bit CAN_ENTER_RETMODEL(void) { unsigned char TempData; SJA_CANAdr= REG_CONTROL; TempData=*SJA_CANAdr; SJA_CANAdr= (TempData!0x01); if((*SJA_CANAdr&0x01)==1) { return 0; } else { return 1; } } } } bit CAN_SET_BANDRATE(unsigned char CAN_ByteRate) { unsigned char BR_Num=CAN_ByteRate,BTR0_num,BTRl num; switch(BR_Num) { case ByteRate_5k: BTR0_num=0xEF; BTRl num=0xFF; break; case ByteRate_10k: BTR0_num=0xD7; BTR1_num=0xFF; break; case ByteRate_20k: BTR0_num=0xCB; BTR1_num=0xFF; break; case ByteRate_40k: BTR0_num=0xC5; BTR1_num=0xFF; break; case ByteRate_50k: BTR0_num=0xC9; BTRl num=0xA7; break; case ByteRate_80k: BTR0_num=0xC2; BTR1_num=0xFF; break; case ByteRate_100k: BTR0_num=0x84; BTR1_num=0xA7; break; case ByteRate_200k; BTR0_num=0x82; BTRl_num=0x25; break; case ByteRate_250k: BTR0 num=1; BTRl num=0x45; break; case ByteRate_300k: BTR0_num=0x01; BTRl_num=0x25; break; case ByteRate_500k: BTR0_num=0x01; BTRl_num=0x12; break; case ByteRate_600k: BTR0_num=00; BTRl num=0x25; break; case ByteRate_1000k: BTR0_num=0x0; BTRl num=0x12; break; default: return 1; break; SJA_CANAdr=REG_BTR0; *SJA_CANAdr= BTR0_num; if(*SJA_CANAdr!=BTRO_num) { return 1; } SJ A_CANAdr= REG_BTR1; *SJA_CANAdr=BTRl num; if(*SJA_CANAdr!=BTRl num) { return 1; } return 0; } return 0; } bit CAN_SET_OUTCLK(unsigned char Out Control,unsigned char Clock_Out) { SJA_CANAdr=REG_OCR; *SJA_CANAdr=Out_Control; //写入参数 if(*SJA_CANAdr!=Out_Control) { return 1; } SJA_CANAdr=REG_CDR; *SJA_CANAdr=Clock_Out; //写入参数 Return 0; } bit CAN_DATA_WRITE(unsigned char * SendDataBuf) ( unsigned char TempCount,status; SJA_CANAdr = REG_STATUS; if((*SJA_CANAdr&0x08)==0) { //判断上次发送是否完成 return(1); } if((*SJA-CANAdr&0x04)==0) { //判断发送缓冲区是否锁定 return(1); } SJA_CANAdr=REG_TxBufferl; if((SendDataBuf[1]&0x10)==0) { TempCount=(SendDataBuf[1] &0x0f)+2; } else { TempCount =2; } memcpy(SJA_CANAdr,SendDataBuf,TempCount); return 0; bit CAN_DATA_RECEIVE(unsigned char,RcvDataBuf) { unsigned char TempCount; SJA_CANAdr=REG_STATUS; if((*SJA_CANAdr&0x01)==0) { //判断报文是否有效 return 1; } SJA_CANAdr= REG_RxBuffer2; if((*SJA_CANAdr&0x10)==0) { //如果是数据帧 TempCount=(*SJA_CANAdr&0x0f)+2; } else { TempCount=2; } SJA_CANAdr = REG_RxBufferl; memcpy(RcvDataBuf,SJA_CANAdr,TempCount); return 0; } bit CAN_CMD_PRG(unsigned char cmd) { SJA_CANAdr=REG_COMMAND; *SJ A_CANAdr= cmd; switch(cmd) { case TR_CMD: return 0; break; case AT_CMD: SJA_CANAdr=REG_STATUS; if((*SJA_CANAdr & 0x20)=~0) { return 0; } else { return 1; } break; case RRR_CMD: SJA_CANAdr=REG_STATUS; if((*SJA_CANAdr&0x01)==1) { //判断是否释放成功 return 1; } else { return 0; } break; case COS_CMD: SJA_CANAdr= REG_STATUS; if((*SJA_CANAdr & 0x02)==0) {return 0;} else {return 1;} break; case GTS CMD: return 0; break; default: return 1; break; } }