⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can1.c

📁 mc68HC12C64的CAN部件例子程序,不错的.
💻 C
📖 第 1 页 / 共 3 页
字号:
**         NAME            - DESCRIPTION
**       * Err             - Pointer to the returned set of errors
**     Returns     :
**         ---             - Error code (if GetError did not succeed),
**                           possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte CAN1_GetError(CAN1_TError *Err)
{
  byte Status = CANRFLG;               /* Temporary variable */

  if((Status & CAN_STATUS_BOFF_MASK) == CAN_STATUS_BOFF_MASK) { /* Bus-Off state */
    ErrFlag |= CAN_STATUS_BOFF_EXT_MASK;
  }
  ErrFlag |= (Status & 191);
  Err->err = 0;                        /* Clear all errors */
  Err->errName.BusOff = ((ErrFlag & CAN_STATUS_BOFF_EXT_MASK) == CAN_STATUS_BOFF_EXT_MASK); /* Bus-Off state */
  if(!Err->errName.BusOff) {
    Err->errName.TxWarning = ((ErrFlag & CAN_STATUS_TX_WARN_MASK) == CAN_STATUS_TX_WARN_MASK); /* Transmitter warning */
    Err->errName.RxWarning = ((ErrFlag & CAN_STATUS_RX_WARN_MASK) == CAN_STATUS_RX_WARN_MASK); /* Receiver warning */
    Err->errName.RxPassive = ((ErrFlag & CAN_STATUS_RX_PASS_MASK) == CAN_STATUS_RX_PASS_MASK); /* Receiver Error passive state */
    Err->errName.TxPassive = ((ErrFlag & CAN_STATUS_TX_PASS_MASK) == CAN_STATUS_TX_PASS_MASK); /* Transmitter Error passive state */
  }
  Err->errName.OverRun = ((ErrFlag & CAN_STATUS_OVERRUN_MASK) == CAN_STATUS_OVERRUN_MASK); /* Overrun error flag */
  ErrFlag = 0;                         /* Clear error flags */
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  CAN1_SendFrame (bean FreescaleCAN)
**
**     Description :
**         Sends the frame via the CAN device. Using this method the
**         user can send own message to the CAN bus. This method
**         allows to specify CAN buffer number, message ID, data to
**         be sent and frame type (DATA_FRAME/REMOTE_FRAME).
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferNum       - Number of the buffer.
**         MessageID       - Identification of the
**                           message - ID. Message ID can be
**                           specified in the STANDARD format
**                           (default) or the EXTENDED format. The
**                           most significant bit in the ID is set to
**                           specify EXTENDED format. Predefined
**                           macro CAN_EXTENDED_FRAME_ID can be used
**                           (ID "bitwise or" CAN_EXTENDED_FRAME_ID)
**                           to mark ID as extended. If the most
**                           significant bit of ID is clear, STANDARD
**                           format is used.
**         FrameType       - Type of frame
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**         Length          - The length of the frame in bytes
**                           (0..8)
**       * Data            - Pointer to data
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
**                           ERR_VALUE - Some parameter is out of
**                           possible range
**                           ERR_TXFULL - Transmition buffer is full.
** ===================================================================
*/
byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,byte *Data)
{
  byte i;                              /* Temorary variables */
  byte bufmask=((word)1 << BufferNum); /* Buffer mask */
  TMsgBuff *MsgBuff;
  dword tmpId;

  if (((MessageID & CAN_EXTENDED_FRAME_ID) == 0) && (MessageID > CAN_STANDARD_FRAME_MAX_ID)) { /* Is the standard ID greater that 2047? */
    return ERR_VALUE;                  /* If yes then error */
  }
  if ((BufferNum > (CAN_TX_MBUFFERS - 1)) || (Length > CAN_MAX_DATA_LEN)) { /* Is BufferNum greater than CAN_MAXBUFF or Length greater than CAN_MAX_DATA_LEN? */
    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 (!(CANTFLG & bufmask)) {          /* Is the transmit buffer full? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  CANTBSEL = bufmask;                  /* Select requested transmit buffer */
  MsgBuff = (TMsgBuff *)&CANTXIDR0;
  EnterCritical();                     /* Disable global interrupts */
  if (MessageID & CAN_EXTENDED_FRAME_ID) {
    tmpId = (((MessageID & 0x1FFC0000UL) << 3) | 0x00180000UL | ((MessageID & 0x0003FFFFUL) << 1)); /* Extended frame */
  }
  else {
    tmpId = MessageID << 21;           /* Standard frame */
  }
  MsgBuff->IDR3 = ((DwordSwap *)&tmpId)->b.b3; /* Store the message ID */
  MsgBuff->IDR2 = ((DwordSwap *)&tmpId)->b.b2;
  MsgBuff->IDR1 = ((DwordSwap *)&tmpId)->b.b1;
  MsgBuff->IDR0 = ((DwordSwap *)&tmpId)->b.b0;
  if (FrameType == DATA_FRAME) {       /* Is it a data frame? */
    for (i=0; i<Length; i++) {
      MsgBuff->Data[i] = Data[i];      /* Store data to the transmit register */
    }
    if (MessageID & CAN_EXTENDED_FRAME_ID) { /* Is it the extended frame? */
      MsgBuff->IDR3 &= 254;            /* If no then set message type as "data frame" */
    }
    else {
      MsgBuff->IDR1 &= 239;            /* If yes then set message type as "data frame" */
    }
  }
  else {                               /* Remote frame */
    if (MessageID & CAN_EXTENDED_FRAME_ID) { /* Is it the extended frame? */
      MsgBuff->IDR3 |= 1;              /* If yes then set message type as "remote frame" */
    }
    else {
      MsgBuff->IDR1 |= 16;             /* If yes then set message type as "remote frame" */
    }
  }
  MsgBuff->DLR = Length;               /* Set the length of the message */
  MsgBuff->TBPR = 0;                   /* Set the priority (high) */
  CANTFLG = bufmask;                   /* Start transmission */
  ExitCritical();                      /* Enable global interrupts */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_SendFrameExt (bean FreescaleCAN)
**
**     Description :
**         Sends a frame. This method automatically selects a free
**         transmit buffer for data transmission. The user cannot
**         specify a transmit buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**         MessageID       - Identification of the
**                           message - ID. Message ID can be
**                           specified in the STANDARD format
**                           (default) or the EXTENDED format. The
**                           most significant bit in the ID is set to
**                           specify EXTENDED format. Predefined
**                           macro CAN_EXTENDED_FRAME_ID can be used
**                           (ID "bitwise or" CAN_EXTENDED_FRAME_ID)
**                           to mark ID as extended. If the most
**                           significant bit of ID is clear, STANDARD
**                           format is used.
**         FrameType       - Type of frame
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**         Length          - The length of the frame in bytes
**                           (0..8)
**       * Data            - Pointer to data
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
**                           ERR_VALUE - Some parameter is out of
**                           possible range
** ===================================================================
*/
byte CAN1_SendFrameExt(dword MessageID,byte FrameType,byte Length,byte *Data)
{
  byte i;                              /* Temorary variables */
  TMsgBuff *MsgBuff;
  dword tmpId;

  if (((MessageID & CAN_EXTENDED_FRAME_ID) == 0) && (MessageID > CAN_STANDARD_FRAME_MAX_ID)) { /* Is the standard ID greater that 2047? */
    return ERR_VALUE;                  /* If yes then error */
  }
  if (Length > CAN_MAX_DATA_LEN) {     /* Is the message 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 (!(CANTFLG & 7)) {                /* Are all transmit buffers full? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  CANTBSEL = CANTFLG;                  /* Find any empty transmit buffer */
  MsgBuff = (TMsgBuff *)&CANTXIDR0;
  EnterCritical();                     /* Disable global interrupts */
  if (MessageID & CAN_EXTENDED_FRAME_ID) {
    tmpId = (((MessageID & 0x1FFC0000UL) << 3) | 0x00180000UL | ((MessageID & 0x0003FFFFUL) << 1)); /* Extended frame */
  }
  else {
    tmpId = MessageID << 21;           /* Standard frame */
  }
  MsgBuff->IDR3 = ((DwordSwap *)&tmpId)->b.b3; /* Store the message ID */
  MsgBuff->IDR2 = ((DwordSwap *)&tmpId)->b.b2;
  MsgBuff->IDR1 = ((DwordSwap *)&tmpId)->b.b1;
  MsgBuff->IDR0 = ((DwordSwap *)&tmpId)->b.b0;
  if (FrameType == DATA_FRAME) {       /* Is it a data frame? */
    for (i=0; i<Length; i++) {
      MsgBuff->Data[i] = Data[i];      /* Store data to the transmit register */
    }
    if (MessageID & CAN_EXTENDED_FRAME_ID) { /* Is it the extended frame? */
      MsgBuff->IDR3 &= 254;            /* If no then set message type as "data frame" */
    }
    else {
      MsgBuff->IDR1 &= 239;            /* If yes then set message type as "data frame" */
    }
  }
  else {                               /* Remote frame */
    if (MessageID & CAN_EXTENDED_FRAME_ID) { /* Is it the extended frame? */
      MsgBuff->IDR3 |= 1;              /* If yes then set message type as "remote frame" */
    }
    else {
      MsgBuff->IDR1 |= 16;             /* If yes then set message type as "remote frame" */
    }
  }
  CANTXDLR = Length;                   /* Set the length of the message */
  CANTXTBPR = 0;                       /* Set the priority (high) */
  CANTFLG = CANTBSEL;                  /* Start transmission */
  ExitCritical();                      /* Enable global interrupts */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_ReadFrame (bean FreescaleCAN)
**
**     Description :
**         Reads a frame from the CAN device. The user is informed
**         about CAN reception through OnFullRxBuffer event or
**         GetStateRX method.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * MessageID       - Pointer to a message
**                           indentification
**       * FrameType       - Pointer to a frame type
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**       * FrameFormat     - Pointer to a frame
**                           format
**                           STANDARD_FORMAT - standard frame 11-bits
**                           EXTENDED_FORMAT - extended frame 29-bits.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -