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

📄 can_下位机.c

📁 很完整的CAN总线项目应用例子
💻 C
📖 第 1 页 / 共 2 页
字号:
	  PutString(0,6,str1);
	  PutString(0,7,str2);
	  //PutString(18,6,str3);
	  PutString(18,6,str4);
/////////////////////显示字库///////////字符输入////////////////////////////待完善
/*	  for(m=0x10,n=0;m<0x1a;m++,n++)
	     PutChar(n,6,m);             //0~9
	  for(m=0x41,n=10;m<91;m++,n++)
	     PutChar(n,6,m);             //a~z
	  for(m=0x21,n=5;m<59;m++,n++)
	     PutChar(n,7,m);             //A~Z
*/
}

/***********************************************************
					子菜单1界面
***********************************************************/
void manu_14(void)
{	 
     LcmClear(0);
	 showword(symbol5, 0x00,0);
 	 showword(cao, 0x20,0);
	 showword(zuo,0x30,0);
	 showword(shuo_2,0x40,0);
	 showword(ming_2,0x50,0);
	 PutChar(11,4,'N'-32);
	 PutChar(12,4,'U'-32);
	 PutChar(13,4,'L'-32);
	 PutChar(14,4,'L'-32);
}


/***********************************************************
					子菜单1界面
***********************************************************/
void manu_12(void)
{	 
     LcmClear(0);
	 showword(symbol5,0x00,0);//四角星
 	 showword(dian_4, 0x10,0);
	 showword(ya_4,0x20,0);
	 showword(ce_2,0x30,0);
	 showword(liang_2,0x40,0);
	 PutString(10,1,str1);
	 
	 showword(symbol5, 0x01,0);//四角星
 	 showword(dian_4, 0x11,0);
	 showword(liu_4,0x21,0);
	 showword(ce_2,0x31,0);
	 showword(liang_2,0x41,0);
	 PutString(10,3,str2);

	// showword(symbol5, 0x02,0);//四角星
 	// showword(pin_4, 0x12,0);
	// showword(lv_4,0x22,0);
	// showword(ce_2,0x32,0);
	// showword(liang_2,0x42,0);
	// PutString(10,5,str3);

	 showword(symbol5, 0x02,0);//四角星
 	 showword(bo_4, 0x12,0);
	 showword(xing_4,0x22,0);
	 showword(ce_2,0x32,0);
	 showword(liang_2,0x42,0);
	 PutString(10,5,str4);
}

/***********************************************************
					子菜单111界面---量程选择
***********************************************************/
void manu_112(bit c)
{
	 LcmClear(0);
	 showword(symbol5, 0x00,0);
 	 showword(can_3, 0x10,0);
	 showword(shu_3,0x20,0);
	 showword(zai_3,0x30,0);
	 showword(ru_3,0x40,0);
	 showword(mo_3,0x50,0);
	 showword(shi_3,0x60,0);
	 showword(she_2,0x70,0);
	 showword(zhi_2,0x80,0);

     showword(shou_2,    0x21,0);
	 showword(dong_2,    0x31,0);
     showword(zi_2,      0x22,0);
	 showword(dong_2,    0x32,0);
	 if(c==0)
	 {
 	 showword(symbol1,   0x01,0);//四角星
 	 showword(symbol2,   0x02,0);/////////////////////////////////
 	 }
	 else
	 {
	 showword(symbol2,   0x01,0);//四角星
 	 showword(symbol1,   0x02,0);///////////////////////////////
 	}
}

/***********************************************************
					子菜单111界面---量程选择
***********************************************************/
void manu_111(bit b)
{
	 LcmClear(0);
	 showword(symbol5,  0x00,0);
 	 showword(liang_2,  0x10,0);
	 showword(cheng_2,  0x20,0);
	 showword(zi_2,     0x30,0);
	 showword(dong_2,	0x40,0);
	 showword(huan_2,	0x50,0);
	 showword(dang_2,	0x60,0);
	 showword(she_2,	0x70,0);
	 showword(zhi_2,	0x80,0);
	 showword(guan_3,    0x21,0);
	 showword(kai_3,   0x22,0);
	 if(b==0)
	 {
 	 showword(symbol1,  0x01,0);//四角星
 	 showword(symbol2,  0x02,0);//////////////////////////////
 	 }
	 else
	 {
	 showword(symbol2,  0x01,0);//四角星
	 showword(symbol1,  0x02,0);//////////////////////////////
	}
}

/***********************************************************
					子菜单1界面
***********************************************************/
void manu_11(void)
{
	 #define k  0x10
	 LcmClear(0);
	 showword(symbol5, 0x00+k,0);//四角星
 	 showword(cao, 0x10+k,0);
	 showword(zuo,0x20+k,0);
	 showword(zhe,0x30+k,0);
	 showword(bian_2,0x40+k,0);
	 showword(hao_2,0x50+k,0);
	 showword(she_2,0x60+k,0);
	 showword(zhi_2,0x70+k,0);

	 showword(symbol5, 0x01+k,0);
 	 showword(liang_2, 0x11+k,0);
	 showword(cheng_2,0x21+k,0);
	 showword(zi_2,0x31+k,0);
	 showword(dong_2,0x41+k,0);
	 showword(huan_2,0x51+k,0);
	 showword(dang_2,0x61+k,0);
	 showword(she_2,0x71+k,0);
	 showword(zhi_2,0x81+k,0);

	 showword(symbol5, 0x02+k,0);
 	 showword(can_3, 0x12+k,0);
	 showword(shu_3,0x22+k,0);
	 showword(zai_3,0x32+k,0);
	 showword(ru_3,0x42+k,0);
	 showword(mo_3,0x52+k,0);
	 showword(shi_3,0x62+k,0);
	 showword(she_2,0x72+k,0);
	 showword(zhi_2,0x82+k,0);
}

/***********************************************************
					主菜单界面
***********************************************************/
void manu_major(void)
{
	 #define x  0x30
	 LcmClear(0);
	 showword(symbol4, 0x00+x,0);
 	 showword(xi, 0x20+x,0);
	 showword(tong,0x30+x,0);
	 showword(she_2,0x40+x,0);
	 showword(zhi_2,0x50+x,0);

	 showword(symbol4, 0x01+x,0);
 	 showword(xin_2, 0x21+x,0);
	 showword(hao_2,0x31+x,0);
	 showword(bian_2,0x41+x,0);
	 showword(ma_2,0x51+x,0);

	 showword(symbol4, 0x02+x,0);
 	 showword(xin_2, 0x22+x,0);
	 showword(hao_2,0x32+x,0);
	 showword(ce_2,0x42+x,0);
	 showword(liang_2,0x52+x,0);

	 showword(symbol4, 0x03+x,0);
 	 showword(cao, 0x23+x,0);
	 showword(zuo,0x33+x,0);
	 showword(shuo_2,0x43+x,0);
	 showword(ming_2,0x53+x,0);

}


/***********************************************************
					连接显示界面
***********************************************************/
void manu_contact1(void)
{
	 LcmClear(0);
 	 manu_init(0x00);
 	 showword(lian, 0x52,0);
	 showword(jie,0x62,0);
	 showword(zhong,0x72,0);
}

/***********************************************************
					连接显示界面
***********************************************************/
void manu_contact2(void)
{	 
 	 uchar i;
	 for(i=0;i<3;i++)
	  {
	   PutChar(16+i,4,'.'-32);
	   Delay(300);
 	  }
	   PutChar(16,4,' '-32);
	   PutChar(17,4,' '-32);
	   PutChar(18,4,' '-32);
	   Delay(300);
}


/***********************************************************
					连接显示界面
**********************************************************
void manu_contact3(void)
{
	 LcmClear(0);
 	 manu_init(0x00);
 	 showword(lian, 0x52,0);
	 showword(jie,0x62,0);
	 PutChar(14,5,'O'-32);
	 PutChar(15,5,'K'-32);
	 PutChar(16,5,'!'-32);
	 //showword(cheng,0x72,0);
	// showword(gong,0x82,0);
	/// showword(symbol3, 0xa2,0);			//显示笑脸符号
}*/


/***********************************************************
					学号发送界面
***********************************************************/
void send_xuexiao(uchar dev_id,uchar num)      //sec_id代表本节点地址
{
  uchar  id[4]={0,0,0,0xf0};		//0xf0代表学号
  id[0]=dev_id;
  id[1]=num;
  send_dframe(id,xuehao1);										//只有1个学号,发送一帧数据
  if(num>1) { Delay(300);  send_dframe(id,xuehao2);}			//只有2个学号,发送一帧数据
  if(num>2) { Delay(300);  send_dframe(id,xuehao3);}			//只有3个学号,发送一帧数据
  if(num>3) { Delay(300);  send_dframe(id,xuehao4);}			//只有4个学号,发送一帧数据
  if(num>4)	{ Delay(300);  send_dframe(id,xuehao5);}			//只有5个学号,发送一帧数据
}

/***********************************************************
					学号输入界面
***********************************************************/
void manu_xuehao(void)
{
	 bit ok=0;
	 uchar key;
	 uchar n,m,t;
	 showword(cao, 0x01,0);
	 showword(zuo,0x11,0);
	 showword(zhe,0x21,0);
	 PutChar(6,3,':'-32);
	 for(n=0;n<0xa0;n+=0x10)
	   showword(line1,0x41+n,0);
	 for(m=1;m<0x03;m++)
	   for(n=0;n<0xa0;n+=0x10)
	     showword(line2,0x41+n+m,0);
     while(xuehao_num<5&&ok==0)              //最多输入5个学号
		{
		  input_xuehao(xuehao_num);          //输入第一个学号 //PutData2(6+t,5,xuehao1[i]);
 		  switch (xuehao_num)
		   {
		    case 0: for(n=0,t=0;n<6;n++,t+=2)  xuehao1[n]=xuehao[n];   break;
	  	    case 1: for(n=0,t=0;n<6;n++,t+=2)  xuehao2[n]=xuehao[n];   break;
		    case 2: for(n=0,t=0;n<6;n++,t+=2)  xuehao3[n]=xuehao[n];   break;
			case 3: for(n=0,t=0;n<6;n++,t+=2)  xuehao4[n]=xuehao[n];   break;
		    case 4: for(n=0,t=0;n<6;n++,t+=2)  xuehao5[n]=xuehao[n];   break;
	       }
		   key=key_scan();
		   while(key>10)  key=key_scan();
		   if(key==10)    ok=1;
		   else           xuehao_num++;      //保存学号个数为xuehao_num+1
       	}//while
}

/***********************************************************
					学号输入子程序
***********************************************************/
void input_xuehao(uchar n)
{	
   uchar key,num=0,t,tt;
   while(num<12)    //等待完成学号的输入   12位数
   {
	  key=key_scan();
	  if(key!=0xff&&key<10&&key>=0)
	  { 
	    xuehao[num++]=key;
	    PutData1(10+num,3+n,key);
	  } 
	  if(key==11) 
	  {
	  switch (n)
		   {
		    case 0:  
		      {
			    for(t=0;t<20;t++)
			    PutChar(8+t,3,' '-32);
	        	break;
			   }
	  	    case 1:  
		      {
			    for(t=0;t<20;t++)
			    PutChar(8+t,4,' '-32);
	          	break;
			   }
			case 2:  
		      {
			    for(t=0;t<20;t++)
			    PutChar(8+t,5,' '-32);
	         	break;
			   }
		   case 3:  
		      {
			    for(t=0;t<20;t++)
			    PutChar(8+t,6,' '-32);
	 			break;
			   }
		   case 4:  
		      {
			    for(t=0;t<20;t++)
			    PutChar(8+t,7,' '-32);
	       		break;
			   }
	       }
	   num=0;
	  }
   }

   for(t=0,tt=0;t<6;t++,tt+=2)
	 xuehao[t]=xuehao[tt]*10+xuehao[tt+1];//数据装入data中
}

/***********************************************************
					CAN中断服务程序
***********************************************************/
void int0_service()   interrupt 0 
{
   	uchar state;
	uchar xdata *int_poin;      		//SJA1000地址变量指针	
	P1_0=1;////////////////////开蜂鸣器
	EA=0;/////////////////////////////////
	int_poin=IR;                		//IR   0X0003	中断寄存器
	state=*int_poin;					//读中断寄存器清除中断位
	if((state&0x08)==0x08)      		//0000 1000 IR.3=1数据溢出错误中断
	{							
		int_poin=CMR;           		//CMR  0x0001	命令寄存器
		*int_poin=0x0c;					//0000 1100 CMR.3,CMR.2=1 功能:清除数据溢出错误和释放接收缓冲区
	} 
	else if((state&0x04)==0x04)			//0000 0100 IR.2=1 总线出错报警中断
	{
		int_poin=SR;                    //SR 0X0002	状态寄存器
		state=*int_poin;                //读总线错报类型
		if ((state&0x80)==0x80)         //1000 0000 SR.7=1 总线关闭错误
		{
			int_poin=MODE;             
			*int_poin=0x00;				//进入工作模式
			state=0x01;
			while((state&0x01)==0x01)	//确保进入工作模式
			{
				state=*int_poin;
			}
		}
	}
/***********************接收数据中断**********************************/
	else if((state&0x01)==0x01)      	//0000 0001 IR.0=1接收中断
	{			
		int_poin=CANRX;                 //CANRX	0X0010	报文接受缓冲区首址
		state=*int_poin;		
		state=state&0x0f;          		//截取数据长度,低四位为所接数据长度
	 if (state==8)                      //数据长度为8
		{
		 for(state=0;state<4;state++)
			{
				int_poin++;
				ID2[state]=*int_poin;	    //存放在ID2数组中		
			}	
			for(state=0;state<8;state++)
			{
				int_poin++;
				buf[state]=*int_poin;	    //存放在buf数组中		
			}
//---------------------------------------数据处理------------------------------------------------
		 if((buf[0]==0xff)&&(buf[1]==0xff)&&(buf[2]==0xff)&&(buf[3]==0xff))             //若为学号应答握手信号
			{
			 grid_ack=0xa0;
			}
			P1_0=0;//关蜂鸣器*/
		
//-------------------------------------数据处理---------------------------------------		
		}//if (state==8) 
		//若state=0,则为远程帧,暂不作处理
		int_poin=CMR;			 
		*int_poin=0x04;			//释放当前报文空间
	}
	else                       //发送中断
	{   
		 Delay(200);
		 P1_0=0;//关蜂鸣器       //叫2次表示发送成功
	}
	EA=1;///////////////////////////////
}


⌨️ 快捷键说明

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