基于dsPIC33FJ128MC506的CAN通信(07-100)
写报文内容
本文引用地址:http://www.amcfsurvey.com/article/92042.htm/*下面的函数将待发报文内容写入到发射缓冲区,各参数含义如下:
buf -> 发射缓冲器号
dataLength -> 数据的长度
data1/data2/data3/data4 -> 发射数据内容*/
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) ;//数据长度
ecan1msgBuf[buf][3] = data1; //数据1
ecan1msgBuf[buf][4] = data2; //数据2
ecan1msgBuf[buf][5] = data3; //数据3
ecan1msgBuf[buf][6] = data4; //数据4
}
报文的接收
报文接收在中断中进行,函数语句如下:
void __attribute__((interrupt, no_auto_psv))_C1Interrupt(void) //CAN1中断
{
IFS2bits.C1IF = 0; //请中断标志
if(C1INTFbits.TBIF) //是否为发射中断
{
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;
}
评论