CAN总线硬件连接及程序.docx
《CAN总线硬件连接及程序.docx》由会员分享,可在线阅读,更多相关《CAN总线硬件连接及程序.docx(26页珍藏版)》请在冰豆网上搜索。
CAN总线硬件连接及程序
1.// F04x_CAN1.c
2.//------------------------------------------------------------------------------
3.//
4.//
5.// DEVICE:
C8051F040
6.//
7.// AUTHOR:
LS
8.//
9.// TOOLS:
Keil C-compiler and Silicon Labs IDE
10.//
11.//
12.// CAN1.c and CAN2.c are a simple example of configuring a CAN network to
13.// transmit and receive data on a CAN network, and how to move information to
14.// and from CAN RAM message objects. Each C8051F040-TB CAN node is configured
15.// to send a message when it's P3.7 button is depressed/released, with a 0x11
16.// to indicate the button is pushed, and 0x00 when released. Each node also has
17.// a message object configured to receive messages. The C8051 tests the
18.// received data and will turn on/off the target board's LED. When one target
19.// is loaded with CAN2.c and the other is loaded with CAN1.c, one target
20.// board's push-button will control the other target board's LED, establishing
21.// a simple control link via the CAN bus and can be observed directly on the
22.// target boards.
23.////////////////////////////////////////////////////////////////////////////////
24.
25.////////////////////////////////////////////////////////////////////////////////
26.// Includes
27.////////////////////////////////////////////////////////////////////////////////
28.
29.#include // SFR declarations
30.
31.// CAN Protocol Register Index for CAN0ADR, from TABLE 18.1 of the C8051F040
32.// datasheet
33.////////////////////////////////////////////////////////////////////////////////
34.#define CANCTRL 0x00 //Control Register
35.#define CANSTAT 0x01 //Status register
36.#define ERRCNT 0x02 //Error Counter Register
37.#define BITREG 0x03 //Bit Timing Register
38.#define INTREG 0x04 //Interrupt Low Byte Register
39.#define CANTSTR 0x05 //Test register
40.#define BRPEXT 0x06 //BRP Extension Register
41.////////////////////////////////////////////////////////////////////////////////
42.//IF1 Interface Registers
43.////////////////////////////////////////////////////////////////////////////////
44.#define IF1CMDRQST 0x08 //IF1 Command Rest Register
45.#define IF1CMDMSK 0x09 //IF1 Command Mask Register
46.#define IF1MSK1 0x0A //IF1 Mask1 Register
47.#define IF1MSK2 0x0B //IF1 Mask2 Register
48.#define IF1ARB1 0x0C //IF1 Arbitration 1 Register
49.#define IF1ARB2 0x0D //IF1 Arbitration 2 Register
50.#define IF1MSGC 0x0E //IF1 Message Control Register
51.#define IF1DATA1 0x0F //IF1 Data A1 Register
52.#define IF1DATA2 0x10 //IF1 Data A2 Register
53.#define IF1DATB1 0x11 //IF1 Data B1 Register
54.#define IF1DATB2 0x12 //IF1 Data B2 Register
55.////////////////////////////////////////////////////////////////////////////////
56.//IF2 Interface Registers
57.////////////////////////////////////////////////////////////////////////////////
58.#define IF2CMDRQST 0x20 //IF2 Command Rest Register
59.#define IF2CMDMSK 0x21 //IF2 Command Mask Register
60.#define IF2MSK1 0x22 //IF2 Mask1 Register
61.#define IF2MSK2 0x23 //IF2 Mask2 Register
62.#define IF2ARB1 0x24 //IF2 Arbitration 1 Register
63.#define IF2ARB2 0x25 //IF2 Arbitration 2 Register
64.#define IF2MSGC 0x26 //IF2 Message Control Register
65.#define IF2DATA1 0x27 //IF2 Data A1 Register
66.#define IF2DATA2 0x28 //IF2 Data A2 Register
67.#define IF2DATB1 0x29 //IF2 Data B1 Register
68.#define IF2DATB2 0x2A //IF2 Data B2 Register
69.////////////////////////////////////////////////////////////////////////////////
70.//Message Handler Registers
71.////////////////////////////////////////////////////////////////////////////////
72.#define TRANSREQ1 0x40 //Transmission Rest1 Register
73.#define TRANSREQ2 0x41 //Transmission Rest2 Register
74.
75.#define NEWDAT1 0x48 //New Data 1 Register
76.#define NEWDAT2 0x49 //New Data 2 Register
77.
78.#define INTPEND1 0x50 //Interrupt Pending 1 Register
79.#define INTPEND2 0x51 //Interrupt Pending 2 Register
80.
81.#define MSGVAL1 0x58 //Message Valid 1 Register
82.#define MSGVAL2 0x59 //Message Valid 2 Register
83.
84.////////////////////////////////////////////////////////////////////////////////
85.//Global Variables
86.////////////////////////////////////////////////////////////////////////////////
87.char MsgNum;
88.char status;
89.int i;
90.int MOTwoIndex = 0;
91.int MOOneIndex = 0;
92.int StatusCopy;
93.int RXbuffer [4];
94.int TXbuffer [8];
95.int MsgIntNum;
96.int Temperature;
97.sbit BUTTON = P3^7;
98.sbit LED = P1^6;
99.sfr16 CAN0DAT = 0xD8;
100.
101.
102.
103.////////////////////////////////////////////////////////////////////////////////
104.// Function PROTOTYPES
105.////////////////////////////////////////////////////////////////////////////////
106.
107.// Initialize Message Object
108.void clear_msg_objects (void);
109.void init_msg_object_TX (char MsgNum);
110.void init_msg_object_RX (char MsgNum);
111.void start_CAN (void);
112.void transmit_turn_LED_ON (char MsgNum);
113.void transmit_turn_LED_OFF (char MsgNum);
114.void receive_data (char MsgNum);
115.void external_osc (void);
116.void config_IO (void);
117.void flash_LED (void);
118.void test_reg_write (char test);
119.void stop_CAN (void);
120.
121.
122.////////////////////////////////////////////////////////////////////////////////
123.// MAIN Routine
124.////////////////////////////////////////////////////////////////////////////////
125.void main (void) {
126.
127. // disable watchdog timer
128. WDTCN = 0xde;
129. WDTCN = 0xad;
130.
131. //configure Port I/O
132. config_IO();
133.
134. // switch to external oscillator
135. external_osc();
136.
137.
138.////////////////////////////////////////////////////////////////////////////////
139.// Configure CAN communications
140.//
141.// IF1 used for procedures calles by main program
142.// IF2 used for interrupt service procedure receive_data
143.//
144.// Message Object assignments:
145.// 0x02:
Used to transmit commands to toggle its LED, arbitration number 1
146.//
147.////////////////////////////////////////////////////////////////////////////////
148.
149. // Clear CAN RAM
150. clear_msg_objects();
151.
152. // Initialize message object to transmit data
153. init_msg_object_TX (0x02);
154.
155. // Initialize message object to receive data
156. init_msg_object_RX (0x01);
157.
158. // Enable CAN interrupts in CIP-51
159. EIE2 = 0x20;
160.
161. //Function call to start CAN
162. start_CAN();
163.
164. //Global enable 8051 interrupts
165. EA = 1;
166.
167. //Loop and wait for interrupts
168. while
(1)
169. {
170. if (BUTTON == 0){
171. while (BUTTON == 0){}
172. transmit_turn_LED_OFF(0x02);}
173. else {
174. while (BUTTON == 1){}
175. transmit_turn_LED_ON(0x02);}
176. }
177.}
178.
179.
180.////////////////////////////////////////////////////////////////////////////////
181.// Set up C8051F040
182.////////////////////////////////////////////////////////////////////////////////
183.
184.// Switch to external oscillator
185.void external_osc (void)
186.{
187. int n; // local variable used in delay FOR loop.
188. SFRPAGE = CONFIG_PAGE; // switch to config page to config oscillator
189. OSCXCN = 0x77; // start external oscillator; 22.1 MHz Crystal
190. // system clock is 22.1 MHz / 2 = 11.05 MHz
191. for (n=0;n<255;n++); // delay about 1ms
192. while ((OSCXCN & 0x80) == 0); // wait for oscillator to stabilize
193. CLKSEL |= 0x01; // switch to external oscillator
194.}
195.
196.void config_IO (void)
197.{
198. SFRPAGE = CONFIG_PAGE; //Port SFR's on Configuration page
199. XBR3 = 0x80; // Configure CAN TX p