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

📄 can1.c

📁 mc68HC12C64的CAN部件例子程序,不错的.
💻 C
📖 第 1 页 / 共 3 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : CAN1.C
**     Project   : NODE_A
**     Processor : MC9S12C64CFA16
**     Beantype  : FreescaleCAN
**     Version   : Bean 02.334, Driver 01.21, CPU db: 2.87.339
**     Compiler  : Metrowerks HC12 C Compiler
**     Date/Time : 2006-11-11, 14:37
**     Abstract  :
**         This bean "FreescaleCAN" implements a CAN serial channel.
**     Settings  :
**         CAN channel                 : MSCAN
**
**         Protocol
**             Interrupt priority      : 1
**             Time segment 1          : 7
**             Time segment 2          : 3
**             RSJ                     : 1
**
**             Recieve accept. code    : 0
**             Recieve accept. mask    : 4294967295
**
**         Input interrupt
**             Vector name             : Vcanrx
**             Priority                : 1
**
**         Output interrupt
**             Vector name             : Vcantx
**             Priority                : 1
**
**     Contents  :
**         SetAcceptanceCode - byte CAN1_SetAcceptanceCode(dword AccCode1,dword AccCode2);
**         SetAcceptanceMask - byte CAN1_SetAcceptanceMask(dword AccMask1,dword AccMask2);
**         SetAcceptanceMode - byte CAN1_SetAcceptanceMode(byte Mode);
**         SendFrame         - byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte...
**         ReadFrame         - byte CAN1_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte...
**         GetStateTX        - byte CAN1_GetStateTX(void);
**         GetStateRX        - bool CAN1_GetStateRX(void);
**         GetError          - byte CAN1_GetError(CAN1_TError *Err);
**         SendFrameExt      - byte CAN1_SendFrameExt(dword MessageID,byte FrameType,byte Length,byte *Data);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2005
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE CAN1. */

#pragma MESSAGE DISABLE C1825
#pragma MESSAGE DISABLE C4301 /* INFORMATION C4301: Inline expansion done for function call */
#pragma OPTION ADD "-Onf"

#include "CAN1.h"
#include "TI1.h"
#include "AS1.h"
#include "PWM6.h"
#include "Bits1.h"
#include "TO1.h"
#include "Events.h"

#define CAN_STANDARD_FRAME_MAX_ID    2047 /* Max ID of the standard frame */
#define CAN_MAX_DATA_LEN             8 /* Max number of data to be sent in one frame */
#define FULL_RX_BUF                  1 /* RX buffer full           */

#define MB_ID_IDE                    0x00080000UL
#define CAN_TX_MBUFFERS              3 /* Number of TX buffers */
#define CAN_MAX_RX_FIFO              4 /* Max length of the RX fifo */

#define CAN_STATUS_OVERRUN_MASK      2 /* Overrun error flag mask */
#define CAN_STATUS_TX_MASK           12 /* Transmitter error state mask*/
#define CAN_STATUS_RX_MASK           48 /* Receiver error state mask*/
#define CAN_STATUS_BOFF_MASK         12 /* Bus-Off state mask in register */
#define CAN_STATUS_BOFF_EXT_MASK     64 /* Bus-Off state mask in error flag */
#define CAN_STATUS_TX_PASS_MASK      8 /* Transmitter error passive state mask */
#define CAN_STATUS_RX_PASS_MASK      32 /* Receiver error passive state mask */
#define CAN_STATUS_TX_WARN_MASK      4 /* Transmitter warning mask */
#define CAN_STATUS_RX_WARN_MASK      16 /* Receiver warning mask */
#define CAN_STATUS_WAKEUP_MASK       128 /* Wakeup interrupt flag mask */

typedef struct {                       /* Message buffer structure */
          byte IDR0;
          byte IDR1;
          byte IDR2;
          byte IDR3;
          byte Data[CAN_MAX_DATA_LEN];
          byte DLR;
          byte TBPR;
          byte Reserved;
          byte Reserved2;
        }TMsgBuff;                     /* Message buffer structure */

typedef union {
          dword dw;
          struct {
            byte b0;
            byte b1;
            byte b2;
            byte b3;
          }b;
        }DwordSwap;

#pragma DATA_SEG CAN1_DATA
#pragma CODE_SEG CAN1_CODE

static volatile byte ErrFlag;          /* Error flags mirror of the status register */
static volatile byte SerFlag;          /* Internal driver flags */

/*
** ===================================================================
**     Method      :  CAN1_SetAcceptanceMode (bean FreescaleCAN)
**
**     Description :
**         Sets the acceptance mode register.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Mode            - Acceptance mode.
**                           Supported modes: 
**                           TWO_32_FILTERS - Two 32-bit acceptance
**                           filters
**                           FOUR_16_FILTERS - Four 16-bit acceptance
**                           filters
**                           EIGHT_8_FILTERS - Eight 8-bit acceptance
**                           filters
**                           FILTER_CLOSED - Filter closed
**     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 - Parameter has incorrect value
** ===================================================================
*/
byte CAN1_SetAcceptanceMode(byte Mode)
{
  if (Mode > 3) {                      /* Is mode parameter greater then 3 */
    return ERR_VALUE;                  /* If yes then error */
  }
  EnterCritical();                     /* Enter critical section */
  CANCTL0_INITRQ = 1;                  /* Disable device */
  while(!CANCTL1_INITAK){}             /* Wait for disable */
  CANIDAC_IDAM = Mode;                 /* Set acceptance mode of the receiver */
  CANCTL0_INITRQ = 0;                  /* Start device */
  while(CANCTL1_INITAK){}              /* Wait for enable */
  /* CANRFLG: WUPIF=1,CSCIF=1,RSTAT1=1,RSTAT0=1,TSTAT1=1,TSTAT0=1,OVRIF=1 */
  CANRFLG |= 254;                      /* Reset error flags */
  /* CANRIER: WUPIE=0,CSCIE=1,RSTATE1=1,RSTATE0=1,TSTATE1=1,TSTATE0=1,OVRIE=0,RXFIE=1 */
  CANRIER = 125;                       /* Enable interrupts */
  ExitCritical();                      /* Exit critical section */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_GetStateRX (bean FreescaleCAN)
**
**     Description :
**         Returns a value of the reception complete flag.
**     Parameters  : None
**     Returns     :
**         ---             - The value of the receiver complete flag
**                           of the given buffer. 
**                           Possible values:
**                           FALSE - message buffer is empty
**                           TRUE - message buffer isn't empty
** ===================================================================
*/
byte CAN1_GetStateRX(void)
{
  return ((SerFlag & FULL_RX_BUF) != 0)? (byte)1 : (byte)0; /* Return status of the RX buffer */
}

/*
** ===================================================================
**     Method      :  CAN1_SetAcceptanceCode (bean FreescaleCAN)
**
**     Description :
**         Sets the acceptance code registers. This method writes a
**         code mask directly to the acceptance code registers.
**     Parameters  :
**         NAME            - DESCRIPTION
**         AccCode1        - Acceptance code for the
**                           message filtering. This acceptance code
**                           will be written to the acceptance code
**                           registers IDAR0-IDAR3. The most
**                           significant byte of the acceptance code
**                           will be written to the IDAR0 register
**                           and the least significant byte of the
**                           acceptance code will be written to the
**                           IDAR3 register.
**         AccCode2        - Acceptance code for the
**                           message filtering. This acceptance code
**                           will be written to the acceptance code
**                           registers IDAR4-IDAR7. The most
**                           significant byte of the acceptance code
**                           will be written to the IDAR4 register
**                           and the least significant byte of the
**                           acceptance code will be written to the
**                           IDAR7 register.
**     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
** ===================================================================
*/
byte CAN1_SetAcceptanceCode(dword AccCode1, dword AccCode2)
{
  EnterCritical();                     /* Enter critical section */
  CANCTL0_INITRQ = 1;                  /* Disable device */
  while(!CANCTL1_INITAK){}             /* Wait for disable */
  CANIDAR3 = ((DwordSwap *)&AccCode1)->b.b3; /* Set acceptance code, register CANIDAR3 */
  CANIDAR2 = ((DwordSwap *)&AccCode1)->b.b2; /* Set acceptance code, register CANIDAR2 */
  CANIDAR1 = ((DwordSwap *)&AccCode1)->b.b1; /* Set acceptance code, register CANIDAR1 */
  CANIDAR0 = ((DwordSwap *)&AccCode1)->b.b0; /* Set acceptance code, register CANIDAR0 */
  CANIDAR7 = ((DwordSwap *)&AccCode2)->b.b3; /* Set acceptance code, register CANIDAR7 */
  CANIDAR6 = ((DwordSwap *)&AccCode2)->b.b2; /* Set acceptance code, register CANIDAR6 */
  CANIDAR5 = ((DwordSwap *)&AccCode2)->b.b1; /* Set acceptance code, register CANIDAR5 */
  CANIDAR4 = ((DwordSwap *)&AccCode2)->b.b0; /* Set acceptance code, register CANIDAR4 */
  CANCTL0_INITRQ = 0;                  /* Start device */
  while(CANCTL1_INITAK){}              /* Wait for enable */
  /* CANRFLG: WUPIF=1,CSCIF=1,RSTAT1=1,RSTAT0=1,TSTAT1=1,TSTAT0=1,OVRIF=1 */
  CANRFLG |= 254;                      /* Reset error flags */
  /* CANRIER: WUPIE=0,CSCIE=1,RSTATE1=1,RSTATE0=1,TSTATE1=1,TSTATE0=1,OVRIE=0,RXFIE=1 */
  CANRIER = 125;                       /* Enable interrupts */
  ExitCritical();                      /* Exit critical section */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_GetError (bean FreescaleCAN)
**
**     Description :
**         Returns the content of the receiver flag register.
**     Parameters  :

⌨️ 快捷键说明

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