View Single Post
Old 12-02-2009, 10:46 AM   #2
hopeman
Đệ tử 6 túi
 
hopeman's Avatar
 
Tham gia ngày: Jul 2007
Bài gửi: 146
:
Phần cứng hết sức đơn giản nên tôi sẽ ko post lên nữa, bạn cứ nối TXD và RXD của dsPIC và MCP2551 lại với nhau, còn CANH và CANL của MCP2551 thì nối vào bus CANH và CANL là xong.
Phần thiết lập cấu hình cho CAN như sau:
Code:
// Init Can Protocol	
void init_can1(void)
{
//--------------------------------------------------------------------------------------------------------------------
					//Initialization of CAN1 Module and Enabling of CAN1 Interrupts
//--------------------------------------------------------------------------------------------------------------------

 
 
 C1CTRLbits.CANCKS = 1;			// Select the CAN Master Clock . It is equal to Fcy here. 
								// equal to Fcy.(Fcy=30Mhz)

 C1CFG1bits.SJW=00;				//Synchronized jump width time is 1 x TQ when SJW is equal to 00
 
 C1CFG1bits.BRP = BRP_VAL;		//((FCY/(2*NTQ*BITRATE))-1) 	

 C1CFG2 = 0x03F5;               // SEG1PH=6Tq, SEG2PH=3Tq, PRSEG=5Tq 
                                // Sample 3 times
                                // Each bit time is 15Tq

///Interrupt Section of CAN Peripheral

 C1INTF = 0;					//Reset all The CAN Interrupts 
 IFS1bits.C1IF = 0;  			//Reset the Interrupt Flag status register
 C1INTE = 0x00FF;               //Enable all CAN interrupt sources
 IEC1bits.C1IE = 1;				//Enable the CAN1 Interrupt 
//-----------------------------------------------------------------------------------------------------------------------
						// Configure Receive registers, Filters and Masks
//-----------------------------------------------------------------------------------------------------------------------

 // We are initializing the Receive Buffer 0 and Receive Buffer 1 for CAN1 

 
 C1RX0CON = C1RX1CON = 0x0000; 	// Receive Buffer1 and 0 Status
 														//and Control Register for CAN1 

 
 // Acceptance Mask Register0SID and Register1SID associated with Recieve Buffer0
 // and Receive Buffer1 for CAN1 
 C1RXM0SID  = C1RXM1SID  = 0x1FFD; 
 
 // Acceptance Mask Register0EIDH and Register1EIDH associated with Recieve Buffer0
 // and Receive Buffer1 for CAN1
 C1RXM0EIDH = C1RXM1EIDH = 0x0FFF; 

 // Acceptance Mask Register0EIDL and Register1EIDL associated with Recieve Buffer0 
 //and Receive Buffer1 for CAN1 and CAN2
 C1RXM0EIDL = C1RXM1EIDL = 0xFC00; 

 
//Initializing of Acceptance Filter n Standard Identifier for CAN1

 C1RXF0SID 	= 0x0AA8;	//CAN1 Receive Acceptance Filter0 SID 		
 C1RXF2SID 	= 0x1555;   //CAN1 Receive Acceptance Filter2 SID
 C1RXF2EIDH = 0x0004;   //CAN1 Receive Acceptace  Filter2 Extended Identifier high byte
 C1RXF2EIDL = 0x8C00;	//CAN1 Receive Acceptance Filter2 Extended identifier low byte
 
 //-----------------------------------------------------------------------------------------------------------------------
						// Configure Transmit Registers Buffer 0 and Transmit Buffer 1
//-----------------------------------------------------------------------------------------------------------------------
  
 C1TX0CON = 0x0003;     // High priority
 C1TX0SID = 0x50A8;     // SID 
 C1TX0EID = 0x0000;     // EID 
 C1TX0DLC = 0x01C0;		//Select the Data word Length for CAN1 Transmit Buffer0 which is 8 byte  
 
 
 
 C1TX1CON = 0x0002;             // High Intermediate priority
 C1TX1SID = 0xA855;             // SID  
 C1TX1EID = 0x0004;             // EID                   
 C1TX1DLC = 0x8DA0;				//Select the Data word Length for CAN1 Transmit Buffer1 which
								// is 4 byte

 C1CTRLbits.REQOP = 0; // nomal mode

 while(C1CTRLbits.OPMODE != 0);//Wait for CAN1 mode change

 
 //Enable transmission

 C1TX0CONbits.TXREQ = 1;        
 C1TX1CONbits.TXREQ = 1;
 C1CTRLbits.CANCAP=1;
}
để hiểu rõ hơn mong các bạn tham khảo lại trong dsPIC30F
Family Reference Manual phần các thanh ghi của CAN, nếu có gì chưa rõ, xin cứ đặt câu hỏi trực tiếp tại đây, ta sẽ cùng trao đổi
__________________
mạch nạp - kit phát triển - linh kiện điện tử www.PNLabvn.com
hopeman vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn