📄 can1.lst
字号:
203 ////////////////////////////////////////////////////////////////////////////////
204
205 // Switch to external oscillator
206 void external_osc (void)
207 {
208 1 int n; // local variable used in delay FOR loop.
209 1 SFRPAGE = CONFIG_PAGE; // switch to config page to config oscillator
210 1 OSCXCN = 0x77; // start external oscillator; 22.1 MHz Crystal
211 1 // system clock is 22.1 MHz / 2 = 11.05 MHz
212 1 for (n=0;n<255;n++); // delay about 1ms
213 1 while ((OSCXCN & 0x80) == 0); // wait for oscillator to stabilize
214 1 CLKSEL |= 0x01; // switch to external oscillator
215 1 }
216
217 void config_IO (void)
218 {
219 1 SFRPAGE = CONFIG_PAGE; //Port SFR's on Configuration page
220 1 XBR3 = 0x80; // Configure CAN TX pin (CTX) as push-pull digital output
221 1 P1MDOUT |= 0x40; // Configure P1.6 as push-pull to drive LED
222 1 XBR2 = 0x40; // Enable Crossbar/low ports
223 1 }
224
225 ////////////////////////////////////////////////////////////////////////////////
226 //CAN Functions
227 ////////////////////////////////////////////////////////////////////////////////
228
229
230 //Clear Message Objects
231 void clear_msg_objects (void)
232 {
233 1 SFRPAGE = CAN0_PAGE;
234 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask Register 1
235 1 CAN0DATL = 0xFF; // Set direction to WRITE all IF registers to Msg Obj
236 1 for (i=1;i<33;i++)
237 1 {
238 2 CAN0ADR = IF1CMDRQST; // Write blank (reset) IF registers to each msg obj
239 2 CAN0DATL = i;
240 2 }
241 1 }
C51 COMPILER V7.50 CAN1 12/06/2006 11:41:26 PAGE 5
242
243 //Initialize Message Object for RX
244 void init_msg_object_RX (char MsgNum)
245 {
246 1 SFRPAGE = CAN0_PAGE;
247 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
248 1 CAN0DAT = 0x00B8; // Set to WRITE, and alter all Msg Obj except ID MASK
249 1 // and data bits
250 1 CAN0ADR = IF1ARB1; // Point to arbitration1 register
251 1 CAN0DAT = 0x0000; // Set arbitration1 ID to "0"
252 1 CAN0DAT = 0x8004; // Arb2 high byte:Set MsgVal bit, no extended ID,
253 1 // Dir = RECEIVE
254 1 CAN0DAT = 0x0480; // Msg Cntrl: set RXIE, remote frame function disabled
255 1 CAN0ADR = IF1CMDRQST; // Point to Command Request reg.
256 1 CAN0DATL = MsgNum; // Select Msg Obj passed into function parameter list
257 1 // --initiates write to Msg Obj
258 1 // 3-6 CAN clock cycles to move IF register contents to the Msg Obj in CAN RAM
259 1 }
260
261 //Initialize Message Object for TX
262 void init_msg_object_TX (char MsgNum)
263 {
264 1 SFRPAGE = CAN0_PAGE;
265 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
266 1 CAN0DAT = 0x00B2; // Set to WRITE, & alter all Msg Obj except ID MASK bits
267 1 CAN0ADR = IF1ARB1; // Point to arbitration1 register
268 1 CAN0DAT = 0x0000; // Set arbitration1 ID to highest priority
269 1 CAN0DAT = 0xA000; // Autoincrement to Arb2 high byte:
270 1 // Set MsgVal bit, no extended ID, Dir = WRITE
271 1 CAN0DAT = 0x0081; // Msg Cntrl: DLC = 1, remote frame function not enabled
272 1 CAN0ADR = IF1CMDRQST; // Point to Command Request reg.
273 1 CAN0DAT = MsgNum; // Select Msg Obj passed into function parameter list
274 1 // --initiates write to Msg Obj
275 1 // 3-6 CAN clock cycles to move IF reg contents to the Msg Obj in CAN RAM.
276 1 }
277
278 //Start CAN
279 void start_CAN (void)
280 {
281 1 /* Calculation of the CAN bit timing :
282 1
283 1 System clock f_sys = 22.1184 MHz/2 = 11.0592 MHz.
284 1 System clock period t_sys = 1/f_sys = 90.422454 ns.
285 1 CAN time quantum tq = t_sys (at BRP = 0)
286 1
287 1 Desired bit rate is 1 MBit/s, desired bit time is 1000 ns.
288 1 Actual bit time = 11 tq = 996.65ns ~ 1000 ns
289 1 Actual bit rate is 1.005381818 MBit/s = Desired bit rate+0.5381%
290 1
291 1 CAN bus length = 10 m, with 5 ns/m signal delay time.
292 1 Propagation delay time : 2*(transceiver loop delay + bus line delay) = 400 ns
293 1 (maximum loop delay between CAN nodes)
294 1
295 1 Prop_Seg = 5 tq = 452 ns ( >= 400 ns).
296 1 Sync_Seg = 1 tq
297 1
298 1 Phase_seg1 + Phase_Seg2 = (11-6) tq = 5 tq
299 1 Phase_seg1 <= Phase_Seg2, => Phase_seg1 = 2 tq and Phase_Seg2 = 3 tq
300 1 SJW = (min(Phase_Seg1, 4) tq = 2 tq
301 1
302 1 TSEG1 = (Prop_Seg + Phase_Seg1 - 1) = 6
303 1 TSEG2 = (Phase_Seg2 - 1) = 2
C51 COMPILER V7.50 CAN1 12/06/2006 11:41:26 PAGE 6
304 1 SJW_p = (SJW - 1) = 1
305 1
306 1 Bit Timing Register = BRP + SJW_p*0x0040 = TSEG1*0x0100 + TSEG2*0x1000 = 2640
307 1
308 1 Clock tolerance df :
309 1
310 1 A: df < min(Phase_Seg1, Phase_Seg2) / (2 * (13*bit_time - Phase_Seg2))
311 1 B: df < SJW / (20 * bit_time)
312 1
313 1 A: df < 2/(2*(13*11-3)) = 1/(141-3) = 1/138 = 0.7246%
314 1 B: df < 2/(20*11) = 1/110 = 0.9091%
315 1
316 1 Actual clock tolerance is 0.7246% - 0.5381% = 0.1865% (no problem for quartz)
317 1 */
318 1
319 1 SFRPAGE = CAN0_PAGE;
320 1 CAN0CN |= 0x41; // Configuration Change Enable CCE and INIT
321 1 CAN0ADR = BITREG ; // Point to Bit Timing register
322 1 CAN0DAT = 0x2640; // see above
323 1
324 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
325 1 CAN0DAT = 0x0087; // Config for TX : WRITE to CAN RAM, write data bytes,
326 1 // set TXrqst/NewDat, clr IntPnd
327 1
328 1 // RX-IF2 operation may interrupt TX-IF1 operation
329 1 CAN0ADR = IF2CMDMSK; // Point to Command Mask 2
330 1 CAN0DATL = 0x1F; // Config for RX : READ CAN RAM, read data bytes,
331 1 // clr NewDat and IntPnd
332 1 CAN0CN |= 0x06; // Global Int. Enable IE and SIE
333 1 CAN0CN &= ~0x41; // Clear CCE and INIT bits, starts CAN state machine
334 1 }
335
336 //Transmit CAN frame to turn other node's LED ON
337 void transmit_turn_LED_ON (char MsgNum)
338 {
339 1 SFRPAGE = CAN0_PAGE; // IF1 already set up for TX
340 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
341 1 CAN0DAT = 0x0087; // Config to WRITE to CAN RAM, write data bytes,
342 1 // set TXrqst/NewDat, Clr IntPnd
343 1 CAN0ADR = IF1DATA1; // Point to 1st byte of Data Field
344 1 CAN0DATL = 0x11; // Ones signals to turn LED's light ON in data A1 field
345 1 CAN0ADR = IF1CMDRQST; // Point to Command Request Reg.
346 1 CAN0DATL = MsgNum; // Move new data for TX to Msg Obj "MsgNum"
347 1 }
348
349 //Transmit CAN Frame to turn other node's LED OFF
350 void transmit_turn_LED_OFF (char MsgNum)
351 {
352 1 SFRPAGE = CAN0_PAGE; // IF1 already set up for TX
353 1 CAN0ADR = IF1DATA1; // Point to 1st byte of Data Field
354 1 CAN0DATL = 0x00; // Zero signals to turn LED's light ON in Data A1 field
355 1 CAN0ADR = IF1CMDRQST; // Point to Command Request Reg.
356 1 CAN0DATL = MsgNum; // Move new data for TX to Msg Obj "MsgNum"
357 1 }
358
359
360 // Receive Data from the IF2 buffer
361 void receive_data (char MsgNum)
362 {
363 1 char virtual_button;
364 1 SFRPAGE = CAN0_PAGE; // IF1 already set up for RX
365 1 CAN0ADR = IF2CMDRQST;// Point to Command Request Reg.
C51 COMPILER V7.50 CAN1 12/06/2006 11:41:26 PAGE 7
366 1 CAN0DATL = MsgNum; // Move new data for RX from Msg Obj "MsgNum"
367 1 // Move new data to a
368 1 CAN0ADR = IF2DATA1; // Point to 1st byte of Data Field
369 1
370 1 virtual_button = CAN0DATL;
371 1 if (virtual_button == 0x11) //Ones is signal from other node to turn LED ON
372 1 LED = 1;
373 1 else LED = 0; //Otherwise turn LED OFF (message was one's)
374 1 }
375
376 ////////////////////////////////////////////////////////////////////////////////
377 //Interrupt Service Routine
378 ////////////////////////////////////////////////////////////////////////////////
379 void ISRname (void) interrupt 19
380 {
381 1 status = CAN0STA;
382 1 if ((status&0x10) != 0)
383 1 { // RxOk is set, interrupt caused by reception
384 2 CAN0STA = (CAN0STA&0xEF)|0x07; // Reset RxOk, set LEC to NoChange
385 2 /* read message number from CAN INTREG */
386 2 receive_data (0x01); // Up to now, we have only one RX message
387 2 }
388 1 if ((status&0x08) != 0)
389 1 { // TxOk is set, interrupt caused by transmision
390 2 CAN0STA = (CAN0STA&0xF7)|0x07; // Reset TxOk, set LEC to NoChange
391 2 }
392 1 if (((status&0x07) != 0)&&((status&0x07) != 7))
393 1 { // Error interrupt, LEC changed
394 2 /* error handling ? */
395 2 CAN0STA = CAN0STA|0x07; // Set LEC to NoChange
396 2 }
397 1 }
398
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 523 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 38 1
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -