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

📄 main.cpp

📁 周立功公司的ARM处理器
💻 CPP
字号:
//////////////////////////////////////////////////////////////
// 基于C++类库CCAN及CLUT的LPC2000 CAN演示程序
// 周立功单片机版权所有
//////////////////////////////////////////////////////////////

#include "config.h"

#include "CAN.H"	// CCAN类定义
#include "LUT.h"	// CLUT类定义

///////////////////////////////////////////////////////////////////////////////

CCAN Can1(1,32);	// 构造一个CAN通道,CAN控制器号为1,内建32帧软件接收缓冲
CCAN Can2(2,64);	// 构造一个CAN通道,CAN控制器号为2,内建64帧软件接收缓冲

///////////////////////////////////////////////////////////////////////////////

volatile UINT32 err;

void __irq CAN1TxRxINT(void)	// CAN1的发送接收VIC中断函数
{
	Can1.IntEntry();
	VICVectAddr = 0;
}

void __irq CAN2TxRxINT(void)	// CAN2的发送接收VIC中断函数
{
	Can2.IntEntry();
	VICVectAddr = 0;
}


void __irq LUTINTorOther(void)	// 表格错误或其他CAN中断入口
{
    err = CANLUTerrAd;			// debug
	Can1.IntEntry();
	Can2.IntEntry();

	VICVectAddr = 0;
}

////////////////////////////////////////////////////////////////

// LPC ARM CAN 测试程序
// 在2119/2194/2229简单测试通过

int main()
{	
	VICIntSelect = 0;				// 设置所有中断分配为IRQ中断

	// CAN1 中断入口设定
	VICVectCntl10 = 0x20 | 20;
	VICVectAddr10 = (UINT32)CAN1TxRxINT;
	VICVectCntl11 = 0x20 | 26;
	VICVectAddr11 = (UINT32)CAN1TxRxINT;
	// CAN2 中断入口设定
	VICVectCntl12 = 0x20 | 21;
	VICVectAddr12 = (UINT32)CAN2TxRxINT;
	VICVectCntl13 = 0x20 | 27;
	VICVectAddr13 = (UINT32)CAN2TxRxINT;

	// 19共享中断入口设定
	VICVectCntl14 = 0x20 | 19;
	VICVectAddr14 = (UINT32)LUTINTorOther;

	// 打开相应的VIC中断允许
	VICIntEnable = (0x01L << 19);
	VICIntEnable = ((0x01L << 20) | (0x01L << 26));
	VICIntEnable = ((0x01L << 21) | (0x01L << 27));


	CLUT::AFSetMode(1);	// bypass

	_CANBTR btr;

	btr.dwValue = 0;		// 1000K
	btr.Bits.BRP = 0;
	btr.Bits.TSEG1 = 7;
	btr.Bits.TSEG2 = 1;
	btr.Bits.SJW = 0;
	btr.Bits.SAM = 0;

	// 用波特率初始化CAN,也可以用不同的波特律进行CAN的初始化
	Can1.Initial(btr);
	Can2.Initial(btr);

	// 启动总线进入工作模式
	Can1.SoftBusEnable();
	Can2.SoftBusEnable();
	
	_CANRxBUF rxbuf;
	_CANTxBUF txbuf;

	// CAN1 CAN2 数据中转传输
	while(1)
	{
		if(TRUE == Can1.Receive(&rxbuf))
		{
			txbuf.fi.Bits.DLC=rxbuf.fs.Bits.DLC;
			txbuf.fi.Bits.FF = rxbuf.fs.Bits.FF;
			txbuf.fi.Bits.RTR=rxbuf.fs.Bits.RTR;
			
			txbuf.id.dwValue=rxbuf.id.dwValue;
			txbuf.da.dwValue=rxbuf.da.dwValue;
			txbuf.db.dwValue=rxbuf.db.dwValue;
			// 写发送缓冲并启动发送,这个demo没有任何超时机制限制
			// 真正使用时应该调用IsTransCompleted函数查询是否发送成功并需要有超时限制
			while(Can2.Transmit(&txbuf) == 0);
		}

		if(TRUE == Can2.Receive(&rxbuf))
		{
			txbuf.fi.Bits.DLC=rxbuf.fs.Bits.DLC;
			txbuf.fi.Bits.FF = rxbuf.fs.Bits.FF;
			txbuf.fi.Bits.RTR=rxbuf.fs.Bits.RTR;
			
			txbuf.id.dwValue=rxbuf.id.dwValue;
			txbuf.da.dwValue=rxbuf.da.dwValue;
			txbuf.db.dwValue=rxbuf.db.dwValue;
			// 写发送缓冲并启动发送,这个demo没有任何超时机制限制
			// 真正使用时应该调用IsTransCompleted函数查询是否发送成功并需要有超时限制
			while(Can1.Transmit(&txbuf) == 0);
		}
	}

	return 0;
}

⌨️ 快捷键说明

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