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

📄 can.c

📁 本程序是针对Infineon公司的XC167CI处理器而编写的CAN网络程序。CAN(控制局域网)协议是一种广泛的应用于汽车电子的网络协议。本程序建立了三个CAN节点
💻 C
📖 第 1 页 / 共 3 页
字号:
//****************************************************************************
// @Module        TwinCAN Module (CAN)
// @Filename      CAN.C
// @Project       cannet.dav
//----------------------------------------------------------------------------
// @Controller    Infineon XC167CI-32F20
//
// @Compiler      Keil
//
// @Codegenerator 2.4
//
// @Description   This file contains functions that use the CAN module.
//
//----------------------------------------------------------------------------
// @Date          2006-12-10 17:52:24
//
//****************************************************************************

// USER CODE BEGIN (CAN_General,1)

// USER CODE END



//****************************************************************************
// @Project Includes
//****************************************************************************

#include "MAIN.H"

// USER CODE BEGIN (CAN_General,2)

// USER CODE END


//****************************************************************************
// @Macros
//****************************************************************************

// USER CODE BEGIN (CAN_General,3)

// USER CODE END


//****************************************************************************
// @Defines
//****************************************************************************

// Structure for a single TwinCAN object
// A total of 31 such object structures exists

struct stCanObj 
{
  ubyte  ubData[8];  // Message Data 0..7
  ulong  ulCANAR;    // Arbitration Register
  ulong  ulCANAMR;   // Acceptance Mask Register
  uword  uwMSGCTR;   // Message Control Register
  uword  uwCounter;  // Frame Counter
  uword  uwMSGCFG;   // Message Configuration Register
  uword  uwINP;      // Interrupt Node Pointer
  uword  uwCANFCR;   // FIFO / Gateway Control Register 
  uword  uwCANPTR;   // FIFO Pointer
  ulong  ulReserved; // Reserved
};

#define CAN_HWOBJ ((struct stCanObj volatile far *) 0x200300)

// USER CODE BEGIN (CAN_General,4)

// USER CODE END


//****************************************************************************
// @Typedefs
//****************************************************************************

// USER CODE BEGIN (CAN_General,5)

// USER CODE END


//****************************************************************************
// @Imported Global Variables
//****************************************************************************

// USER CODE BEGIN (CAN_General,6)

// USER CODE END


//****************************************************************************
// @Global Variables
//****************************************************************************


// USER CODE BEGIN (CAN_General,7)

// USER CODE END


//****************************************************************************
// @External Prototypes
//****************************************************************************

// USER CODE BEGIN (CAN_General,8)

// USER CODE END


//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

// USER CODE BEGIN (CAN_General,9)

// USER CODE END


//****************************************************************************
// @Function      void CAN_vInit(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the initialization function of the CAN function 
//                library. It is assumed that the SFRs used by this library 
//                are in its reset state. 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2006-12-10
//
//****************************************************************************

// USER CODE BEGIN (Init,1)

// USER CODE END

void CAN_vInit(void)
{

  // USER CODE BEGIN (Init,2)

  // USER CODE END

  ///  -----------------------------------------------------------------------
  ///  Configuration of CAN Node A:
  ///  -----------------------------------------------------------------------

  ///  General Configuration of the Node A:
  ///  - set INIT and CCE

  CAN_ACR        =  0x0041;      // load global control register

  ///  -----------------------------------------------------------------------
  ///  Configuration of CAN Node B:
  ///  -----------------------------------------------------------------------

  ///  General Configuration of the Node B:
  ///  - set INIT and CCE
  ///  - enable interrupt generation when a message transfer is completed
  ///  - transmit / receive OK interrupt node pointer: TwinCAN SRN 0

  CAN_BCR        =  0x0045;      // load global control register
  CAN_BGINP      =  0x0000;      // load global interrupt node pointer 
                                 // register

  ///  Configuration of the Node B Error Counter:
  ///  - the error warning threshold value (warning level) is 96

  CAN_BECNTH     =  0x0060;      // load error counter register high

  ///  Configuration of the used CAN Port Pins:
  ///  - P9.0 is used for CAN Interface Input (RXDCB)
  ///  - P9.1 is used for CAN Interface Output (TXDCB)

  ALTSEL0P9     |=  0x0002;      // select alternate output function
  ALTSEL1P9     |=  0x0002;      // select alternate output function
  DP9  = (DP9  & ~(uword)0x0002) | 0x0002;    //set direction register

  ///  Configuration of the Node B Baud Rate:
  ///  - required baud rate = 100.000 kbaud
  ///  - real baud rate     = 100.000 kbaud
  ///  - sample point       = 60.00 %
  ///  - there are 5 time quanta before sample point
  ///  - there are 4 time quanta after sample point
  ///  - the (re)synchronization jump width is 2 time quanta

  CAN_BBTRL      =  0x3453;      // load bit timing register low

  CAN_BBTRH      =  0x0000;      // load bit timing register high

  CAN_BFCRL      =  0x0000;      // load frame counter timing register low

  CAN_BFCRH      =  0x0000;      // load frame counter timing register high

  ///  -----------------------------------------------------------------------
  ///  Configuration of the CAN Message Objects 0 - 31:
  ///  -----------------------------------------------------------------------

  ///  -----------------------------------------------------------------------
  ///  Configuration of Message Object 0:
  ///  -----------------------------------------------------------------------
  ///  - message object 0 is valid
  ///  - enable receive interrupt; bit INTPND is set after successfull 
  ///    reception of a frame

  ///  - message object is used as receive object
  ///  - standard 11-bit identifier
  ///  - 8 valid data bytes
  ///  - this message object works with CAN node B
  ///  - remote monitoring is disabled
  ///  - receive interrupt node pointer: TwinCAN SRN 1

  CAN_MSGCFGL0   =  0x0082;      // load message configuration register low
  CAN_MSGCFGH0   =  0x0001;      // load message configuration register high

  ///  - acceptance mask 11-bit: 0x7FF
  ///  - identifier 11-bit:      0x122

  CAN_MSGAMRL0   =  0xFFFF;      // load acceptance mask register low
  CAN_MSGAMRH0   =  0xFFFF;      // load acceptance mask register high
  CAN_MSGARL0    =  0x0000;      // load arbitration register low
  CAN_MSGARH0    =  0x0488;      // load arbitration register high
  CAN_MSGDRL00   =  0x0000;      // load data register 0 low
  CAN_MSGDRH00   =  0x0000;      // load data register 0 high
  CAN_MSGDRL04   =  0x0000;      // load data register 4 low
  CAN_MSGDRH04   =  0x0000;      // load data register 4 high

  ///  - functionality of standard message object

  CAN_MSGFGCRL0  =  0x0000;      // load FIFO/gateway control register low
  CAN_MSGFGCRH0  =  0x0000;      // load FIFO/gateway control register high

  CAN_MSGCTRH0   =  0x0000;      // load message control register high
  CAN_MSGCTRL0   =  0x5599;      // load message control register low

  ///  -----------------------------------------------------------------------
  ///  Configuration of Message Object 1:
  ///  -----------------------------------------------------------------------
  ///  - message object 1 is valid
  ///  - enable transmit interrupt; bit INTPND is set after successfull 
  ///    transmission of a frame

  ///  - message object is used as transmit object
  ///  - standard 11-bit identifier
  ///  - 8 valid data bytes
  ///  - this message object works with CAN node B
  ///  - remote monitoring is disabled
  ///  - transmit interrupt node pointer: TwinCAN SRN 2

  CAN_MSGCFGL1   =  0x008A;      // load message configuration register low
  CAN_MSGCFGH1   =  0x0022;      // load message configuration register high

  ///  - acceptance mask 11-bit: 0x7FF
  ///  - identifier 11-bit:      0x123

  CAN_MSGAMRL1   =  0xFFFF;      // load acceptance mask register low
  CAN_MSGAMRH1   =  0xFFFF;      // load acceptance mask register high
  CAN_MSGARL1    =  0x0000;      // load arbitration register low
  CAN_MSGARH1    =  0x048C;      // load arbitration register high
  CAN_MSGDRL10   =  0x0000;      // load data register 0 low
  CAN_MSGDRH10   =  0x0000;      // load data register 0 high
  CAN_MSGDRL14   =  0x0000;      // load data register 4 low
  CAN_MSGDRH14   =  0x0000;      // load data register 4 high

  ///  - functionality of standard message object

  CAN_MSGFGCRL1  =  0x0000;      // load FIFO/gateway control register low
  CAN_MSGFGCRH1  =  0x0001;      // load FIFO/gateway control register high

  CAN_MSGCTRH1   =  0x0000;      // load message control register high
  CAN_MSGCTRL1   =  0x55A5;      // load message control register low

  ///  -----------------------------------------------------------------------
  ///  Configuration of Message Object 2:
  ///  -----------------------------------------------------------------------
  ///  - message object 2 is valid
  ///  - enable transmit interrupt; bit INTPND is set after successfull 
  ///    transmission of a frame

  ///  - message object is used as transmit object
  ///  - standard 11-bit identifier
  ///  - 8 valid data bytes
  ///  - this message object works with CAN node B
  ///  - remote monitoring is disabled
  ///  - transmit interrupt node pointer: TwinCAN SRN 3

  CAN_MSGCFGL2   =  0x008A;      // load message configuration register low
  CAN_MSGCFGH2   =  0x0030;      // load message configuration register high

  ///  - acceptance mask 11-bit: 0x7FF
  ///  - identifier 11-bit:      0x133

  CAN_MSGAMRL2   =  0xFFFF;      // load acceptance mask register low
  CAN_MSGAMRH2   =  0xFFFF;      // load acceptance mask register high
  CAN_MSGARL2    =  0x0000;      // load arbitration register low
  CAN_MSGARH2    =  0x04CC;      // load arbitration register high
  CAN_MSGDRL20   =  0x0000;      // load data register 0 low
  CAN_MSGDRH20   =  0x0000;      // load data register 0 high
  CAN_MSGDRL24   =  0x0000;      // load data register 4 low
  CAN_MSGDRH24   =  0x0000;      // load data register 4 high

  ///  - functionality of standard message object

  CAN_MSGFGCRL2  =  0x0000;      // load FIFO/gateway control register low
  CAN_MSGFGCRH2  =  0x0002;      // load FIFO/gateway control register high

  CAN_MSGCTRH2   =  0x0000;      // load message control register high
  CAN_MSGCTRL2   =  0x55A5;      // load message control register low

  ///  -----------------------------------------------------------------------
  ///  Configuration of Message Object 3:
  ///  -----------------------------------------------------------------------
  ///  - message object 3 is valid
  ///  - enable receive interrupt; bit INTPND is set after successfull 
  ///    reception of a frame

  ///  - message object is used as receive object
  ///  - standard 11-bit identifier
  ///  - 8 valid data bytes
  ///  - this message object works with CAN node B
  ///  - remote monitoring is disabled
  ///  - receive interrupt node pointer: TwinCAN SRN 4

  CAN_MSGCFGL3   =  0x0082;      // load message configuration register low
  CAN_MSGCFGH3   =  0x0004;      // load message configuration register high

  ///  - acceptance mask 11-bit: 0x7FD
  ///  - identifier 11-bit:      0x130

  CAN_MSGAMRL3   =  0x0000;      // load acceptance mask register low
  CAN_MSGAMRH3   =  0xFFF4;      // load acceptance mask register high
  CAN_MSGARL3    =  0x0000;      // load arbitration register low
  CAN_MSGARH3    =  0x04C0;      // load arbitration register high
  CAN_MSGDRL30   =  0x0000;      // load data register 0 low
  CAN_MSGDRH30   =  0x0000;      // load data register 0 high
  CAN_MSGDRL34   =  0x0000;      // load data register 4 low
  CAN_MSGDRH34   =  0x0000;      // load data register 4 high

  ///  - functionality of standard message object

  CAN_MSGFGCRL3  =  0x0000;      // load FIFO/gateway control register low
  CAN_MSGFGCRH3  =  0x0003;      // load FIFO/gateway control register high

  CAN_MSGCTRH3   =  0x0000;      // load message control register high
  CAN_MSGCTRL3   =  0x5599;      // load message control register low


  ///  -----------------------------------------------------------------------
  ///  Configuration of Service Request Nodes 0 - 7:
  ///  -----------------------------------------------------------------------
  ///  SRN0 service request node configuration:
  ///  - SRN0 interrupt priority level (ILVL) = 15
  ///  - SRN0 interrupt group level (GLVL) = 0
  ///  - SRN0 group priority extension (GPX) = 0

  CAN_0IC        =  0x007C;     

  ///  Use PEC channel 4 for CAN INT 0:
  ///  - normal interrupt
  ///  - pointers are not modified
  ///  - transfer a word
  ///  - service End of PEC interrrupt by a EOP interrupt node is disabled

  PECC4          =  0x0000;      // load PECC4 control register


  ///  SRN1 service request node configuration:
  ///  - SRN1 interrupt priority level (ILVL) = 14
  ///  - SRN1 interrupt group level (GLVL) = 1
  ///  - SRN1 group priority extension (GPX) = 0

  CAN_1IC        =  0x0079;     

  ///  Use PEC channel 1 for CAN INT 1:
  ///  - normal interrupt
  ///  - pointers are not modified
  ///  - transfer a word
  ///  - service End of PEC interrrupt by a EOP interrupt node is disabled

⌨️ 快捷键说明

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