CAN1Initialize(CAN_SYNC_JUMP_WIDTH2 &
CAN_BAUD_PRE_SCALE(2),
CAN_WAKEUP_BY_FILTER_DIS &
CAN_PHASE_SEG2_TQ(5) &
CAN_PHASE_SEG1_TQ(4) &
CAN_PROPAGATIONTIME_SEG_TQ(4) &
CAN_SEG2_FREE_PROG &
CAN_SAMPLE1TIME);
unsigned char *rx_data;
CAN1ReceiveMessage (rx_data, 3, 0x4030);
void CAN1SendMessage (unsigned char *data,
unsigned char datalen,
unsigned long txIdentifier,
unsigned char ide,
unsigned char remoteTransmit,
unsigned int *dmaBaseAddress, char MsgFlag);
函数的定义如下:
void CAN1SendMessage(unsigned char *data, unsigned char datalen, unsigned long txIdentifier,\
unsigned char ide, unsigned char remoteTransmit, unsigned int *dmaBaseAddress, unsigned char MsgFlag){
unsigned char i; //给for循环用,用来循环合成要发送的数据帧。
unsigned int word1;
unsigned int word2, word0; //这三个字用来存放数据帧的头三个字
char *ptr; //用来合成一个帧,作为指针指向*data区间
switch(ide)
{
case 0:
// Standard ID
word0 = (unsigned short)(((txIdentifier & 0x7FF) << 2) | (remoteTransmit << 1));
word2 = (unsigned short)((remoteTransmit << 9) | (1 << 4) | datalen);
break;
case 1:
//Extended ID
word0 = (unsigned short)((((txIdentifier>>18) & 0x7FF) << 2) | (1 << 1) | 1);
word1 = (unsigned short)((txIdentifier>>6) & 0xFFF);
word2 = (unsigned short)(((txIdentifier & 0x3F) << 10) | (remoteTransmit << 9) |
( 1 << 8) | (1 << 4) | datalen);
break;
default:
// Standard ID
word0 = (unsigned short)(((txIdentifier & 0x7FF) << 2) | (remoteTransmit << 1));
word2 = (unsigned short)((remoteTransmit << 9) | (1 << 4) | datalen);
break;
}
// Obtain the Address of Transmit Buffer in DMA RAM for a given Transmit Buffer number
dmaBaseAddress[0] = word0;
dmaBaseAddress[1] = word1;
dmaBaseAddress[2] = word2;
ptr=(char *)&dmaBaseAddress[3];
for(i = 0;i < datalen;i++)
{
*ptr++ = data;
}
/* Msg send request */
switch(MsgFlag)
{
case 0:
C1TR01CONbits.TXREQ0 = 1;
break;
case 1:
C1TR01CONbits.TXREQ1 = 1;
break;
case 2:
C1TR23CONbits.TXREQ2 = 1;
break;
case 3:
C1TR23CONbits.TXREQ3 = 1;
break;
case 4:
C1TR45CONbits.TXREQ4 = 1;
break;
case 5:
C1TR45CONbits.TXREQ5 = 1;
break;
case 6:
C1TR67CONbits.TXREQ6 = 1;
break;
case 7:
C1TR67CONbits.TXREQ7 = 1;
break;
default:
C1TR01CONbits.TXREQ0 = 1;
break;
}
}
对照数据帧的格式如下:
文章评论(0条评论)
登录后参与讨论