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

📄 can0.c

📁 充电车的控制板程序
💻 C
📖 第 1 页 / 共 2 页
字号:
					tx0_buf[4]=CAN0_ADDR;
					tx0_buf[5]=0x08;
					
					tx0_buf[6]=(unsigned char) period_all<<4;
					tx0_buf[6]|=(unsigned char) period;
					
								tx0_buf[7]=0xaa;
								
								for(i=8;i<14;i++)
								  tx0_buf[i]=0x00;
								  
								  SET_485_TX0();
	      UCSR0B=(1<<TXEN0)|(1<<UDRIE0); 
	      do{
					}while(!tx_en);
								
							};break;	
						default:break;	
					}
					
				};
				if(rx0_buf[2]==0x51)//请求写从站运行模式数据//4.3.1
				{
					temp[0]=rx0_buf[6]>>4;
					period_all=(int)temp[0];
					temp[1]=rx0_buf[6]&0x0F;
					period = (int)temp[1];
					
					switch (rx0_buf[7])
					{
						case 0x9F://恒流限压0xa0+0xff = 0x19f
							{
							 mode = 0;
							 
							 temp[3]=(rx0_buf[8]&0xF0)>>4;
							 temp[2]=rx0_buf[8]&0x0F;
							 temp[1]=(rx0_buf[9]&0xF0)>>4;
							 temp[0]=rx0_buf[9]&0x0F;
							 tempa=temp[3]*1000+(int)temp[2]*100+(int)temp[1]*10+temp[0];
							// Ichg
							 charge_para[2]=tempa/10;
							 
							 temp[3]=(rx0_buf[10]&0xF0)>>4;
							 temp[2]=rx0_buf[10]&0x0F;
							 temp[1]=(rx0_buf[11]&0xF0)>>4;
							 temp[0]=rx0_buf[11]&0x0F;
							 tempa = temp[3]*1000+temp[2]*100+temp[1]*10+temp[0];
							// Vlimt//charge_para[3]
							 charge_para[3]=tempa/10;
							 
							 temp[3]=(rx0_buf[12]&0xF0)>>4;
							 temp[2]=rx0_buf[12]&0x0F;
							 temp[1]=(rx0_buf[13]&0xF0)>>4;
							 temp[0]=rx0_buf[13]&0x0F;
							 tempa=temp[3]*1000+temp[2]*100+temp[1]*10+temp[0];
							// Vstp
							charge_para[4]=tempa/10;
							 
							};break;
						case 0xa0: //恒压限流0xa1+0xff=0x1a0
							{
							 mode = 1;
							 
							 temp[3]=(rx0_buf[8]&0xF0)>>4;
							 temp[2]=rx0_buf[8]&0x0F;
							 temp[1]=(rx0_buf[9]&0xF0)>>4;
							 temp[0]=rx0_buf[9]&0x0F;
							 tempa=temp[3]*1000+temp[2]*100+temp[1]*10+temp[0];							 
							// Vchg 
							 charge_para[5]=tempa/10;
							 
							 temp[3]=(rx0_buf[10]&0xF0)>>4;
							 temp[2]=rx0_buf[10]&0x0F;
							 temp[1]=(rx0_buf[11]&0xF0)>>4;
							 temp[0]=rx0_buf[11]&0x0F;
							 tempa=temp[3]*1000+temp[2]*100+temp[1]*10+temp[0];
							// Ilmt
							 charge_para[6]=tempa/10;
							 
							 
							 temp[3]=(rx0_buf[12]&0xF0)>>4;
							 temp[2]=rx0_buf[12]&0x0F;
							 temp[1]=(rx0_buf[13]&0xF0)>>4;
							 temp[0]=rx0_buf[13]&0x0F;
							 tempa=(int)(temp[3]*1000+temp[2]*100+temp[1]*10+temp[0]);
							// Istp
							 charge_para[7]=tempa/10;
							};break;
						case 0xa1://恒流定时 0xa2+0xff =0x1a1
							{
							 mode = 2;
							 
							 temp[3]=(rx0_buf[8]&0xF0)>>4;
							 temp[2]=rx0_buf[8]&0x0F;
							 temp[1]=(rx0_buf[9]&0xF0)>>4;
							 temp[0]=rx0_buf[9]&0x0F;
							 tempa=temp[3]*1000+temp[2]*100+temp[1]*10+temp[0];
							 //Ids
							 charge_para[8]=tempa/10; 
							 
							};break;
						case 0xa2://恒压定时  0xa3+0xff= 0x1a2
							{
							 mode = 3;
							 
							 temp[3]=(rx0_buf[8]&0xF0)>>4;
							 temp[2]=rx0_buf[8]&0x0F;
							 temp[1]=(rx0_buf[9]&0xF0)>>4;
							 temp[0]=rx0_buf[9]&0x0F;
							 tempa= temp[3]*1000+temp[2]*100+temp[1]*10+temp[0];
							 //Vds
							 charge_para[11]=tempa/10;
							};break;
						case 0xa9://停机  0xaa+0xff = 0x1a9
							{
							 mode=4;
							 
							};break;
						default:break; 	
					}
				};
			};break;
		case 0x04://100
			{
				if(rx0_buf[2]==0x6E)//充电机启动,开始为车辆充电
				{
          charge_init(1);
				};
				if(rx0_buf[2]==0x6F)//充电机停机,终止为车辆充电
				{
					mode=4;
				};
			};break;	
		default:break;		 
	}
}
//----------
void slave_device_sent41 (void)
{
	unsigned char temp[4];
	
		//uart0_can_en ();
		
	AIM_ADDR=   0x01;
	CAN0_ADDR = 0x80;
	
	do{
	}while(!tx_en);
	tx_en=0;
	tx0_buf[0]=0xaa;
	
	tx0_buf[1]=0x18;
	tx0_buf[2]=0x32;
	tx0_buf[3]=CAN0_ADDR;
	tx0_buf[4]=AIM_ADDR;
	
	tx0_buf[5]=0x08;
	
	switch(mode)
	{
		case 0:
			{
				tx0_buf[6]=0xa0;
			};break;
		case 1:
			{
				tx0_buf[6]=0xa1;
			};break;
		case 2:
			{
				tx0_buf[6]=0xa2;
			};break;
		case 3:
			{
				tx0_buf[6]=0xa3;
			};break;			
		case 4:
			{
				tx0_buf[6]=0xaa;
			};break;	
			default:break;
	}
	
	tx0_buf[7]=period;//N2

//	temp[4]=Vout/10000;//万位
	temp[3]=(Vout%10000)/1000;//千位
	temp[2]=(Vout%1000)/100;//百位
	temp[1]=(Vout%100)/10;//十位
	temp[0]=Vout%10;//个位
	
	tx0_buf[8]=temp[3]<<4;
	tx0_buf[8]|=temp[2];
	tx0_buf[9]=temp[1]<<4;
	tx0_buf[9]|=temp[0];
	
	temp[4]=Iout/10000;//万位
	temp[3]=(Iout%10000)/1000;//千位
	temp[2]=(Iout%1000)/100;//百位
	temp[1]=(Iout%100)/10;//十位
	temp[0]=Iout%10;//个位
	
	tx0_buf[10]=temp[3]<<4;
	tx0_buf[10]|=temp[2];
	tx0_buf[11]=temp[1]<<4;
	tx0_buf[11]|=temp[0];
	
//	tx0_buf[12]= 0 ;//本机故障代码?????
	
	if(alarm&0x0001) //bit0:交流输入过压
	 tx0_buf[12]=0x05;
	else if (alarm&0x0002)//bit1:交流输入欠压
	 tx0_buf[12]=0x04;
	else if (alarm&0x0004) //bit2:输出过压
	 tx0_buf[12]=0x07;
	else if (alarm&0x0008) //bit3:输出过流
	 tx0_buf[12]=0x06;
	
	tx0_buf[13]=1 ;
	
	SET_485_TX0();
	UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
}
//---------------------	

void slave_device_sent42 (void)
{
		unsigned char temp[4];
			
	AIM_ADDR=   0x01;
	CAN0_ADDR = 0x80;
		//-------------------------
		//	uart0_can_en ();
//	delay(1000);
		do{
	}while(!tx_en);
	tx_en=0;
	
	tx0_buf[0]=0xaa;
	
	tx0_buf[1]=0x18;
	tx0_buf[2]=0x33;
	tx0_buf[3]=CAN0_ADDR;
	tx0_buf[4]=AIM_ADDR;
	
	tx0_buf[5]=0x08;
	
	tx0_buf[6]=1;
	
//	tx0_buf[7]= 0x00 ;//累计工作时间
//	tx0_buf[8]= 0x00;
	
	temp[3]=(work_time_car%10000)/1000;//千位
	temp[2]=(work_time_car%1000)/100;//百位
	temp[1]=(work_time_car%100)/10;//十位
	temp[0]=work_time_car%10;//个位
	
	tx0_buf[7]=temp[3]<<4;
	tx0_buf[7]|=temp[2];
	tx0_buf[8]=temp[1]<<4;
	tx0_buf[8]|=temp[0];
	
	
	temp[3]=(work_time_perid%10000)/1000;//千位
	temp[2]=(work_time_perid%1000)/100;//百位
	temp[1]=(work_time_perid%100)/10;//十位
	temp[0]=work_time_perid%10;//个位
	
	tx0_buf[9]=temp[3]<<4;
	tx0_buf[9]|=temp[2];
	tx0_buf[10]=temp[1]<<4;
	tx0_buf[10]|=temp[0];
	
//	tx0_buf[9]= 0x00; //本阶段开始到当前的时间
//	tx0_buf[10]= 0x00;
	
	tx0_buf[11]=0;//从属充电机地址编号
	tx0_buf[12]=0;
	tx0_buf[13]=0;
	
	SET_485_TX0();
	UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
} 

//-----------------
void  slave_device_sent43  (void)
{
		unsigned char temp[4];
			
	AIM_ADDR=   0x01;
	CAN0_ADDR = 0x80;
	//---------------------
	//	uart0_can_en ();
	//delay(1000);
		do{
	}while(!tx_en);
	tx_en=0;
	
	tx0_buf[0]=0xaa;
	
	tx0_buf[1]=0x18;
	tx0_buf[2]=0x34;
	tx0_buf[3]=AIM_ADDR;
	tx0_buf[4]=CAN0_ADDR;
	tx0_buf[5]=0x08;
	
	tx0_buf[6]= 0x00;	//?????????  本机累计输入电量
	tx0_buf[7]= 0x00;
	tx0_buf[8]= 0x00;	
	tx0_buf[9]= 0x00; 
	
	tx0_buf[10]= 0x00;//本机累计输出电量
	tx0_buf[11]= 0x00;
	tx0_buf[12]= 0x00;
	tx0_buf[13]= 0x00;
	
	read_Qout();
//	Qtemp[4];
	tx0_buf[10]= Qtemp[3];//本机累计输出电量
	tx0_buf[11]= Qtemp[2];
	tx0_buf[12]= Qtemp[1];
	tx0_buf[13]= Qtemp[0];
	
	
	
	SET_485_TX0();
	UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
}

//-------------------
void state_sent (void)
{
	state_counter_sec++;
	if(state_counter_sec>12)
	{
		state_counter_sec=0;
		state_counter_min++;	
		

		can_sent_state_en=1;
	};
	
	if((state_counter_min==300)&can_sent_state_en)
	{
		can_sent_state_en=0;
		 slave_device_sent41();
	};
	
	if((state_counter_min==360)&can_sent_state_en)
	{
		can_sent_state_en=0;
		 slave_device_sent42();
	};
	
	if((state_counter_min==420)&can_sent_state_en)
	{
		state_counter_min=0;
		can_sent_state_en=0;
		 slave_device_sent43();
	};
	
}

⌨️ 快捷键说明

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