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

📄 can1.c

📁 本文件是CAN总线通讯演示程序;将CAN_TEST下的CAN1
💻 C
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------
// CAN1.c
//------------------------------------------------------------------------------
//
//
// DEVICE: C8051F040
//
// AUTHOR: LS
//
// TOOLS:  Keil C-compiler and Silicon Labs IDE
//
//
// CAN1.c and CAN2.c are a simple example of configuring a CAN network to
// transmit and receive data on a CAN network, and how to move information to
// and from CAN RAM message objects.  Each C8051F040-TB CAN node is configured
// to send a message when it's P5.1 button is depressed/released, with a 0x11
// to indicate the button is pushed, and 0x00 when released. Each node also has
// a message object configured to receive messages. The C8051 tests the
// received data and will turn on/off the target board's LED. When one target
// is loaded with CAN2.c and the other is loaded with CAN1.c, one target
// board's push-button will control the other target board's LED, establishing
// a simple control link via the CAN bus and can be observed directly on the
// target boards.
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////

#include "c8051f040.h"                          // SFR declarations

// CAN Protocol Register Index for CAN0ADR, from TABLE 18.1 of the C8051F040
// datasheet
////////////////////////////////////////////////////////////////////////////////
#define CANCTRL            0x00                 //Control Register
#define CANSTAT            0x01                 //Status register
#define ERRCNT             0x02                 //Error Counter Register
#define BITREG             0x03                 //Bit Timing Register
#define INTREG             0x04                 //Interrupt Low Byte Register
#define CANTSTR            0x05                 //Test register
#define BRPEXT             0x06                 //BRP Extension         Register
////////////////////////////////////////////////////////////////////////////////
//IF1 Interface Registers
////////////////////////////////////////////////////////////////////////////////
#define IF1CMDRQST         0x08                 //IF1 Command Rest      Register
#define IF1CMDMSK          0x09                 //IF1 Command Mask      Register
#define IF1MSK1            0x0A                 //IF1 Mask1             Register
#define IF1MSK2            0x0B                 //IF1 Mask2             Register
#define IF1ARB1            0x0C                 //IF1 Arbitration 1     Register
#define IF1ARB2            0x0D                 //IF1 Arbitration 2     Register
#define IF1MSGC            0x0E                 //IF1 Message Control   Register
#define IF1DATA1           0x0F                 //IF1 Data A1           Register
#define IF1DATA2           0x10                 //IF1 Data A2           Register
#define IF1DATB1           0x11                 //IF1 Data B1           Register
#define IF1DATB2           0x12                 //IF1 Data B2           Register
////////////////////////////////////////////////////////////////////////////////
//IF2 Interface Registers
////////////////////////////////////////////////////////////////////////////////
#define IF2CMDRQST         0x20                 //IF2 Command Rest      Register
#define IF2CMDMSK          0x21                 //IF2 Command Mask      Register
#define IF2MSK1            0x22                 //IF2 Mask1             Register
#define IF2MSK2            0x23                 //IF2 Mask2             Register
#define IF2ARB1            0x24                 //IF2 Arbitration 1     Register
#define IF2ARB2            0x25                 //IF2 Arbitration 2     Register
#define IF2MSGC            0x26                 //IF2 Message Control   Register
#define IF2DATA1           0x27                 //IF2 Data A1           Register
#define IF2DATA2           0x28                 //IF2 Data A2           Register
#define IF2DATB1           0x29                 //IF2 Data B1           Register
#define IF2DATB2           0x2A                 //IF2 Data B2           Register
////////////////////////////////////////////////////////////////////////////////
//Message Handler Registers
////////////////////////////////////////////////////////////////////////////////
#define TRANSREQ1          0x40                 //Transmission Rest1 Register
#define TRANSREQ2          0x41                 //Transmission Rest2 Register

#define NEWDAT1            0x48                 //New Data 1            Register
#define NEWDAT2            0x49                 //New Data 2            Register

#define INTPEND1           0x50                 //Interrupt Pending 1   Register
#define INTPEND2           0x51                 //Interrupt Pending 2   Register

#define MSGVAL1            0x58                 //Message Valid 1       Register
#define MSGVAL2            0x59                 //Message Valid 2       Register

////////////////////////////////////////////////////////////////////////////////
//Global Variables
////////////////////////////////////////////////////////////////////////////////
char MsgNum;
char status;
int i;
int MOTwoIndex = 0;
int MOOneIndex = 0;
int StatusCopy;
int RXbuffer [4];
int TXbuffer [8];
int MsgIntNum;
int Temperature;

#define BUTTON (P5 & 0x01) //P5.1



#define LED_0 P5 = 0x0f //P5.4
#define LED_1 P5 = 0x1f


sfr16 CAN0DAT = 0xD8;



////////////////////////////////////////////////////////////////////////////////
// Function PROTOTYPES
////////////////////////////////////////////////////////////////////////////////

// Initialize Message Object
void clear_msg_objects (void);
void init_msg_object_TX (char MsgNum);
void init_msg_object_RX (char MsgNum);
void start_CAN (void);
void transmit_turn_LED_ON (char MsgNum);
void transmit_turn_LED_OFF (char MsgNum);
void receive_data (char MsgNum);
void external_osc (void);
void config_IO (void);
void flash_LED (void);
void test_reg_write (char test);
void stop_CAN (void);
void DelayMs(unsigned int n);

////////////////////////////////////////////////////////////////////////////////
// MAIN Routine
////////////////////////////////////////////////////////////////////////////////
void main (void) {
  char SFRPAGE_SAVE = SFRPAGE;        // Save SFRPAGE
  SFRPAGE = CONFIG_PAGE;

  // disable watchdog timer
  WDTCN = 0xde;
  WDTCN = 0xad;

  //configure Port I/O
  config_IO();

  // switch to external oscillator
  external_osc();


////////////////////////////////////////////////////////////////////////////////
// Configure CAN communications
//
// IF1 used for procedures calles by main program
// IF2 used for interrupt service procedure receive_data
//
// Message Object assignments:
//  0x02: Used to transmit commands to toggle its LED, arbitration number 1
//
////////////////////////////////////////////////////////////////////////////////

  // Clear CAN RAM
  clear_msg_objects();

  // Initialize message object to transmit data
  init_msg_object_TX (0x02);

  // Initialize message object to receive data
  init_msg_object_RX (0x01);

  // Enable CAN interrupts in CIP-51
  EIE2 = 0x20;

  //Function call to start CAN
  start_CAN();

  //Global enable 8051 interrupts
  EA = 1;

  //Loop and wait for interrupts
  while (1)
    {
      SFRPAGE_SAVE = SFRPAGE;
      SFRPAGE = CONFIG_PAGE;
      DelayMs(10);
      if (BUTTON == 0)
      {
        SFRPAGE = SFRPAGE_SAVE;
        transmit_turn_LED_OFF(0x02);
      }
      else
      {
        SFRPAGE = SFRPAGE_SAVE;
        transmit_turn_LED_ON(0x02);
      }
    }
}

void DelayMs(unsigned int n)//Delay (n)MS
{
	unsigned int i;
	for(; n > 0; n--)
	{
		for(i = 2211; i > 0; i--);
	}
}

////////////////////////////////////////////////////////////////////////////////
// Set up C8051F040
////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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