void main(void) { CAN1_Init();
CAN1_SendFrame(MessageID,FrameType,FrameFormat,Length,Data);
while(!CAN1RFLG_RXF); CAN1_ReadFrame(MessageID,FrameType,FrameFormat,Length,Data); return ERR_OK; }
以上是主程序 在主程序前面应该是定义实参,但我怎么定义,CODEWARRIOR都提示我有错.估计问题出在参数定义上.版主能否给看看怎么定义啊?
下面是接收程序和发送程序. byte CAN1_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte *Length,byte *Data) { byte i; /* Temporary variable */ dword ID; /* Temporary variable */
ID = *(dword *)&CAN1RXIDR0; /* Read the identification of the received message */ if ((CAN1RXIDR1 & 8)) { /* Is the received message "extended frame" */ ID = ( ((ID >> 1) & 0x0003FFFF) | ((ID >> 3) & 0xFFFC0000) ); /* Result the identification */ *FrameType = (CAN1RXIDR3 & 1)? REMOTE_FRAME : DATA_FRAME; /* Result the frame type */ } else { /* Message is "standard frame" */ ID >>= 21; /* Result the identification */ *FrameType = (CAN1RXIDR1 & 16)? REMOTE_FRAME : DATA_FRAME; /* Result the frame type */ } *MessageID = ID; /* Result the identification */ *FrameFormat = (CAN1RXIDR1 & 8)? EXTENDED_FORMAT : STANDARD_FORMAT; /* Result the frame type */ *Length = CAN1RXDLR & 15; /* Result length of the message */ if (*FrameType == DATA_FRAME) { /* Is frame "data frame" */ for(i=0; i<*Length; i++) Data = *((byte *)&CAN1RXDSR0 + i); /* Return received data */ } return ERR_OK; /* OK */ }
byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,byte *Data) { byte i; /* Temorary variables */ byte bufmask=((word)1 << BufferNum); /* Buffer mask */
if ( (BufferNum > 2) || (Length > 8) ) /* Is BufferNum greater than 2 or Length greater than 8?*/ return ERR_VALUE; /* If yes then error */ if (FrameType > REMOTE_FRAME) /* Is FrameType other than REMOTE_FRAME or DATA_FRAME */ return ERR_VALUE; /* If yes then error */ if (!(CAN1TFLG & bufmask)) /* Is the transmit buffer full? */ return ERR_TXFULL; /* If yes then error */ CAN1TBSEL = bufmask; /* Find any empty transmit buffer */ if (MessageID <= EXTENDED_FRAME_ID) /* Is it the standard frame? */ *(dword *)&CAN1TXIDR0 = (MessageID << 21); /* Sets the message as "standard" */ else *(dword *)&CAN1TXIDR0 = ( ((MessageID << 1) & 0x0007FFFF) | ((MessageID << 3) & 0xFFE00000) ) | 0x00080000; /* Set the message as "extended" */ if (FrameType == DATA_FRAME) { /* Is it a data frame? */ for(i=0; i *((byte *)&CAN1TXDSR0 + i) = Data; /* Store data to the transmit register */ if (MessageID <= EXTENDED_FRAME_ID) /* Is it the standard frame? */ CAN1TXIDR1 &= 239; /* If yes then set the standard message type as "data frame" */ else CAN1TXIDR3 &= 254; /* If no then set the extended message type as "data frame" */ } else { /* Remote frame */ if (MessageID <= EXTENDED_FRAME_ID) /* Is it the standard frame? */ CAN1TXIDR1 |= 16; /* If yes then set the standard message type as "remote frame" */ else CAN1TXIDR3 |= 1; /* If yes then set the extended message type as "remote frame" */ } CAN1TXDLR = Length; /* Set the length of the message */ CAN1TXTBPR = 0; /* Set the priority (high) */ CAN1TFLG = bufmask; /* Start transmission */ return ERR_OK; /* OK */ }
[此贴子已经被作者于2007-4-26 8:56:38编辑过] |