?? mcp2510.lst
字號:
260 {
261 U8 mcp_addr = (nbuffer<<4) + 0x31;
262 MCP2510_Swrite(mcp_addr+5, data, dlc ); // write data bytes
263 MCP2510_Write_Can_ID( mcp_addr, can_id,ext); // write CAN id
264 if (rxRTR)
265 dlc |= RTR_MASK; // if RTR set bit in byte
266 MCP2510_Write((mcp_addr+4), dlc); // write the RTR and DLC
267 }*/
268
269 /*******************************************\
270 * 設置MCP2510 CAN總線ID *
271 * 參數: address為MCP2510寄存器地址*
272 * can_id為設置的ID值 *
273 * IsExt表示是否為擴展ID *
274 \*******************************************/
275 void MCP2510_Write_Can_ID(int address, U32 can_id, BOOL IsExt)
276 {
277 1 U32 tbufdata;
278 1 unsigned char IDarry[4],i;
279 1
280 1 if (IsExt) {
281 2 can_id&=0x1fffffff; //29位
282 2 tbufdata=can_id &0xffff;
283 2 tbufdata<<=16;
284 2 tbufdata|=(can_id>>(18-5)&(~0x1f));
285 2 tbufdata |= TXB_EXIDE_M;
286 2 }
287 1 else{
288 2 can_id&=0x7ff; //11位
289 2 tbufdata= (can_id>>3)|((can_id&0x7)<<13);
290 2 }
291 1 IDarry[0]=tbufdata&0xff;
292 1 IDarry[1]=(tbufdata>>8)&0xff;
293 1 IDarry[2]=(tbufdata>>16)&0xff;
294 1 IDarry[3]=(tbufdata>>24)&0xff;
295 1 for(i=0;i<4;i++)
296 1 MCP2510_Swrite(address, IDarry[i], 1);
297 1
298 1 //MCP2510_Swrite(address, (unsigned char)tbufdata,4);
299 1 }
300
301
302
303 // Setup the CAN buffers used by the application.
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 6
304 // We currently use only one for reception and one for transmission.
305 // It is possible to use several to get a simple form of queue.
306 //
307 // We setup the unit to receive all CAN messages.
308 // As we only have at most 4 different messages to receive, we could use the
309 // filters to select them for us.
310 //
311 // mcp_init() should already have been called.
312 void canSetup(void)
313 {
314 1 // As no filters are active, all messages will be stored in RXB0 only if
315 1 // no roll-over is active. We want to recieve all CAN messages (standard and extended)
316 1 // (RXM<1:0> = 11).
317 1 //SPI_mcp_write_bits(RXB0CTRL, RXB_RX_ANY, 0xFF);
318 1 //SPI_mcp_write_bits(RXB1CTRL, RXB_RX_ANY, 0xFF);
319 1
320 1 // But there is a bug in the chip, so we have to activate roll-over.
321 1 MCP2510_WriteBits(RXB0CTRL, (RXB_BUKT+RXB_RX_ANY), 0xFF);
322 1 MCP2510_WriteBits(RXB1CTRL, RXB_RX_ANY, 0xFF);
323 1 }
324
325 /***********************************************************************************\
326 發送數據
327 參數:
328 data,發送數據
329
330 Note: 使用三個緩沖區循環發送,沒有做緩沖區有效檢測
331 \***********************************************************************************/
332 /*void canWrite(U32 id, U8 *pdata, unsigned char dlc, BOOL IsExt, BOOL rxRTR)
333 {
334 static int ntxbuffer=0;
335 MCP2510_Write_Can(ntxbuffer, IsExt, id, rxRTR, pdata, dlc);
336
337 switch(ntxbuffer){
338 case 0:
339 MCP2510_transmit(TXB10CTRL);
340 ntxbuffer=1;
341 break;
342 case 1:
343 MCP2510_transmit(TXB1CTRL);
344 ntxbuffer=2;
345 break;
346 case 2:
347 MCP2510_transmit(TXB2CTRL);
348 ntxbuffer=0;
349 break;
350 }
351 }*/
352
353 void init_MCP2510(CanBandRate bandrate)
354 {
355 1 unsigned char i,j,a;
356 1
357 1 MCP2510_Reset();
358 1
359 1 // for(i=0;i<255;i++)
360 1 // Uart_Printf("%x\n",MCP2510_Read(i));
361 1
362 1 MCP2510_SetBandRate(bandrate,FALSE);
363 1
364 1 // Disable interrups.
365 1 MCP2510_Write(CANINTE, NO_IE);
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 7
366 1
367 1 // Mark all filter bits as don't care:
368 1 MCP2510_Write_Can_ID(RXM0SIDH, 0,1);
369 1 MCP2510_Write_Can_ID(RXM1SIDH, 0,1);
370 1 // Anyway, set all filters to 0:
371 1 MCP2510_Write_Can_ID(RXF0SIDH, 0, 0);
372 1 MCP2510_Write_Can_ID(RXF1SIDH, 0, 0);
373 1 MCP2510_Write_Can_ID(RXF2SIDH, 0, 0);
374 1 MCP2510_Write_Can_ID(RXF3SIDH, 0, 0);
375 1 MCP2510_Write_Can_ID(RXF4SIDH, 0, 0);
376 1 MCP2510_Write_Can_ID(RXF5SIDH, 0, 0);
377 1
378 1
379 1 //Enable clock output MODE_NORMAL
380 1 //MCP2510_Write(CLKCTRL, MODE_LOOPBACK| CLKEN | CLK1);//回環模式
381 1 MCP2510_Write(CLKCTRL, MODE_NORMAL| CLKEN | CLK1);//標準模式
382 1 //如果不能用兩臺設備聯機實驗的話,可以選擇回環模式
383 1 //這樣在超級終端中可以顯示鍵盤的輸入
384 1
385 1
386 1
387 1
388 1
389 1
390 1 // Clear, deactivate the three transmit buffers
391 1 a = TXB0CTRL;
392 1 for (i = 0; i < 3; i++) {
393 2 for (j = 0; j < 14; j++) {
394 3 MCP2510_Write(a, 0);
395 3 a++;
396 3 }
397 2 a += 2; // We did not clear CANSTAT or CANCTRL
398 2 }
399 1 // and the two receive buffers.
400 1 MCP2510_Write(RXB0CTRL, 0);
401 1 MCP2510_Write(RXB1CTRL, 0);
402 1
403 1 // The two pins RX0BF and RX1BF are used to control two LEDs; set them as outputs and set them as 00.
404 1 //MCP2510_Write(BFPCTRL, 0x3C);
405 1
406 1 //Open Interrupt
407 1 MCP2510_Write(CANINTE, RX0IE|RX1IE);
408 1 }
409
410
411 /***********************************************************************************\
412 查詢是否收到數據
413 返回值:如果沒有數據,則返回-1,
414 否則,返回收到數據的緩沖區號
415 Note: 如果兩個緩沖區都收到數據,則返回第一個緩沖區
416 \***********************************************************************************/
417 int canPoll()
418 {
419 1 if(MCP2510_ReadStatus()&RX0INT)
420 1 return 0;
421 1
422 1 if(MCP2510_ReadStatus()&RX1INT)
423 1 return 1;
424 1
425 1 return -1;
426 1 }
427
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 8
428 void SendSIOData(unsigned char value)
429 {
430 1 char i;
431 1
432 1 for(i=0;i<8;i++)
433 1 {P1_3=0;
434 2
435 2 P1_1=value&0x80;
436 2 P1_3=1;
437 2 value=value<<1;
438 2 }
439 1 P1_3=0;
440 1 }
441
442 unsigned char ReadSIOData(void)
443 { char i,j;
444 1 unsigned char d,v=0;
445 1
446 1 for(i=0;i<8;i++)
447 1 {P1_3=0;
448 2 P1_3=1;
449 2 for(j=0;j<10;j++);
450 2 v=v+(((P1&0x04)<<5)>>i);
451 2
452 2 d=v;
453 2 }
454 1
455 1 P1_3=0;
456 1 return (d);
457 1 }
458
459 /*BOOL canRead(int n, U32* id, U8 *pdata, U8*dlc, BOOL* rxRTR, BOOL *isExt)
460 {
461 U8 byte;
462 byte=MCP2510_Read(CANINTF);
463
464 if(n==0){
465 if(byte & RX0INT){
466 *isExt=MCP2510_Read_Can(n+3, rxRTR, id, pdata, dlc);
467 MCP2510_WriteBits(CANINTF, ~RX0INT, RX0INT); // Clear interrupt
468 }
469 return FALSE;
470 }
471 else if(n ==1 ){
472 if(byte & RX1INT){
473 *isExt=MCP2510_Read_Can(n+4, rxRTR, id, pdata, dlc);
474 MCP2510_WriteBits(CANINTF, ~RX1INT, RX1INT); // Clear interrupt
475 }
476 return FALSE;
477 }
478
479
480 }*/
481
482 /*void CAN_Test()
483 {
484 int i;
485 U32 id;
486 unsigned char dlc;
487 BOOL rxRTR, isExt;
488 BOOL temp;
489
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 9
490 U8 data[8]={1,2,3,4,5,6,7,8};
491 init_MCP2510(BandRate_125kbps);
492
493 canSetup();
494 canWrite(0x123, data, 8, FALSE, FALSE);
495
496 memset(data,0,8);
497
498 Delay(100);
499
500 while((i=canPoll())==-1);
501
502 temp=canRead(i, &id, data, &dlc, &rxRTR, &isExt);
503
504 Uart_Printf("\nid=%x",id);
505 Uart_Printf("\ndata=%x,%x,%x,%x",data[0],data[1],data[2],data[3]);
506
507 // temp=canRead(1, &id, data, &dlc, &rxRTR, &isExt);
508
509
510 }*/
511
512
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 747 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 34
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 + -