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

📄 mcp2510.c

📁 博创2410的实验代码
💻 C
📖 第 1 页 / 共 2 页
字号:

	U8 mcp_addr = (nbuffer<<4) + 0x31, ctrl;   //mcp_addr=0x61-0x71
	BOOL IsExt;

	IsExt=MCP2510_Read_Can_ID( mcp_addr, can_id);  //读取ID,返回是否扩展标志位

	ctrl=MCP2510_Read(mcp_addr-1);                             //读取接收缓冲控制寄存器
	*dlc=MCP2510_Read( mcp_addr+4);                         //读取接收缓冲器长度码
	if ((ctrl & 0x08)) {                                                      //是否远程接收请求
		*rxRTR = TRUE;
	}
	else{
		*rxRTR = FALSE;
	}
	*dlc &= DLC_MASK;                                                  //取出数据长度
	MCP2510_SRead(mcp_addr+5, data, *dlc);             //读取解收的数据数据

	return IsExt;
}


/***********************************************************\
*	写入MCP2510 发送的数据							*
*	参数: nbuffer为第几个缓冲区可以为0、1、2	*
*			ext表示是否是扩展总线					*
*			can_id为返回的ID值							*
*			rxRTR表示是否是RXRTR						*
*			data表示读取的数据						*
*			dlc表示data length code							*
*		FALSE,表示非扩展总线						*
\***********************************************************/
void MCP2510_Write_Can( U8 nbuffer, BOOL ext, U32 can_id, BOOL rxRTR, U8* data,U8 dlc )
{
	U8 mcp_addr = (nbuffer<<4) + 0x31;
	MCP2510_Swrite(mcp_addr+5, data, dlc );  // write data bytes
	MCP2510_Write_Can_ID( mcp_addr, can_id,ext);  // write CAN id
	if (rxRTR)
		dlc |= RTR_MASK;  // if RTR set bit in byte
	MCP2510_Write((mcp_addr+4), dlc);            // write the RTR and DLC
}

/*******************************************\
*	设置MCP2510 CAN总线接收ID				*
*	参数: address为MCP2510寄存器地址*
*			can_id为设置的ID值			*
*			IsExt表示是否为扩展ID	*
\*******************************************/
void MCP2510_Write_Can_ID(int address, U32 can_id, BOOL IsExt)
{
	U32 tbufdata;

	if (IsExt) {
		can_id&=0x1fffffff;	//29位
		tbufdata=can_id &0xffff;
		tbufdata<<=16;
		tbufdata|=(can_id>>(18-5)&(~0x1f));
		tbufdata |= TXB_EXIDE_M;
	}
	else{
		can_id&=0x7ff;	//11位
		tbufdata= (can_id>>3)|((can_id&0x7)<<13);//
	}
	MCP2510_Swrite(address, (unsigned char*)&tbufdata, 4);  //写入ID
}



// Setup the CAN buffers used by the application.
// We currently use only one for reception and one for transmission.
// It is possible to use several to get a simple form of queue.
//
// We setup the unit to receive all CAN messages.
// As we only have at most 4 different messages to receive, we could use the
// filters to select them for us.
//
// mcp_init() should already have been called.

/*CAN设置函数*/
void canSetup(void)
{   //U8 byte;
    // As no filters are active, all messages will be stored in RXB0 only if
    // no roll-over is active. We want to recieve all CAN messages (standard and extended)
    // (RXM<1:0> = 11).
    //SPI_mcp_write_bits(RXB0CTRL, RXB_RX_ANY, 0xFF);
    //SPI_mcp_write_bits(RXB1CTRL, RXB_RX_ANY, 0xFF);

    // But there is a bug in the chip, so we have to activate roll-over.
	MCP2510_WriteBits(RXB0CTRL, (RXB_BUKT+RXB_RX_ANY), 0x0F);//接收缓冲寄存器0设置,参见MCP2510手册P24
	MCP2510_WriteBits(RXB1CTRL, RXB_RX_ANY, 0x0F);           //接收缓冲寄存器1设置,参见MCP2510手册P24
		
}

/***********************************************************************************\
								发送数据
	参数:
		data,发送数据

	Note: 使用三个缓冲区循环发送,没有做缓冲区有效检测
\***********************************************************************************/
void canWrite(U32 id, U8 *pdata, unsigned char dlc, BOOL IsExt, BOOL rxRTR)
{
	static int ntxbuffer=0;
	MCP2510_Write_Can(ntxbuffer, IsExt, id, rxRTR, pdata, dlc);

	switch(ntxbuffer){
	case 0:
		MCP2510_transmit(TXB0CTRL);//发送缓冲区1请求报文发送,以下类似
		ntxbuffer=1;                             //设为下一个发送缓冲区号,以下类似
		break;
	case 1:
		MCP2510_transmit(TXB1CTRL);
		ntxbuffer=2;
		break;
	case 2:
		MCP2510_transmit(TXB2CTRL);
		ntxbuffer=0;
		break;
	}
}
/***********************************************************************************\
								MCP2510控制器初始化								
	参数:
		bandrate,波特率

	
\***********************************************************************************/
void init_MCP2510(CanBandRate bandrate)
{
	unsigned char i,j,a;    
	MCP2510_Reset();
	
	

//	for(i=0;i<255;i++)
//		Uart_Printf("%x\n",MCP2510_Read(i));

	MCP2510_SetBandRate(bandrate,FALSE);//设置波特率

	// Disable interrups.
	MCP2510_Write(CANINTE, NO_IE); //CAN 中断关闭,采用查询方式,参见MCP2510手册P47

	// Mark all filter bits as don't care:,由过滤寄存器进行验收
	MCP2510_Write_Can_ID(RXM0SIDH, 0x7ff,0);//设置验收滤波屏蔽寄存器高8位,P32
	MCP2510_Write_Can_ID(RXM1SIDH, 0x7ff,0);//设置验收滤波屏蔽寄存器低3位	
	
	// Anyway, set all filters to 0:
	MCP2510_Write_Can_ID(RXF0SIDH, 0x7ff, 0);  //设置过滤寄存器的高位
	MCP2510_Write_Can_ID(RXF1SIDH, 0x79A, 0);
	MCP2510_Write_Can_ID(RXF2SIDH, 0x7AB, 0);
	MCP2510_Write_Can_ID(RXF3SIDH, 0x7BC, 0);
	MCP2510_Write_Can_ID(RXF4SIDH, 0x7CD, 0);
	MCP2510_Write_Can_ID(RXF5SIDH, 0x7DE, 0);
	
   
	//Filter is applied only to Standard Frames
	MCP2510_WriteBits(RXF0SIDL,0,FILTER_MASK);  //设置过滤寄存器的低位
	MCP2510_WriteBits(RXF1SIDL,0,FILTER_MASK);
	MCP2510_WriteBits(RXF2SIDL,0,FILTER_MASK);
	MCP2510_WriteBits(RXF3SIDL,0,FILTER_MASK);
	MCP2510_WriteBits(RXF4SIDL,0,FILTER_MASK);
	MCP2510_WriteBits(RXF5SIDL,0,FILTER_MASK);
	
	     
	//Enable clock output
	//MCP2510_Write(CLKCTRL, MODE_LOOPBACK| CLKEN | CLK1);//回环模式
	MCP2510_Write(CLKCTRL, MODE_NORMAL| CLKEN | CLK1);//标准模式
    //如果不能用两台设备联机实验的话,可以选择回环模式
    //这样在超级终端中可以显示键盘的输入  

  
	// Clear, deactivate the three transmit buffers
	a = TXB0CTRL;
	for (i = 0; i < 3; i++) {
		for (j = 0; j < 14; j++) {
			MCP2510_Write(a, 0);
			a++;
	        }
       	a += 2; // We did not clear CANSTAT or CANCTRL
	}
	// and the two receive buffers.
	MCP2510_Write(RXB0CTRL, 0x0);
	MCP2510_Write(RXB1CTRL, 0x0);
		
	// The two pins RX0BF and RX1BF are used to control two LEDs; set them as outputs and set them as 00.
	MCP2510_Write(BFPCTRL, 0x3C);
	
	//Open Interrupt
	MCP2510_Write(CANINTE, RX0IE|RX1IE);
	
}


/***********************************************************************************\
								查询是否收到数据
	返回值:如果没有数据,则返回-1,
			否则,返回收到数据的缓冲区号
	Note: 如果两个缓冲区都收到数据,则返回第一个缓冲区
\***********************************************************************************/
int canPoll()
{
	if(MCP2510_ReadStatus()&RX0INT)
		return 0;
	
	if(MCP2510_ReadStatus()&RX1INT)
		return 1;

	return -1;
}
/*************************************************************************************\
读取CAN接收数据函数
arg:         n,缓冲区号;id,读取的ID号;dlc,接收数据长度;
                rxRTR,是否远程接收请求;isExt,是否扩展标志符


**************************************************************************************/
BOOL canRead(int n, U32* id, U8 *pdata,  U8*dlc, BOOL* rxRTR, BOOL *isExt)
{
	U8 byte;
	byte=MCP2510_Read(CANINTF);                                       //读取中断标志寄存器

	if(n==0){
		if(byte & RX0INT){
			*isExt=MCP2510_Read_Can(n+3, rxRTR, id, pdata, dlc); //读取数据和相关标志,下面类似
			MCP2510_WriteBits(CANINTF, ~RX0INT, RX0INT); // Clear interrupt
		}
		return FALSE;
	}
	else if(n ==1 ){
		if(byte & RX1INT){
			*isExt=MCP2510_Read_Can(n+3, rxRTR, id, pdata, dlc);
			MCP2510_WriteBits(CANINTF, ~RX1INT, RX1INT); // Clear interrupt
		}
		return FALSE;
	}


}

void CAN_Test()
{
	int i;
	U32 id;
	unsigned char dlc;
	BOOL rxRTR, isExt;
	BOOL temp;
	
	U8 data[8]={1,2,3,4,5,6,7,8};
	init_MCP2510(BandRate_125kbps);

	canSetup();
	canWrite(0x123, data, 8, FALSE, FALSE);

	memset(data,0,8);

	hudelay(100);

	while((i=canPoll())==-1);

	temp=canRead(i, &id, data, &dlc, &rxRTR, &isExt);

	Uart_Printf("\nid=%x",id);
	Uart_Printf("\ndata=%x,%x,%x,%x",data[0],data[1],data[2],data[3]);
	
//	temp=canRead(1, &id, data, &dlc, &rxRTR, &isExt);
}

⌨️ 快捷键说明

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