基于dsPIC33FJ128MC506的CAN通信(07-100)
寫報文內容
本文引用地址:http://m.butianyuan.cn/article/92042.htm/*下面的函數(shù)將待發(fā)報文內容寫入到發(fā)射緩沖區(qū),各參數(shù)含義如下:
buf -> 發(fā)射緩沖器號
dataLength -> 數(shù)據(jù)的長度
data1/data2/data3/data4 -> 發(fā)射數(shù)據(jù)內容*/
void ecan1WriteTxMsgBufData(unsigned int buf, unsigned int dataLength, unsigned int data1, unsigned int data2, unsigned int data3, unsigned int data4)
{
ecan1msgBuf[buf][2] = ((ecan1msgBuf[buf][2] & 0xFFF0) + dataLength) ;//數(shù)據(jù)長度
ecan1msgBuf[buf][3] = data1; //數(shù)據(jù)1
ecan1msgBuf[buf][4] = data2; //數(shù)據(jù)2
ecan1msgBuf[buf][5] = data3; //數(shù)據(jù)3
ecan1msgBuf[buf][6] = data4; //數(shù)據(jù)4
}
報文的接收
報文接收在中斷中進行,函數(shù)語句如下:
void __attribute__((interrupt, no_auto_psv))_C1Interrupt(void) //CAN1中斷
{
IFS2bits.C1IF = 0; //請中斷標志
if(C1INTFbits.TBIF) //是否為發(fā)射中斷
{
C1INTFbits.TBIF = 0;
}
if(C1INTFbits.RBIF) //是否為接收中斷
{
C1INTFbits.RBIF = 0; //請接收中斷標志位
/*讀出報文*/
ReceiveMessageID = (ecan1msgBuf[2][0]>>2);
ReveiveDataLength = (ecan1msgBuf[2][2]&0x000f);
ReceiveData[0] = ecan1msgBuf[2][3];
ReceiveData[1] = ecan1msgBuf[2][4];
ReceiveData[2] = ecan1msgBuf[2][5];
ReceiveData[3] = ecan1msgBuf[2][6];
C1RXFUL1bits.RXFUL2 = 0; //清報文溢出標志
ReceiveNewMessage = 1; //接收標志置位
}
C1RXOVF1bits.RXOVF2 = 0;
}
評論