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

📄 main.c

📁 一个CAN总线应用的sample ,采用LPC2119处理器.
💻 C
📖 第 1 页 / 共 2 页
字号:
					CanSendFlag =1;         //检验一下报尾,使能CAN发送
					HC595_SendDat(DISP_TAB[5]);
				}
				UartStatus=0;
			}
			break;
		case	7://	F	毫米波信道机
			break;
			
		case	8://	G		GPS定位设备
			SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			if((UINT8)uU0RBR.Word == 'P' )
				UartStatus=12;
			else
				UartStatus=0;
			break;

		case	12://	GP		GPS定位设备
			SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			if((UINT8)uU0RBR.Word == 'G' )
				UartStatus=13;
			else
				UartStatus=0;
			break;
		case	13://	GPG		GPS定位设备
			SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			if((UINT8)uU0RBR.Word == 'G' )
				UartStatus=14;
			else
				UartStatus=0;
			break;
		case	14://	GPGG	GPS定位设备
			SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			if((UINT8)uU0RBR.Word == 'A' )
				UartStatus=15;
			else
				UartStatus=0;
			break;
		case	15://	GPGGA	GPS定位设备 
			/*
			if(RcvCounter < G_LEN-1)
				SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			else
			{
				if((UINT8)uU0RBR.Word == 0x0A )
				{
					SRBuf[RcvCounter++]=0x0A;
					CanSendFlag =1;         //检验一下报尾,使能CAN发送
				}
				UartStatus=0;
			}*/
			
			SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			if((UINT8)uU0RBR.Word == 0x0A )
			{
				CanSendFlag =1;         //收到报尾,使能CAN发送
				HC595_SendDat(DISP_TAB[9]);
				UartStatus=0;
				break;
			}

			if(RcvCounter > G_LEN)		//迟迟未收到报尾,强制使能CAN发送
			{						
				SRBuf[G_LEN-2]=0x0D;
				SRBuf[G_LEN-1]=0x0A;
				RcvCounter = G_LEN;
				CanSendFlag =1;    
				UartStatus=0;
			}
			break;
			
		case	9://	H	惯性导航设备
			break;
		case	10://I	车载电源(直流)
			HC595_SendDat(DISP_TAB[8]);
			if(RcvCounter < I_LEN-1)
				SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			else
			{
				if((UINT8)uU0RBR.Word == 0x0A )
				{
					SRBuf[RcvCounter++]=0x0A;
					CanSendFlag =1;         //检验一下报尾,使能CAN发送
				}
				UartStatus=0;
			}
			break;
		case	11://J	车载电源(交流)
			HC595_SendDat(DISP_TAB[9]);
			if(RcvCounter < J_LEN-1)
				SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			else
			{
				if((UINT8)uU0RBR.Word == 0x0A )
				{
					SRBuf[RcvCounter++]=0x0A;
					CanSendFlag =1;         //检验一下报尾,使能CAN发送
				}
				UartStatus=0;
			}
			break;
		case	16://N	测试数据 begin with $N....\r\n;
			SRBuf[RcvCounter++]=(UINT8)uU0RBR.Word; 
			if((UINT8)uU0RBR.Word == 0x0A )
			{
				CanSendFlag =1;         //收到报尾,使能CAN发送
				HC595_SendDat(DISP_TAB[1]);
				UartStatus=0;
			}

			break;

		default:
			tmp = (UINT8)uU0RBR.Word; 
			UartStatus=0;
			RcvCounter = 0;
			break;
	}
	else
		tmp = (UINT8)uU0RBR.Word; //不需要的数据在这里接收
	
   VICVectAddr = 0x00;              	// 中断处理结束
}               

/*
************************************************************************************************************************
**函数原型	:  	void		InitUart0(UINT32 BPS)
**参数说明	:  	BPS		-->>	BandRate;
				LCR		-->>	U0LCR 寄存器;
				FCR		-->>	UOFCR 寄存器;
**返回	值	:	无
**说	明	:	本函数用于硬件初始化UART0
************************************************************************************************************************
*/
void		InitUart0(UINT32 BPS)
{
	PCONP |= (0x01 << 3) ;
	PINSEL0 &= ~((UINT32)0x0F); 
	PINSEL0 |= ((UINT32)0x05);
	//设定波特率
	U0LCR =0x80;
	U0DLL=Fpclk/(16*BPS)&0x000000ff;
	U0DLM=(Fpclk/(16*BPS)) >>8;
	uU0LCR.Bits.DLAB_BIT =0x00;
	//在此加入自己的初始化代码
	U0LCR =0x03;
	U0FCR =0x01;//0xC1;
	U0IER = 0x01; //允许接收中断

	/* 设置中断允许 */
	VICIntSelect = 0x00000000;           // 设置所有通道为IRQ中断
	VICVectCntl0 = 0x26;                 // UART0中断通道分配到IRQ slot 0,即优先级最高
	VICVectAddr0 = (int)IRQ_UART0;       // 设置UART0向量地址
	VICIntEnable = 0x00000040;           // 使能UART0中断


}
#endif


/*
************************************************************************************************************************
**函数原型	:  	int	main(void)
**参数说明	:  	
**返回	值	:	无
**说	明	:主循环函数
************************************************************************************************************************
*/
int main(void)
{
	UINT32	i;
	//Debug Uart0
	TargetResetInit();

   	PINSEL0 = 0x00000000;		// 设置所有管脚连接GPIO
   	PINSEL1 = 0x00000000;
   
   	DelayNS(10);					// 延时,防止JTAG调试时,芯片复位后蜂鸣器马上蜂鸣(直到重新设置PINSEL0)
   	IO0DIR = SPI_IOCON|LED_IOCON|BEEP;	// 设置SPI等控制口为输出

	BeepOff();
	
   	/* 控制蜂鸣器报警 */
	BeepOn();
	DelayNS(50);					// 延时
	BeepOff();
	HC595_SendDat(DISP_TAB[15]);	// 输出LED显示数据
	LED(15);
	DelayNS(50);	

	  
	InitUart0(9600);
	Time0Init();								// 定时器0初始化

	//初始化CAN
	for(i=0;i<CAN_MAX_NUM;i++)
	{
		InitCAN(i);	
	}
//	while(1)
//	{
//		;
//	}
	
#if 1
	memcpy(STBuf,HelloArmCan,sizeof(HelloArmCan));
	i = 1000;
	while (1)//i>0)
	{
		//HC595_SendDat(DISP_TAB[15]);	// 输出LED显示数据
		DataToCan(0,1);
		Delay(900000);
		DataToCan(1,1);
		Delay(900000);
		//HC595_SendDat(DISP_TAB[1]);	// 输出LED显示数据
		Uart0Send(sizeof(HelloArmCan),STBuf);
		Delay(9000000);
		i--;

	}
	
	while(1);
#endif

	//循环处理事件
	while(1)
	{
		//RS232数据转发CAN
		if(0 != CanSendFlag)
		{
#if 1			
			switch(DeviceID)  //this sentence should move out the loop, to improve speed.
			{
				case 'A':
				case 'C':
					//if(WaitResponse)
					{
						if(SendPacket(55,SRBuf,RcvCounter) != true)
							if(SendPacket(5,SRBuf,RcvCounter) != true)
								Beep(1);
					}
					WaitResponse = 0;
					break;
				case 'E':
					if(SendPacket(55,SRBuf,RcvCounter) != true)
						if(SendPacket(5,SRBuf,RcvCounter) != true)
							Beep(1);
					break;
					
				case 'G':
					if(SendPacket(55,SRBuf,RcvCounter) != true)
						if(SendPacket(5,SRBuf,RcvCounter) != true)
							Beep(1);
					break;

				case 'I':
					//check and send
					break;
				case 'J':
					//check and send
					break;
				case 'N': //测试数据,直接发送
					if(SendPacket(55,SRBuf,RcvCounter) != true)
						if(SendPacket(5,SRBuf,RcvCounter) != true)
							Beep(1);
					break;
			}
#endif


//			if(SendPacket(54,SRBuf,RcvCounter) != true)
//				if(SendPacket(4,SRBuf,RcvCounter) != true)
//					Beep(1);
			CanSendFlag=0;
			RcvCounter=0;
		}

		//键盘按键处理
//		KeyDeal();

		//系统检测CAN_bus将挂起的CAN激活
		for(i=0;i<CAN_MAX_NUM;i++)
		{
			if(CANMOD(i).Bits.RM_BIT != 0)
			{
				CANGSR(i).Word=0;
				CANMOD(i).Bits.RM_BIT=0x00;
				CANCMR(i).Word=0x02;
			}
		}
		for(i=0;i<CAN_MAX_NUM;i++)
		{
			if(ReadCANRcvCyBuf(i,&CRBuf) == 0)
			{
				CanDataToNetApp( i );
			}
		}
	}
	return 0;
}

⌨️ 快捷键说明

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