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

📄 mco.h

📁 运行在AT89C51CC01单片机上的CANOpen协议栈源代码
💻 H
字号:
/**************************************************************************
MODULE:    MCO
CONTAINS:  MicroCANopen implementation
COPYRIGHT: Embedded Systems Academy, Inc. 2002.
           All rights reserved. www.microcanopen.com
           This software was written in accordance to the guidelines at
		   www.esacademy.com/software/softwarestyleguide.pdf
DISCLAIM:  Read and understand our disclaimer before using this code!
           www.esacademy.com/disclaim.htm
LICENSE:   Users that have purchased a license for PCANopenMagic
           (www.esacademy.com/software/pcanopenmagic)
           may use this code in commercial projects.
           Otherwise only educational use is acceptable.
VERSION:   1.01, Pf/Aa/Ck 17-DEC-02
---------------------------------------------------------------------------
HISTORY:   1.01, Pf 17-DEC-02, Made Object Dictionary more readable
           1.00, Pf 07-OCT-02, First Published Version
***************************************************************************/ 
#ifndef _MCO_H
#define _MCO_H


/**************************************************************************
DEFINES: CONST ENTRIES IN OBJECT DICTIONARY
**************************************************************************/
// This is the joystick implementation
#define JOYSTICK

#define OD_NODEID        5
#define OD_HEARTBEAT     1000L
#define OD_DEVICE_TYPE   0x01050191L // DS401 - Joystick
#define OD_VENDOR_ID     0x03455341L // Embedded Systems Solutions
#define OD_PRODUCT_CODE  0x41543035L // "AT05"
#define OD_REVISION      0x00010014L // 1.20
#define OD_SERIAL        0xFFFFFFFFL


/**************************************************************************
DEFINES: ENABLING/DISABLING CODE FUNCTIONALITY
**************************************************************************/

// Maximum number of transmit PDOs (0 to 4)
#define NR_OF_TPDOS 2

// Maximum number of receive PDOs (0 to 4)
#define NR_OF_RPDOS 0

// If defined, 1 or more TPDOs use the event timer
#define USE_EVENT_TIME

// If defined, 1 or more TPDOs are change-of-state and use the inhibit timer
#define USE_INHIBIT_TIME

#ifndef USE_EVENT_TIME
  #ifndef USE_INHIBIT_TIME
ERROR: At least one, USE_EVENT_TIME or USE_INHIBIT_TIME must be defined!
  #endif
#endif

// If defined, OD entry [1017,00] is supported with SDO read/write accesses
// This is also an example on how to implement dynamic/variable OD entries
#define DYNAMIC_HEARTBEAT

// If defined, node starts up automatically (does not wait for NMT master)
// #define AUTOSTART

// If defined, application supports CANopen RUN and ERR LEDs
// Define the ports used for the LEDs
#define USE_LED
#ifdef USE_LED
#define LED_RUN P0_4 // Run LED is on P0.4
#define LED_ERR P0_5 // Error LED is on P0.5
#define LED_OFF 0
#define LED_ON 0xFF
#define LED_BLINK 0x7F
#define LED_FLASH1 1
#endif

// If defined, all parameters passed to functions are checked for consistency. 
// On failures, the user function MCOUSER_FatalError is called.
#define CHECK_PARAMETERS


/**************************************************************************
END OF CUSTOMIZATION AREA - DON'T CHANGE ANYTHING BELOW
**************************************************************************/

/**************************************************************************
GLOBAL TYPE DEFINITIONS
**************************************************************************/

// Standard data types
#define BYTE  unsigned char
#define WORD  unsigned int
#define DWORD unsigned long

// Boolean expressions
#define BOOLEAN unsigned char
#define TRUE 0xFF
#define FALSE 0

// Data structure for a single CAN message 
typedef struct
{
  WORD ID;                  // Message Identifier 
  BYTE LEN;                 // Data length (0-8) 
  BYTE BUF[8];              // Data buffer 
} CAN_MSG;

// This structure holds all node specific configuration
typedef struct
{
  BYTE Node_ID;             // Current Node ID (1-126)
  BYTE error_code;          // Bits: 0=RxQueue 1=TxQueue 3=CAN
  WORD Baudrate;            // Current Baud rate in kbit
  WORD heartbeat_time;      // Heartbeat time in ms
  WORD heartbeat_timestamp; // Timestamp of last heartbeat
  CAN_MSG heartbeat_msg;    // Heartbeat message contents
  BYTE error_register;      // Error regiter for OD entry [1001,00]
} MCO_CONFIG;

// This structure holds all the TPDO configuration data for one TPDO
typedef struct 
{
#ifdef USE_EVENT_TIME
  WORD event_time;          // Event timer in ms (0 for COS only operation)
  WORD event_timestamp;     // If event timer is used, this is the 
                            // timestamp for the next transmission
#endif
#ifdef USE_INHIBIT_TIME
  WORD inhibit_time;        // Inhibit timer in ms (0 if COS not used)
  WORD inhibit_timestamp;   // If inhibit timer is used, this is the 
                            // timestamp for the next transmission
  BYTE inhibit_status;      // 0: Inhibit timer not started or expired
                            // 1: Inhibit timer started
                            // 2: Transmit msg waiting for expiration of inhibit
#endif
  BYTE *pData;              // Pointer to application data
  CAN_MSG CAN;              // Current/last CAN message to be transmitted
} TPDO_CONFIG;

// This structure holds all the RPDO configuration data for one RPDO
typedef struct 
{
  WORD CANID;               // Message Identifier 
  BYTE LEN;                 // Data length (0-8) 
  BYTE *DAT;                // Pointer to destination of data 
} RPDO_CONFIG;


/**************************************************************************
GLOBAL FUNCTIONS
**************************************************************************/

/**************************************************************************
DOES: This function initializes the CANopen protocol stack.
      It must be called from within MCOUSER_ResetApplication.
**************************************************************************/
void MCO_Init 
  (
  WORD Baudrate,  // CAN baudrate in kbit(1000,800,500,250,125,50,25 or 10)
  BYTE Node_ID,   // CANopen node ID (1-126)
  WORD Heartbeat  // Heartbeat time in ms (0 for none)
  );


/**************************************************************************
DOES: This function initializes a transmit PDO. Once initialized, the 
      MicroCANopen stack automatically handles transmitting the PDO.
      The application can directly change the data at any time.
NOTE: For data consistency, the application should not write to the data
      while function MCO_ProcessStack executes.
**************************************************************************/
void MCO_InitTPDO
  (
  BYTE PDO_NR,     // TPDO number (1-4)
  WORD CAN_ID,     // CAN identifier to be used (set to 0 to use default)
  WORD event_tim,  // Transmitted every event_tim ms 
                   // (set to 0 if ONLY inhibit_tim should be used)
  WORD inhibit_tim,// Inhibit time in ms for change-of-state transmit
                   // (set to 0 if ONLY event_tim should be used)
  BYTE len,        // Number of data bytes in TPDO
  BYTE *dat        // Pointer to transmit data bytes
  );


/**************************************************************************
DOES: This function initializes a receive PDO. Once initialized, the 
      MicroCANopen stack automatically updates the data at *dat.
NOTE: For data consistency, the application should not read the data
      while function MCO_ProcessStack executes.
**************************************************************************/
void MCO_InitRPDO
  (
  BYTE PDO_NR,     // RPDO number (1-4)
  WORD CAN_ID,     // CAN identifier to be used (set to 0 to use default)
  BYTE len,        // Number of data bytes in RPDO
  BYTE *dat        // Pointer to destination location of data bytes
  );


/**************************************************************************
DOES: This function implements the main MicroCANopen protocol stack. 
      It must be called frequently to ensure proper operation of the
      communication stack. 
      Typically it is called from the while(1) loop in main.
**************************************************************************/
BYTE MCO_ProcessStack
  ( // Returns 0 if nothing needed to be done
    // Returns 1 if a CAN message was received or sent
  void
  );


/**************************************************************************
USER CALL-BACK FUNCTIONS
These must be implemented by the application.
**************************************************************************/

/**************************************************************************
DOES: This function resets the application. It is called from within the
      CANopen protocol stack, if a NMT master message was received that
      demanded "Reset Application".
**************************************************************************/
void MCOUSER_ResetApplication
  (
  void
  );


/**************************************************************************
DOES: This function both resets and initializes both the CAN interface
      and the CANopen protocol stack. It is called from within the
      CANopen protocol stack, if a NMT master message was received that
      demanded "Reset Communication".
      This function should call MCO_Init and MCO_InitTPDO/MCO_InitRPDO.
**************************************************************************/
void MCOUSER_ResetCommunication
  (
  void
  );


/**************************************************************************
DOES: This function is called if a fatal error occurred. 
      Error codes of mcohwxxx.c are in the range of 0x8000 to 0x87FF.
      Error codes of mco.c are in the range of 0x8800 to 0x8FFF. 
      All other error codes may be used by the application.
**************************************************************************/
void MCOUSER_FatalError
  (
  WORD ErrCode // To debug, search source code for the ErrCode encountered
  );

#endif

⌨️ 快捷键说明

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