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

📄 can1.c

📁 充电车的控制板程序
💻 C
字号:
#include <iom128v.h>
#include <macros.h>
#include <can1.h>

#define TRE1 4
#define SET_485_TX() PORTD &= ~(1<<TRE1)
#define SET_485_RX() PORTD |= (1<<TRE1)

#define TRE0 5
#define SET_485_TX0() PORTD &= ~(1<<TRE0)
#define SET_485_RX0() PORTD |= (1<<TRE0)

extern unsigned char tx_en1;
extern unsigned char tx_en;
extern unsigned char tx0_buf[14];
extern unsigned char rx0_buf[14];

extern unsigned char CAN0_ADDR;
extern unsigned char AIM_ADDR;
//extern unsigned char tx1_period_en;

unsigned char	bat_sent;


//----------------------
void can1_handle (void)
{
	unsigned char i;
	
		AIM_ADDR=   0x01;
	 CAN0_ADDR = 0x80;
/*	
	do{
	}while(!tx_en1);
	tx_en1=0;
	
	for(i=0;i<14;i++)
	{
		tx1_buf[i]=rx1_buf[i];
	}
	SET_485_TX();
	UCSR1B=(1<<TXEN1)|(1<<UDRIE1);
*/	
	
	switch (rx1_buf[2])
	{
		case 6://ok
			{
				Vbat_single_charge_lmt=((int)rx1_buf[7]<<8)|rx1_buf[6];//单体电池最高允许充电电压			
				Ibat_single_charge_lmt=((int)rx1_buf[9]<<8)|rx1_buf[8];//单体电池最高允许充电电流
				SOC=rx1_buf[10];
				Vbat_charge_lmt=((int)rx1_buf[12]<<8)|rx1_buf[11];//允许最高充电端电压			
				Tbat_charge_lmt=rx1_buf[13];//最高允许温度	
				
				bat_sent |= 0x01;					
			};break;
		case 7:
			{
				  car_num=((int)rx1_buf[7]<<8)|rx1_buf[6];//车号
					Bat_serial_num=(((int)rx1_buf[9]<<8)&0x0F00)|rx1_buf[8];//电池模块串联数
					Bat_parallel_num=rx1_buf[9]>>4;//电池模块并联数
					Wreal=rx1_buf[10];//实际能量
					Wrating=rx1_buf[11];//额定能量
					//状态标志1
					//状态标志2
					
						bat_sent |= 0x02;	

			};break;
		case 8://ok
			{
				  Vbat_single_charge_max=(((int)rx1_buf[7]<<8)&0x0F00)|rx1_buf[6];//电池单体电压??是否是最高?
					Box_num=rx1_buf[7]>>4;//箱号
					Vbat_max_num=rx1_buf[8];//最高单体电压电池编号
					Tbat_charge_max=rx1_buf[9];//电池最高温度
					Tbat_max_num=rx1_buf[10];//最高温度检测点编号

						bat_sent |= 0x04;	
			};break;
		case 9://ok
			{
				Ibat_charge=((int)rx1_buf[7]<<8)|rx1_buf[6];
				Vbat_charge=((int)rx1_buf[9]<<8)|rx1_buf[8];
					bat_sent |= 0x08;					
			};break;
		
		case 39:
	    {
	    	
					DT.year  =(rx1_buf[6]>>4)*10+(rx1_buf[6]&0x0F);//
					DT.month =(rx1_buf[7]>>4)*10+(rx1_buf[7]&0x0F);
					DT.day   =(rx1_buf[8]>>4)*10+(rx1_buf[8]&0x0F);
					DT.hour  =(rx1_buf[9]>>4)*10+(rx1_buf[9]&0x0F);
					DT.minute=(rx1_buf[10]>>4)*10+(rx1_buf[10]&0x0F);
					DT.second=(rx1_buf[11]>>4)*10+(rx1_buf[11]&0x0F);
					
					tx1_buf[0]=0xAA;
					
					tx1_buf[1]=0x18;					
					tx1_buf[2]=48;
					tx1_buf[3]=0xF4;
					tx1_buf[4]=0xE5;
					tx1_buf[5]=0x08;
					
					tx1_buf[6]=rx1_buf[4];
					tx1_buf[7]=rx1_buf[5];
					tx1_buf[8]=rx1_buf[6];
					tx1_buf[9]=rx1_buf[7];
					tx1_buf[10]=rx1_buf[8];
					tx1_buf[11]=rx1_buf[9];
					tx1_buf[12]=rx1_buf[10];
					tx1_buf[13]=rx1_buf[11];
					SET_485_TX();
					UCSR1B=(1<<TXEN1)|(1<<UDRIE1);
	    	
	    };break;
	   default:break; 		
	}
	
	//--------------------

}
//----------------------------------------
	void bat_sent_e8 (void)
	{

		do{
					}while(!tx_en);
					tx_en=0;
					tx0_buf[0]=0xAA;
					
					tx0_buf[1]=0x18;
					tx0_buf[2]=0xE8;
					tx0_buf[3]=AIM_ADDR;
					tx0_buf[4]=CAN0_ADDR;
					
					tx0_buf[5]=0x08;				
					
					tx0_buf[6]=(unsigned char)(car_num&0x00FF);
					tx0_buf[7]=(unsigned char)(car_num>>8);
					tx0_buf[8]=0x00;//电池类型
					tx0_buf[9]=(unsigned char)Bat_serial_num;
					tx0_buf[10]=(unsigned char)((Bat_serial_num>>8)&0x0F);
					tx0_buf[10]=tx0_buf[10]|(Bat_parallel_num<<4);
					tx0_buf[11]=Wrating;
					tx0_buf[12]=Wreal;
					tx0_buf[13]=SOC;
								
				SET_485_TX0();
	      UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
				do{
			}while(!tx_en);	
		}
		
		//------------
		
		void bat_sent_e9 (void)
		{	
			//-------------------------
			//uart1_can_en();
			do{
					}while(!tx_en);
					tx_en=0;
					tx0_buf[0]=0xAA;
					
					tx0_buf[1]=0x18;
					tx0_buf[2]=0xE9;
					tx0_buf[3]=AIM_ADDR;
					tx0_buf[4]=CAN0_ADDR;
					
					tx0_buf[5]=0x08;				
					
					tx0_buf[6]= (char)Vbat_single_charge_lmt;
					tx0_buf[7]= (char)(Vbat_single_charge_lmt>>8);
					tx0_buf[8]= (char)Ibat_single_charge_lmt;
					tx0_buf[9]= (char)(Ibat_single_charge_lmt>>8);
					tx0_buf[10]=(char)(Vbat_charge_lmt);				
					tx0_buf[11]=(char)(Vbat_charge_lmt>>8);			
					tx0_buf[12]=Tbat_charge_lmt;
					tx0_buf[13]=0x00;//??????????
					
				SET_485_TX0();
	      UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
				do{
			}while(!tx_en);	
		}
	//------------------
	
	
void bat_sent_ea (void)
{
		//-----------------------	
		//uart1_can_en();
				do{
					}while(!tx_en);
					tx_en=0;
					tx0_buf[0]=0xAA;
					
					tx0_buf[1]=0x18;
					tx0_buf[2]=0xEA;
					tx0_buf[3]=AIM_ADDR;
					tx0_buf[4]=CAN0_ADDR;
					
					tx0_buf[5]=0x08;				
					
					tx0_buf[6]=(unsigned char)Vbat_single_charge_max;
					tx0_buf[7]=(unsigned char)(Vbat_single_charge_max>>8);
					tx0_buf[8]=Vbat_max_num;
					tx0_buf[9]=Tbat_charge_max;
					tx0_buf[10]=Tbat_max_num;
					tx0_buf[11]=(char)Ibat_charge;
					tx0_buf[12]=(char)(Ibat_charge>>8);
					tx0_buf[13]=0x0A;
					
				SET_485_TX0();
	      UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
				do{
			}while(!tx_en);	
			
		}
			
//-------------
		void bat_sent_eb (void)
	{
	//---------------------------		
//	uart1_can_en();
	        do{
				  }while(!tx_en);
					tx_en=0;
					
					tx0_buf[0]=0xAA;
					tx0_buf[1]=0x18;
					tx0_buf[2]=0xEB;
					tx0_buf[3]=CAN0_ADDR;
					tx0_buf[4]=0xAA;
					tx0_buf[5]=0x08;			
					
					tx0_buf[6]=(unsigned char)Vbat_charge;
					tx0_buf[7]=(unsigned char)(Vbat_charge>>8);
					tx0_buf[8]=0x00;
					tx0_buf[9]=0x00;
					tx0_buf[10]=0x00;
					tx0_buf[11]=0x00;
					tx0_buf[12]=0x00;
					tx0_buf[13]=0x00;
					
					SET_485_TX0();
	        UCSR0B=(1<<TXEN0)|(1<<UDRIE0);
				  do{
			    }while(!tx_en);	
			  }
	//-------------------
	void bat_sent_handle (void)
	{
		if(bat_sent)
		{
			
			if(bat_sent&0x01)
			{
					bat_sent_e8 ();
					bat_sent&= 0xFE;
					
					
			}
			else 
			{
				if(bat_sent&0x02)
				{
						bat_sent_e9 ();
						bat_sent&= 0xFD;
				}
				else
				{
					if(bat_sent&0x04)
					{
							bat_sent_ea ();
							bat_sent&= 0xFB;						

					}
					else
					{
						if(bat_sent&0x08)
						  	bat_sent_eb ();
						  		bat_sent&= 0xF7;
					}
				}
    	}
		};
	}				

⌨️ 快捷键说明

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