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

📄 mcp2515.h

📁 2410 spi 外接2515 can转换 驱动代码
💻 H
📖 第 1 页 / 共 3 页
字号:
/*********************************MCP2515 Instruction***************************************/

#define MCP2515INSTR_RESET		0xc0		//œ«ÄÚ²¿ŒÄŽæÆ÷žŽÎ»ÎªÈ±Ê¡×ŽÌ¬£¬²¢œ«Æ÷ŒþÉ趚ΪÅäÖÃģʜ
#define MCP2515INSTR_READ		0x03		//ŽÓÖž¶šµØÖ·ÆðÊŒµÄŒÄŽæÆ÷¶ÁÈ¡ÊýŸÝ
#define MCP2515INSTR_READ_RX    0x90		//¶ÁÈ¡œÓÊÕ»º³åÆ÷ʱ£¬ÔÚ0nm0Ëù֞ʟµÄËÄžöµØÖ·ÖеÄÒ»žö·ÅÖõØÖ·ÖžÕë¿ÉÒÔŒõÇáÒ»°ã¶ÁÃüÁîµÄ¿ªÏú
#define MCP2515INSTR_WRITE		0x02		//œ«ÊýŸÝÐŽÈëÖž¶šµØÖ·ÆðÊŒµÄŒÄŽæ
#define MCP2515INSTR_WRITE_TX   0x40		//×°ÔØ·¢ËÍ»º³åÆ÷
#define MCP2515INSTR_RTS		0x80		//֞ʟ¿ØÖÆÆ÷¿ªÊŒ·¢ËÍÈÎÒ»·¢ËÍ»º³åÆ÷ÖеıšÎÄ·¢ËÍÐòÁÐ
#define MCP2515INSTR_RDSTAT		0xa0		//¿ìËÙ²éѯÃüÁ¿É¶ÁÈ¡Óйط¢Ëͺ͜ÓÊÕ¹ŠÄܵÄһЩ׎̬λ
#define MCP2515INSTR_RDRXSTAT	0xb0		//¿ìËÙ²éѯÃüÁȷ¶šÆ¥ÅäµÄÂ˲šÆ÷ºÍœÓÊÕ±šÎĵÄÀàÐÍ£š±ê׌֡¡¢À©Õ¹Ö¡ºÍ/ »òÔ¶³ÌÖ¡£©
#define MCP2515INSTR_BITMDFY	0x05		//λÐÞžÄ

/********************************** FUNCTION *************************************************/

typedef enum{
	BandRate_10kbps,
	BandRate_125kbps,
	BandRate_250kbps,
	BandRate_500kbps,
	BandRate_1Mbps
}CanBandRate;typedef struct{   unsigned int id;   unsigned char data[8];   unsigned char dlc;   int IsExt;   int rxRTR;}CanData, *PCanData;

// Start the transmission from one of the tx buffers.
/*#define MCP2510_transmit(address)		do{MCP2510_WriteBits(address, TXB_TXREQ_M, TXB_TXREQ_M);}while(0)*//**********************************************************************************************/
unsigned int uiErr = 0 , brk = 0;
U32 watch , watch1;
U8 WriteCanData1 [8]={0x00,0x00,0x00,0x00,0x55,0x55,0x55,0x55} , WriteCanData2 [8] , WriteCanData3 [8] , WriteCanData4 [8];
U8 ReadCanData1  [8] , ReadCanData2  [8] , ReadCanData3  [8] , ReadCanData4  [8];
U32 id1 , id2 , id3 , id4;
U8 pdata1 , pdata2 , pdata3 , pdata4;  
U8 dlc1 , dlc2 , dlc3 , dlc4;
int rxRTR1 , rxRTR2 , rxRTR3 , rxRTR4; 
int isExt1 , isExt2 , isExt3 , isExt4;int rwflag = 0;	//select can flag



/*****************************************CS enable CAN***************************************/
void MCP2515_RW1_Start(void)	//GPG2=0 nSS{  GPGDAT &=0xfffb;
}void MCP2515_RW1_Close(void)	//GPG2=1{  GPGDAT |=0x0004;}void MCP2515_RW2_Start(void)	//GPC6=0	{  GPCDAT &=0xffbf;
}void MCP2515_RW2_Close(void)	//GPC=1{  GPCDAT |=0x0040;}
void MCP2515_RW3_Start(void)	//GPC7=0{  GPCDAT &=0xff7f;
}void MCP2515_RW3_Close(void)	//GPC=1{  GPCDAT |=0x0080;}
/*********************************************************************************************/




/************************************* reset CAN ************************************************/
void MCP2515_Reset( void )
{    if (rwflag == 1)
	MCP2515_RW1_Start() ;    if (rwflag == 2)    MCP2515_RW2_Start() ;    if (rwflag == 3)    MCP2515_RW3_Start() ;
	
	SPI_SendByte( MCP2515INSTR_RESET );    if (rwflag == 1)
	MCP2515_RW1_Close() ;    if (rwflag == 2)
	MCP2515_RW2_Close() ;    if (rwflag == 3)
	MCP2515_RW3_Close() ;
}
/***********************************************************************************************/





/****************************************************************************
MCU write byte to MCP2515****************************************************************************/
void MCP2515_Write( U8 address, U8 value)
{    if (rwflag == 1)
	MCP2515_RW1_Start() ;
    if (rwflag == 2)
    MCP2515_RW2_Start() ;
    if (rwflag == 3)
    MCP2515_RW3_Start() ;

	SPI_SendByte(MCP2515INSTR_WRITE);
	SPI_SendByte( address );
	SPI_SendByte( value );
    if (rwflag == 1)
	MCP2515_RW1_Close() ;    if (rwflag == 2)
	MCP2515_RW2_Close() ;    if (rwflag == 3)
	MCP2515_RW3_Close() ;
}


/****************************************************************************
MCU write serial bytes to MCP2515 
****************************************************************************/
void MCP2515_Swrite( U8 address, U8 *pdata, U8 nlength)
{
	int i;
    if (rwflag == 1)
	MCP2515_RW1_Start() ;
    if (rwflag == 2)
        MCP2515_RW2_Start() ;
    if (rwflag == 3)
        MCP2515_RW3_Start() ;
       
	SPI_SendByte(MCP2515INSTR_WRITE);
	SPI_SendByte(address);
	
	for (i=0; i < nlength; i++) 
	{
		SPI_SendByte( *pdata );
		//if( MCP2510_DEBUG )    printf( "0x%x\n", (unsigned char)*pdata ) ;
		pdata++;
	}

    if (rwflag == 1)
	MCP2515_RW1_Close() ;
    if (rwflag == 2)
	MCP2515_RW2_Close() ;
    if (rwflag == 3)
	MCP2515_RW3_Close() ;
}


/****************************************************************************
MCU read byte from MCP2515****************************************************************************/
unsigned char MCP2515_Read( U8 address )
{
	unsigned char result;        

    if (rwflag == 1)
	MCP2515_RW1_Start() ;    if (rwflag == 2)    MCP2515_RW2_Start() ;    if (rwflag == 3)    MCP2515_RW3_Start() ;

	SPI_SendByte(MCP2515INSTR_READ) ;		//0x03
	SPI_SendByte( address ) ;
	result = SPI_ReadByte() ;

    if (rwflag == 1)
	MCP2515_RW1_Close() ;    if (rwflag == 2)
	MCP2515_RW2_Close() ;    if (rwflag == 3)
	MCP2515_RW3_Close() ;

	return result ;
}


/****************************************************************************
MCU read serial bytes from MCP2515 
****************************************************************************/
void MCP2515_SRead( U8 address, unsigned char* pdata, U8 nlength )
{
	int i;

    if (rwflag == 1)
	MCP2515_RW1_Start() ;    if (rwflag == 2)    MCP2515_RW2_Start() ;    if (rwflag == 3)    MCP2515_RW3_Start() ;

	SPI_SendByte(MCP2515INSTR_READ);
	SPI_SendByte( address );

	for (i=0; i<nlength; i++)	//nlength *pdata
	{
		*pdata=SPI_ReadByte();
		//if( MCP2510_DEBUG )    printf( "  0x%x\n", (unsigned char)*pdata ) ;
		pdata++;
	}    if (rwflag == 1)
	MCP2515_RW1_Close() ;    if (rwflag == 2)
	MCP2515_RW2_Close() ;    if (rwflag == 3)
	MCP2515_RW3_Close() ;
}
/****************************************************************************
MCU MODIFY BIT OF 2515****************************************************************************/
void MCP2515_WriteBits( U8 address, U8 mask, U8 data )
{
    if (rwflag == 1)
	MCP2515_RW1_Start() ;    if (rwflag == 2)    MCP2515_RW2_Start() ;    if (rwflag == 3)    MCP2515_RW3_Start() ;
        
	SPI_SendByte( MCP2515INSTR_BITMDFY );
	SPI_SendByte( address);
	SPI_SendByte( mask);
	SPI_SendByte( data);    if (rwflag == 1)
	MCP2515_RW1_Close() ;    if (rwflag == 2)
	MCP2515_RW2_Close() ;    if (rwflag == 3)
	MCP2515_RW3_Close() ;
}


/****************************************************************************
mcu read can id
return:expend?
****************************************************************************/
int MCP2515_Read_Can_ID( U8 address, U32* can_id)
{
	U32 tbufdata;//29bit in 4 bytes 
	unsigned char* p=(unsigned char*)&tbufdata; //p=(tbufdata first address) low

	MCP2515_SRead(address, p, 4);				//read 29bit id from 2515_REG RXBnSDIH,SIDL,EID8,EID0
	*can_id = (tbufdata<<3)|((tbufdata>>13)&0x7);
	*can_id &= 0x7ff;//obtain 11bit

	if ( (p[MCP2515LREG_SIDL] & RXB_IDE_SET) ==  RXB_IDE_SET )	// expend id obtain 29bit 
	{
		*can_id = (*can_id<<2) | (p[MCP2515LREG_SIDL] & 0x03);
		*can_id <<= 8;
		*can_id |= ((tbufdata>>16)&0xff);
		*can_id <<= 8;
		*can_id |= tbufdata>>24;								//can_id:sid<10:0>--eid<17:0>
		return TRUE;	//expend
	}
	return FALSE;		//standard
}   


/***********************************************************
                MCU read MCP2515 RXn_REG  						       
nbuffer	: RX buffer No.(3 or 4) 						    
rxRTR	: 	  						   
data	: RX data first address 	
dlc		: data length
return	: expend?						        
***********************************************************/
int MCP2515_Read_Can(U8 nbuffer, int* rxRTR, U32* can_id, U8* data , U8* dlc)
{

	U8 mcp_addr = (nbuffer<<4) + 0x31, ctrl;		
	int IsExt;						//expend id logo 

	IsExt=MCP2515_Read_Can_ID( mcp_addr, can_id);	//expend? return(true or false)

	ctrl=MCP2515_Read(mcp_addr-1);	//read RXBnCTRL
	*dlc=MCP2515_Read(mcp_addr+4);	//read RXBnDLC
	if ((ctrl & 0x08))				//RXRTR?
	{
		*rxRTR = TRUE;
	}
	else
	{
		*rxRTR = FALSE;
	}
	*dlc &= RXB_DLC_SET;				//obtain data length
	//MCP2510_SRead(mcp_addr+5, data, *dlc);
	MCP2515_SRead(mcp_addr+5, data, 8);	//data="rx data"

	return IsExt;
}


/****************************************************************************
                       MCU read MCP2515 RX_REG n	: 0 or 1 RX0,RX1
return:read ok?****************************************************************************/
int Can_Read(int n,U32* id, U8 *pdata,  U8*dlc, int* rxRTR, int *isExt)
{


	if(n==0)		//read RX0
	  {
			*isExt=MCP2515_Read_Can(3, rxRTR, id, pdata, dlc);
			//MCP2510_WriteBits(CANINTF, RX0INT, (U8)(~(RX0INT))); // Clear interrupt
			return TRUE ;
	  }
	else		
	  {
	    if(n==1)	//read RX1
		  {
			*isExt=MCP2515_Read_Can(4, rxRTR, id, pdata, dlc);
			//MCP2510_WriteBits(CANINTF, RX1INT, (U8)(~(RX1INT))); // Clear interrupt
			return TRUE ;
		  }
	    else	  
		return FALSE;
	  }

}


/****************************************************************************
                         2515 bandrate setting
IsBackNormal:?****************************************************************************/
void MCP2515_SetBandRate(CanBandRate bandrate, int IsBackNormal)
{
	//U8 value=0;
	//U8 ReadBackCNT = 0;

	// Bit rate calculations.
	//
	//Input clock fre=16MHz
	// In this case, we'll use a speed of 125 kbit/s, 250 kbit/s, 500 kbit/s.
	// If we set the length of the propagation segment to 7 bit time quanta,
	// and we set both the phase segments to 4 quanta each,
	// one bit will be 1+7+4+4 = 16 quanta in length.
	//
	// setting the prescaler (BRP) to 0 => 500 kbit/s.
	// setting the prescaler (BRP) to 1 => 250 kbit/s.
	// setting the prescaler (BRP) to 3 => 125 kbit/s.
	//
	// If we set the length of the propagation segment to 3 bit time quanta,
	// and we set both the phase segments to 1 quanta each,
	// one bit will be 1+3+2+2 = 8 quanta in length.
	// setting the prescaler (BRP) to 0 => 1 Mbit/s.

	// Go into configuration mode
/*?????????????*/	//MCP2510_Write(MCP2510REG_CANCTRL, MODE_CONFIG);  //只有圚配眮暡匏才胜进入该皋序 所以䞍甚再写了

	/*while( ReadBackCNT<8 )
	{
		value = ( MCP2510_Read( MCP2510REG_CANSTAT ) & 0xe0 );
		if(value == MODE_CONFIG )
		{
			//printf( "ReadBackCNT = 0x%x\n", ReadBackCNT );
			break;
		}
		ReadBackCNT++ ;
	}
	
	if( ReadBackCNT == 8 ) 			//Set mcp2510's mode failed,redo it again
	{
		//printf( "Set config mode is failed! CANCTRL = 0x%x\n", value );
		MCP2510_Reset();
		MCP2510_Write(MCP2510REG_CANCTRL, MODE_CONFIG);		//redo to set mcp2510 mode
		Delay( 150 );
		value = ( MCP2510_Read(MCP2510REG_CANCTRL) & 0xe0 );	//read back mode from CANSTAT Register
		//printf( "Set is 0x%x , Read is 0x%x\n", MODE_CONFIG, value ) ;

⌨️ 快捷键说明

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