📄 tangshants040611.c
字号:
//GLOBAL VARIABLE INITIALIZE
rxbufLen422=0xff;/*the number of rx byte,initialized by a impossible value*/
rxCount422=0;/*counter for rx */
bitFrmCmpl422=0;
}
/*******************************************************************************
* insertNullinTx()--transit the message data when sendding .If the message contains
* STX-character, an additional NUL-character(value=0)has to be inserted to avoid
* misinterpertation of STX.
*/
void insertNullinTx(void)
{
unsigned char data cc;
unsigned char data i,j;
unsigned char txbufLen;/*tx buffer len(byte)*/
/*unsigned char data * data pSdata;
unsigned char data * data pDdata;*/
txbufLen=txbuffer422[1]+1;
for(i=2;i<txbufLen;i++)/*begin with CTRLID,end with Dn,not include CS. */
{
cc=txbuffer422[i];
if(cc==02)
{
i++;
for(j=txbufLen;j>i;j--)
{
txbuffer422[j]=txbuffer422[j-1];
}
txbuffer422[i]=0;
txbuffer422[1]++;/*len increase*/
txbufLen++;
}
}
}
/*****************************************************************************
*delNullFromRx()-- transit the message data when receiving.If the message contains
* STX-character, an additional NUL-character(value=0)is following it.So the additional
* NUL-character must be deleted when receiving.
*/
void delNullFromRx(void)
{
unsigned char data cc;
unsigned char data i,j;
unsigned char data rxCsIndex;
rxCsIndex=rxPrcs422[0];
for(i=1;i<rxCsIndex;i++)/*begin with RADR,end with Dn,not include CS. */
{
cc=rxPrcs422[i];
if(cc==02)
{
for(j=i+1;j<rxCsIndex;j++)
{
rxPrcs422[j]=rxPrcs422[j+1];
}
rxPrcs422[0]--;
rxCsIndex--;
}
}
}
/*****************************************************************************
* txGetChecksum422()--calculate the checksum when sending frame
*/
void txGetChecksum422(void)
{
unsigned char data i,sum=0;
unsigned char data txCsIndex;
//unsigned char data * data pDdata;
txCsIndex=txbuffer422[1]+1;
for(i=1;i<txCsIndex;i++)/*begin with LEN,end with Dn,not include CS. */
{
sum+=txbuffer422[i];
}
sum=0-sum;
txbuffer422[txCsIndex]=sum;/*CS*/
}
/****************************************************************************
* rxCheckSum422()--check the received frame with checksum.If check sum error,
* return ERR,else return OK.
*/
unsigned char rxCheckSum422(void)
{
unsigned char data sum,i;
unsigned char data rxCsIndex;
sum=0;
rxCsIndex=rxPrcs422[0];/*the byte of rxPrcs422 is rxPrcs422[1]+1,no STX */
for(i=0;i<=rxCsIndex;i++)/*begin with LEN,end with CS */
{
sum+=rxPrcs422[i];
}
return sum;
}
/**************************************************************************
* initializeTimer1--initialize timer1 ,supplying Serial1(RS422 PROTOCOL)
* generating baud rates.the serial0 port can use Timer
* 1 or Timer 2 to generate baud rates. This is selected
* ia SFR bits. The serial1 port can only use Timer 1.
*/
void initializeTimer1(void)
{
TR1=0;
TMOD|=0x20; /* timer1 used mode 2 */
TMOD&=0x3f; /* C/T=0;GATE=0*/
TH1=0xfd; //38.4k
TL1=0xfd;
// TR1=1;
}
/*
* sendFram422()
*/
void sendFram422(void)
{
unsigned char data txbufLen;
unsigned char data i;
Watchdog();
insertNullinTx();/*insert the NULL character in tx frame*/
Watchdog();
txGetChecksum422();/*calculate the checksum*/
SCON1=0x50; //0111.0000 mode 1 & SM2=1
txbufLen=txbuffer422[1]+2;
REN1=0;/*Receive disenable*/
txbuffer422[0]=0x02;
for(i=0;i<txbufLen;i++)
{
SBUF1=txbuffer422[i];
while(!TI1) _nop();
TI1=0;
}
Watchdog();
InitializeRXDTXDCom1(); //initialized com1
REN1=1;/*Receive enable*/
}
/*
* sendStartCmmnd()--TCS Send the strart command,the message data store in the
* parameter mssgData.
*/
void sendStartCmmnd(void)
{
unsigned char data i;
unsigned char data sum=0;
txbuffer422[1]=9;/*Len*/
txbuffer422[2]=0x01; /*CTRLID*/
txbuffer422[3]=0x31;
txbuffer422[4]=0x30;
txbuffer422[5]=0x30;
txbuffer422[6]=rxbuf485[4];
txbuffer422[7]=rxbuf485[5];
txbuffer422[8]=rxbuf485[6];
txbuffer422[9]=rxbuf485[7];
Watchdog();
sendFram422();
}
/*
* tiltCmmnd()--TCS Send the tilting command to the tilting electronic,
* the message data store in the parameter mssgData.
*/
void tiltCmmnd(void)
{
txbuffer422[1]=6;/*Len*/
txbuffer422[2]=0x04;/*CTRLID*/
txbuffer422[3]=lifesign;//rxPrcs485[15]/*rxPrcs485[8]*/;
txbuffer422[4]=rxPrcs485[11]&0x02;//GSTEIN;//rxPrcs485[11];
txbuffer422[5]=rxPrcs485[9];
txbuffer422[6]=rxPrcs485[10];
Watchdog();
sendFram422();
}
/*
* ResponseTeErr()--TCS response the TE ERROR
*/
void ResponseTeErr(void)
{
unsigned char data i;
txbuffer422[1]=3;/*Len*/
txbuffer422[2]=0x06 ;/*CTRLID*/
// txbuffer422[3]=rxPrcs485[12];/*返回车号*/
txbuffer422[3]=fhchehao;//test
// for(i=3;i<5;i++)
// {
// txbuffer422[i]=rxPrcs485[i+9];
// }
Watchdog();
sendFram422();
}
/*
* Sendstopcm
*/
void Sendstopcm(void)
{
txbuffer422[1]=3;/*Len*/
txbuffer422[2]=0x07 ;/*CTRLID*/
txbuffer422[3]=0x00;
Watchdog();
sendFram422();
}
void ResponseCPU(void)
{
txbuf485[14]|=0x02;/*TE 422通信失败*/
}
/*
* rx interrupt .Only the LEN upset LSB and MSB.
*/
_interrupt (7) void RxTxIntr422(void)
{
unsigned char rxchar=0;
if(RI1==1)
{
RI1=0;
rxchar=SBUF1;
if((rxchar==0x02)&&(rxbufLen422==0xff))
{ /*receive STX */
goto inter01;
}
if((rxbufLen422==0xff)&&(rxchar!=0x02))
{ /*Receive the LEN*/
rxCount422=0;
rxbufLen422=rxchar;
}
rxPrcs422[rxCount422]=rxchar;
if(rxCount422>=rxbufLen422)
{
Watchdog();
InitializeRXDTXDCom1(); //initialized com1
// rxbufLen422=0xff;
// rxCount422=0;
bitFrmCmpl422=1;/*receive a whole frame completely*/
}
else
{rxCount422++;
}
}
inter01: _nop;
}
/*
* rxFrameAnalyse-- after receiving a whole frame ,judge the frame
* is technical status from tilting electronic or error command.
*/
void rxFrameAnalyse(void)
{
if(rxCheckSum422()==0)/*check sum*/
{
Watchdog();
delNullFromRx();/*delete the NULL charater*/
}
}
/**************************************************************************************
* CAN PROGRAM *
* *
**************************************************************************************/
/******************************************************************
* enterWork()--enter work mode from reset mode
* return 0---OK
* 1---ERR
*/
uchar enterWork(void)
{
CAN[MODE]&=0xfe;
if(CAN[MODE]&0x1)
return 1;
else
return 0;
}
/*****************************************************************
* enterRst()--enter reset mode from work mode
* return 0---OK
* 1---ERR
*/
uchar enterRst(void)
{
while((CAN[STATUS]&0x30));
CAN[MODE]|=0x1;
if(CAN[MODE]&0x1)
return 0;
else
return 1;
}
/******************************************************************
*initCan()
*/
void initCan(void)
{
uchar data i=0;
bitFrmCmplCAN=0;
for(i=TXB1;i<=TXB8;i++)
{
CAN[i]=0xa1;
if(CAN[i]!=0xa1)
{
bitCANOK=0;
LED2A=ON;Delay(10000);
}
else
{
bitCANOK=1;
}
}
CAN[MODE] &=0x09;/*single acceptance filter*/
CAN[CLKDIVID] =0x88;/*PeliCAN mode,*/
CAN[ACCEPT0]=0x07;
CAN[ACCEPT1]=0x00;
CAN[ACCEPT2]=0x00;
CAN[ACCEPT3]=0x00;
CAN[ACCPTMST0] =0xe0;
CAN[ACCPTMST1] =0xFF;
CAN[ACCPTMST2] =0xF8;
CAN[ACCPTMST3] =0x0;
CAN[BUSTIME0] =0x01;
CAN[BUSTIME1] =0x1c;
CAN[OUTCNTRL] =0xaa;
CAN[RBSA] =0;
CAN[RXERR] =0;
CAN[TXERR] =0;
CAN[INTRTENBL] =canBEIE|canDOIE|canRIE;
while( enterWork())
{
if(i<50) i++;
else
{
bitCANOK=0;
break;
}
}
}
/*******************************************************************
*CanSend()--CAN send frame data that store in CanTxbuf[]
*/
void CanSend(void)
{
uchar data i,j;
while(CAN[STATUS]&canTS)/*Sending now*/
{
_nop();
}
while((CAN[STATUS]&canTBS)==0)
{
_nop();/*TX buffer locked*/
}
for(i=0,j=FRMINFRM;i<13;i++,j++)
{
CAN[j]=CanRxbuf[i];
}
CAN[COMMAND]|=1;/*SEND REQUEST*/
}
/********************************************************************
* CanRxProcess()--
*/
void CanRxProcess(void)
{
uchar data pduFormat,i;/*ID23-16*/
uchar data pduSpecific;/*ID15-8*/
for(i=0;i<RXBUFCANMAX;i++)
{
CanRxprcs[i]=CanRxbuf[i];
}
pduFormat= (CanRxprcs[1]<<5)|(CanRxprcs[2]>>3);
pduSpecific= (CanRxprcs[2]<<5)|(CanRxprcs[3]>>3);
if(pduFormat==240)
if(pduSpecific==4)/*ENGINE SPEED pgn61444*/
{
txbuf485[15]=CanRxprcs[8];
txbuf485[16]=CanRxprcs[9];
}
if(pduFormat==254)
{
switch(pduSpecific)
{
case 219:/*燃油总管压力 pgn65243*/
txbuf485[18]=CanRxprcs[7];
txbuf485[19]=CanRxprcs[8];
break;
case 239:/*机油压力 pgn 65263*/
txbuf485[17]=CanRxprcs[8];
break;
case 246:/*增压器压力and 进气总管温度 pgn 65270*/
txbuf485[20]=CanRxprcs[6];
txbuf485[23]=CanRxprcs[7];
break;
case 245:/*大气压力 pgn 65269*/
txbuf485[21]=CanRxprcs[5];
break;
case 238:/*冷却水温度pgn 65262*/
txbuf485[22]=CanRxprcs[5];
break;
}
}
}
/********************************************************************
* RxIntr()--CAN RX INTERRUPT ROUTINE,receive the whole frame data
*/
_interrupt (0) void CanRxIntr(void) //外部中断0,
{
uchar data intrVal,i,j;
intrVal= CAN[INTERRUPT];
if(intrVal&canRI)/*receive interrupt*/
{
if(CAN[STATUS]&canRBS)/*RXFIFO stores valuable information*/
{
for(i=0,j=FRMINFRM;i<13;i++,j++)
{
CanRxbuf[i]=CAN[j];
}
bitFrmCmplCAN=1;
CAN[COMMAND]|=4;/*release RXFIFO*/
}
}
if(intrVal&canDOI)
{
CAN[COMMAND]|=0x08;/*clear data overrun bit*/
}
if(intrVal&canBEI)
{
initCan();
}
}
//the end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -