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

📄 can2.lst

📁 本文件是CAN总线通讯演示程序;将CAN_TEST下的CAN1
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.02b   CAN2                                                                 01/20/2006 11:20:24 PAGE 1   


C51 COMPILER V7.02b, COMPILATION OF MODULE CAN2
OBJECT MODULE PLACED IN CAN2.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE CAN2.c BROWSE DEBUG OBJECTEXTEND TABS(3)

stmt level    source

   1          //------------------------------------------------------------------------------
   2          // CAN2.c
   3          //------------------------------------------------------------------------------
   4          //
   5          //
   6          // DEVICE: C8051F040
   7          //
   8          // AUTHOR: LS
   9          //
  10          // TOOLS:  Keil C-compiler and Silicon Labs IDE
  11          //
  12          //
  13          // CAN1.c and CAN2.c are a simple example of configuring a CAN network to
  14          // transmit and receive data on a CAN network, and how to move information to
  15          // and from CAN RAM message objects.  Each C8051F040-TB CAN node is configured
  16          // to send a message when it's P5.4 button is depressed/released, with a 0x11
  17          // to indicate the button is pushed, and 0x00 when released. Each node also has
  18          // a message object configured to receive messages. The C8051 tests the
  19          // received data and will turn on/off the target board's LED. When one target
  20          // is loaded with CAN2.c and the other is loaded with CAN1.c, one target
  21          // board's push-button will control the other target board's LED, establishing
  22          // a simple control link via the CAN bus and can be observed directly on the
  23          // target boards.
  24          ////////////////////////////////////////////////////////////////////////////////
  25          
  26          ////////////////////////////////////////////////////////////////////////////////
  27          // Includes
  28          ////////////////////////////////////////////////////////////////////////////////
  29          
  30          #include "c8051f040.h"                          // SFR declarations
  31          
  32          // CAN Protocol Register Index for CAN0ADR, from TABLE 18.1 of the C8051F040
  33          // datasheet
  34          ////////////////////////////////////////////////////////////////////////////////
  35          #define CANCTRL            0x00                 //Control Register
  36          #define CANSTAT            0x01                 //Status register
  37          #define ERRCNT             0x02                 //Error Counter Register
  38          #define BITREG             0x03                 //Bit Timing Register
  39          #define INTREG             0x04                 //Interrupt Low Byte Register
  40          #define CANTSTR            0x05                 //Test register
  41          #define BRPEXT             0x06                 //BRP Extension         Register
  42          ////////////////////////////////////////////////////////////////////////////////
  43          //IF1 Interface Registers
  44          ////////////////////////////////////////////////////////////////////////////////
  45          #define IF1CMDRQST         0x08                 //IF1 Command Rest      Register
  46          #define IF1CMDMSK          0x09                 //IF1 Command Mask      Register
  47          #define IF1MSK1            0x0A                 //IF1 Mask1             Register
  48          #define IF1MSK2            0x0B                 //IF1 Mask2             Register
  49          #define IF1ARB1            0x0C                 //IF1 Arbitration 1     Register
  50          #define IF1ARB2            0x0D                 //IF1 Arbitration 2     Register
  51          #define IF1MSGC            0x0E                 //IF1 Message Control   Register
  52          #define IF1DATA1           0x0F                 //IF1 Data A1           Register
  53          #define IF1DATA2           0x10                 //IF1 Data A2           Register
  54          #define IF1DATB1           0x11                 //IF1 Data B1           Register
  55          #define IF1DATB2           0x12                 //IF1 Data B2           Register
C51 COMPILER V7.02b   CAN2                                                                 01/20/2006 11:20:24 PAGE 2   

  56          ////////////////////////////////////////////////////////////////////////////////
  57          //IF2 Interface Registers
  58          ////////////////////////////////////////////////////////////////////////////////
  59          #define IF2CMDRQST         0x20                 //IF2 Command Rest      Register
  60          #define IF2CMDMSK          0x21                 //IF2 Command Mask      Register
  61          #define IF2MSK1            0x22                 //IF2 Mask1             Register
  62          #define IF2MSK2            0x23                 //IF2 Mask2             Register
  63          #define IF2ARB1            0x24                 //IF2 Arbitration 1     Register
  64          #define IF2ARB2            0x25                 //IF2 Arbitration 2     Register
  65          #define IF2MSGC            0x26                 //IF2 Message Control   Register
  66          #define IF2DATA1           0x27                 //IF2 Data A1           Register
  67          #define IF2DATA2           0x28                 //IF2 Data A2           Register
  68          #define IF2DATB1           0x29                 //IF2 Data B1           Register
  69          #define IF2DATB2           0x2A                 //IF2 Data B2           Register
  70          ////////////////////////////////////////////////////////////////////////////////
  71          //Message Handler Registers
  72          ////////////////////////////////////////////////////////////////////////////////
  73          #define TRANSREQ1          0x40                 //Transmission Rest1 Register
  74          #define TRANSREQ2          0x41                 //Transmission Rest2 Register
  75          
  76          #define NEWDAT1            0x48                 //New Data 1            Register
  77          #define NEWDAT2            0x49                 //New Data 2            Register
  78          
  79          #define INTPEND1           0x50                 //Interrupt Pending 1   Register
  80          #define INTPEND2           0x51                 //Interrupt Pending 2   Register
  81          
  82          #define MSGVAL1            0x58                 //Message Valid 1       Register
  83          #define MSGVAL2            0x59                 //Message Valid 2       Register
  84          
  85          ////////////////////////////////////////////////////////////////////////////////
  86          //Global Variables
  87          ////////////////////////////////////////////////////////////////////////////////
  88          char MsgNum;
  89          char status;
  90          int i;
  91          int MOTwoIndex = 0;
  92          int MOOneIndex = 0;
  93          int StatusCopy;
  94          int RXbuffer [4];
  95          int TXbuffer [8];
  96          int MsgIntNum;
  97          int Temperature;
  98          
  99          #define BUTTON (P5 & 0x01) //P5.1
 100          
 101          
 102          
 103          #define LED_0 P5 = 0x0f //P5.4
 104          #define LED_1 P5 = 0x1f
 105          
 106          sfr16 CAN0DAT = 0xD8;
 107          
 108          
 109          
 110          ////////////////////////////////////////////////////////////////////////////////
 111          // Function PROTOTYPES
 112          ////////////////////////////////////////////////////////////////////////////////
 113          
 114          // Initialize Message Object
 115          void clear_msg_objects (void);
 116          void init_msg_object_TX (char MsgNum);
 117          void init_msg_object_RX (char MsgNum);
C51 COMPILER V7.02b   CAN2                                                                 01/20/2006 11:20:24 PAGE 3   

 118          void start_CAN (void);
 119          void transmit_turn_LED_ON (char MsgNum);
 120          void transmit_turn_LED_OFF (char MsgNum);
 121          void receive_data (char MsgNum);
 122          void external_osc (void);
 123          void config_IO (void);
 124          void flash_LED (void);
 125          void test_reg_write (char test);
 126          void stop_CAN (void);
 127          void DelayMs(unsigned int n);
 128          
 129          ////////////////////////////////////////////////////////////////////////////////
 130          // MAIN Routine
 131          ////////////////////////////////////////////////////////////////////////////////
 132          void main (void) {
 133   1        char SFRPAGE_SAVE = SFRPAGE;        // Save SFRPAGE
 134   1        SFRPAGE = CONFIG_PAGE;
 135   1      
 136   1        // disable watchdog timer
 137   1        WDTCN = 0xde;
 138   1        WDTCN = 0xad;
 139   1      
 140   1        //configure Port I/O
 141   1        config_IO();
 142   1      
 143   1        // switch to external oscillator
 144   1        external_osc();
 145   1      
 146   1      
 147   1      ////////////////////////////////////////////////////////////////////////////////
 148   1      // Configure CAN communications
 149   1      //
 150   1      // IF1 used for procedures calles by main program
 151   1      // IF2 used for interrupt service procedure receive_data
 152   1      //
 153   1      // Message Object assignments:
 154   1      //  0x02: Used to transmit commands to toggle its LED, arbitration number 1
 155   1      //
 156   1      ////////////////////////////////////////////////////////////////////////////////
 157   1      
 158   1        // Clear CAN RAM
 159   1        clear_msg_objects();
 160   1      
 161   1        // Initialize message object to transmit data
 162   1        init_msg_object_TX (0x02);
 163   1      
 164   1        // Initialize message object to receive data
 165   1        init_msg_object_RX (0x01);
 166   1      
 167   1        // Enable CAN interrupts in CIP-51
 168   1        EIE2 = 0x20;
 169   1      
 170   1        //Function call to start CAN
 171   1        start_CAN();
 172   1      
 173   1        //Global enable 8051 interrupts
 174   1        EA = 1;
 175   1      
 176   1        //Loop and wait for interrupts
 177   1        while (1)
 178   1          {
 179   2            SFRPAGE_SAVE = SFRPAGE;
C51 COMPILER V7.02b   CAN2                                                                 01/20/2006 11:20:24 PAGE 4   

 180   2            SFRPAGE = CONFIG_PAGE;
 181   2            DelayMs(10);
 182   2            if (BUTTON == 0)
 183   2            {
 184   3              SFRPAGE = SFRPAGE_SAVE;
 185   3              transmit_turn_LED_OFF(0x02);
 186   3            }
 187   2            else
 188   2            {
 189   3              SFRPAGE = SFRPAGE_SAVE;
 190   3              transmit_turn_LED_ON(0x02);
 191   3            }
 192   2          }
 193   1      }
 194          
 195          
 196          void DelayMs(unsigned int n)//Delay (n)MS
 197          {
 198   1         unsigned int i;
 199   1         for(; n > 0; n--)
 200   1         {
 201   2            for(i = 2211; i > 0; i--);
 202   2         }
 203   1      }
 204          
 205          ////////////////////////////////////////////////////////////////////////////////
 206          // Set up C8051F040
 207          ////////////////////////////////////////////////////////////////////////////////
 208          
 209          // Switch to external oscillator

⌨️ 快捷键说明

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