Beispiel #1
0
char CanbusClass::message_rx(unsigned char *buffer) {
		tCAN message;
	
		if (mcp2515_check_message()) {
		
			
			// Lese die Nachricht aus dem Puffern des MCP2515
			if (mcp2515_get_message(&message)) {
			//	print_can_message(&message);
			//	PRINT("\n");
				buffer[0] = message.data[0];
				buffer[1] = message.data[1];
				buffer[2] = message.data[2];
				buffer[3] = message.data[3];
				buffer[4] = message.data[4];
				buffer[5] = message.data[5];
				buffer[6] = message.data[6];
				buffer[7] = message.data[7];								
//				buffer[] = message[];
//				buffer[] = message[];
//				buffer[] = message[];
//				buffer[] = message[];																												
			}
			else {
			//	PRINT("Kann die Nachricht nicht auslesen\n\n");
			}
		}

}
Beispiel #2
0
char CanbusClass::message_rx(unsigned char *buffer) {
		tCAN message;
	
		if (mcp2515_check_message()) {
		
			
			// Read the message from the buffer of MCP2515
			if (mcp2515_get_message(&message)) {
			//	print_can_message(&message);
			//	PRINT("\n");
				buffer[0] = message.data[0];
				buffer[1] = message.data[1];
				buffer[2] = message.data[2];
				buffer[3] = message.data[3];
				buffer[4] = message.data[4];
				buffer[5] = message.data[5];
				buffer[6] = message.data[6];
				buffer[7] = message.data[7];								
//				buffer[] = message[];
//				buffer[] = message[];
//				buffer[] = message[];
//				buffer[] = message[];																												
			}
			else {
			//	PRINT("Error" Message could not be read!\n\n");
			}
		}

}
Beispiel #3
0
char CanbusClass::message_rx(tCAN *msg) {
		tCAN message;

		if (mcp2515_check_message()) {
			if (mcp2515_get_message(&message)) {
				*msg = message;
				return 1;
			}
		}
		return 0;
}
Beispiel #4
0
int CanbusClass::read_msg( int &a, int &b, int &c, int &d)
	{
		tCAN message;
		int timeout;
		while(timeout < 4000)
		{
			timeout++;
			if (mcp2515_check_message()) 
				{
					mcp2515_get_message(&message);
					int tps = (message.data[1]*256 + message.data[0]);
					a = (message.data[1]*256 + message.data[0]);
					b = (message.data[3]*256 + message.data[2]);
					c = (message.data[5]*256 + message.data[4]);
					d = (message.data[7]*256 + message.data[6]);
					return 1;
				} else {
				}
		}
	return 0;
}
Beispiel #5
0
void main()
{

  int test;
  unsigned char del;
  stCanFrame sample;
  unsigned char mpptDevice = 0x01;
  unsigned char deviceMC = 0x02;
  unsigned char mpptCanConfig[4];
  unsigned char mcCanConfig[4];

  unsigned short filterID = mcBaseAddress | motorTempMes  ;



  OpenUSART( USART_TX_INT_OFF &
  USART_RX_INT_OFF &
  USART_ASYNCH_MODE &
  USART_EIGHT_BIT &
  USART_CONT_RX &
  USART_BRGH_HIGH, 71);
  
   printf("\n\r Serial Open \n");

  OpenSPI( SPI_FOSC_16,   //Master clock 16mhz
  		MODE_00,                 
  		SMPEND);                  //Output data a end of buff

  



    selectNoDevice();

    setmppt(mpptCanConfig);

    test = mcp_init(mpptDevice, mpptCanConfig);		
	while ( test != 0)
	{
		printf("\rMPPT. Failer to init MCP 2515, reseting \n");
		test = mcp_init(mpptDevice, mpptCanConfig);
	}
	mcp2515_normal(mpptDevice);
	printf("\rMPPT. Pass init MCP 2515\n");

    delay();
       
    setmc(mcCanConfig);
    test = mcp_init(deviceMC,mcCanConfig);
    while(test !=0)
    {
		printf("\r MOTOR CONTROLLER.  Failer to init MCP 2515, reseting \n");
	    test = mcp_init(deviceMC,mcCanConfig);
    }
	mcp2515_normal(deviceMC);
	printf("\rMOTOR CONTROLLER. Pass init MCP 2515\n");
    delay();

	if (bit_is_set(0b10111111,6))
		printf("\r Error -simple bit shift fails \n");
	else	
		printf("\r simple bit operations work right \n");
		




		
    //Ending set up

    //starting main loop
	//------------------
    getMessagesThatLookLike(&(filterID), deviceMC);
	while(1)
	{	
    	mpptData myMpptData[4];
		mcData myMCData;
		stCanFrame sample;
		stCanFrame result;
		unsigned char counter = 0, temp;
		int pollCount = 10;
            
		
		//MPPT
        //---------------------
		//build the message prompts the MPPT data
		//The mppt address we want are. 
		// 0x711 , 0x713, 0x715, 0x717
		sample.id =   MPPTRequestID;
		sample.length = 0;
		sample.rtr =0;
		while(counter < 4)
		{
			sample.id =   MPPTRequestID + 1 + (counter * 2);
			if(g_debug) printf("\r the id is %x\n",MPPTRequestID + 1 + (counter * 2));
			mcp2515_send_message(&sample, 0x02,mpptDevice);		
			Delay10TCYx(0x30);
			result.id = 0;
			mcp2515_get_message(&result ,mpptDevice);			
	
			while(!(result.id == MPPTAnswerID + 1 + (counter * 2)) && pollCount ) //while we do not have the matching id, and the time has not expired keep waiting for the message
			{
		 		mcp2515_get_message(&result, mpptDevice);
		    	Delay10TCYx(0x30);
		 		pollCount--;
	
		 	}
			if(pollCount) 
			{
				parsMppt((result.data),myMpptData[counter]);
			    if(g_messageDebug) 
			    {
				    printf("\rMessage from the MPPT \n");
				    printCanMessage(result);
		 	    }

			}
			else
			{
				if(g_debug) printf("\r mppt TimeOUT \n");
			}
	
			

			counter++;
		}
		

		pollCount = 10;
        //MOTOR Controller
        //-------------------------------
        

   		Delay10TCYx(0x30);
		result.id = 0;
	   	mcp2515_get_message(&result ,deviceMC);
		while( !(result.id == 0x040b) && pollCount)
	   	{
			mcp2515_get_message(&result ,deviceMC)	;
		}
		if (result.id = 0x040b)
	 	{
		    int cool = convertToInt(result.data + 4 , 100 );
		    printf("\rThe temp is %i\n",cool);
		    printCanMessage(result);
			parsMC((result.data), result.id, myMCData);
		}
		else
		{
			printf("\r MC TimeOUT \n");
		}
				
		
	}
} 
Beispiel #6
0
char CanbusClass::ecu_req(unsigned char pid,  char *buffer) 
{
	tCAN message;
	float engine_data;
	int timeout = 0;
	char message_ok = 0;
	// Prepair message
	message.id = PID_REQUEST;
	message.header.rtr = 0;
	message.header.length = 8;
	message.data[0] = 0x02;
	message.data[1] = 0x01;
	message.data[2] = pid;
	message.data[3] = 0x00;
	message.data[4] = 0x00;
	message.data[5] = 0x00;
	message.data[6] = 0x00;
	message.data[7] = 0x00;						
	

	mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
	
	if (mcp2515_send_message(&message)) {
	}
				if (mcp2515_check_message()) 
				{

					if (mcp2515_get_message(&message)) 
					{
							switch(message.data[2])
								{   /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
									
									case ENGINE_LOAD:				// Throttle Position
									engine_data = (message.data[3]*100)/255;
									sprintf(buffer,"%d %% ",(int) engine_data);
									break;
																	
									case ENGINE_RPM:  			//   ((A*256)+B)/4    [RPM]
									engine_data =  ((message.data[3]*256) + message.data[4])/4;
									sprintf(buffer,"%d rpm ",(int) engine_data);
									break;
							
									case ENGINE_COOLANT_TEMP: 	// 	A-40			  [degree C]
									engine_data =  message.data[3] - 40;
									sprintf(buffer,"%d degC",(int) engine_data);
									break;
							
									case VEHICLE_SPEED: 		// A				  [km]
									engine_data =  message.data[3];
									sprintf(buffer,"%d km ",(int) engine_data);
									break;

									case MAF_SENSOR:   			// ((256*A)+B) / 100  [g/s]
									engine_data =  ((message.data[3]*256) + message.data[4])/100;
									sprintf(buffer,"%d g/s",(int) engine_data);
									break;

									case O2_VOLTAGE:    		// A * 0.005   (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
									engine_data = message.data[3]*0.005;
									sprintf(buffer,"%d V",(int) engine_data);
									break;
									
									case THROTTLE:				// Throttle Position
									engine_data = (message.data[3]*100)/255;
									sprintf(buffer,"%d %% ",(int) engine_data);
									break;
									
									case INTAKE_AIR_TEMP: 	// 	A-40			  [degree C]
									engine_data =  message.data[3] - 40;
									sprintf(buffer,"%d degC",(int) engine_data);
									break;
									
									case FUEL_TAKE_LEVEL:   // Fuel Tank Level
									engine_data = (message.data[3]*100)/255;
									sprintf(buffer,"%d %% ",(int) engine_data);
									break;
							
							        case OBD_STANDARD:   // OBD_STD
									sprintf(buffer,"%d  ",(int) message.data[3]);
									break;
							
								}
					}
				}

}
Beispiel #7
0
char CanbusClass::ecu_req(unsigned char pid,  char *buffer) 
{
	tCAN message;
	float engine_data;
	int timeout = 0;
	char message_ok = 0;
	// Prepair message
	message.id = PID_REQUEST;
	message.header.rtr = 0;
	message.header.length = 8;
	message.data[0] = 0x02;
	message.data[1] = 0x01;
	message.data[2] = pid;
	message.data[3] = 0x00;
	message.data[4] = 0x00;
	message.data[5] = 0x00;
	message.data[6] = 0x00;
	message.data[7] = 0x00;						
	

	mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
//		SET(LED2_HIGH);	
	if (mcp2515_send_message(&message)) {
	}
	
	while(timeout < 4000)
	{
		timeout++;
				if (mcp2515_check_message()) 
				{

					if (mcp2515_get_message(&message)) 
					{
							if((message.id == PID_REPLY) && (message.data[2] == pid))	// Check message is the reply and its the right PID
							{
								switch(message.data[2])
								{   /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
									case ENGINE_RPM:  			//   ((A*256)+B)/4    [RPM]
									engine_data =  ((message.data[3]*256) + message.data[4])/4;
									sprintf(buffer,"%d rpm ",(int) engine_data);
									break;
							
									case ENGINE_COOLANT_TEMP: 	// 	A-40			  [degree C]
									engine_data =  message.data[3] - 40;
									sprintf(buffer,"%d degC",(int) engine_data);
							
									break;
							
									case VEHICLE_SPEED: 		// A				  [km]
									engine_data =  message.data[3];
									sprintf(buffer,"%d km ",(int) engine_data);
							
									break;

									case MAF_SENSOR:   			// ((256*A)+B) / 100  [g/s]
									engine_data =  ((message.data[3]*256) + message.data[4])/100;
									sprintf(buffer,"%d g/s",(int) engine_data);
							
									break;

									case O2_VOLTAGE:    		// A * 0.005   (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
									engine_data = message.data[3]*0.005;
									sprintf(buffer,"%d v",(int) engine_data);
							
									case THROTTLE:				// Throttle Position
									engine_data = (message.data[3]*100)/255;
									sprintf(buffer,"%d %% ",(int) engine_data);
									break;
							
								}
								message_ok = 1;
							}

					}
				}
				if(message_ok == 1) return 1;
	}


 	return 0;
}
Beispiel #8
0
int main(void)
{
	// Initialisiere die UART Schnittstelle
	uart_init(UART_BAUD_SELECT(9600UL, F_CPU));
	
	// Aktiviere Interrupts
	sei();
	
	// Umleiten der Standardausgabe => ab jetzt koennen wir printf() verwenden
	stdout = &mystdout;
	
	// Versuche den MCP2515 zu initilaisieren
	if (!mcp2515_init()) {
		PRINT("Fehler: kann den MCP2515 nicht ansprechen!\n");
		for (;;);
	}
	else {
		PRINT("MCP2515 is aktiv\n\n");
	}
	
	PRINT("Erzeuge Nachricht\n");
	tCAN message;
	
	// einige Testwerte
	message.id = 0x123;
	message.header.rtr = 0;
	message.header.length = 2;
	message.data[0] = 0xab;
	message.data[1] = 0xcd;
	
	print_can_message(&message);
	
	PRINT("\nwechsle zum Loopback-Modus\n\n");
	mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), (1<<REQOP1));
	
	// Sende eine Nachricht
	if (mcp2515_send_message(&message)) {
		PRINT("Nachricht wurde in die Puffer geschrieben\n");
	}
	else {
		PRINT("Fehler: konnte die Nachricht nicht senden\n");
	}
	
	// warte ein bisschen
	_delay_ms(10);
	
	if (mcp2515_check_message()) {
		PRINT("Nachricht empfangen!\n");
		
		// read the message from the buffers
		if (mcp2515_get_message(&message)) {
			print_can_message(&message);
			PRINT("\n");
		}
		else {
			PRINT("Fehler: konnte die Nachricht nicht auslesen\n\n");
		}
	}
	else {
		PRINT("Fehler: keine Nachricht empfangen\n\n");
	}
	
	PRINT("zurueck zum normalen Modus\n\n");
	mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
	
	// wir sind ab hier wieder mit dem CAN Bus verbunden
	
	PRINT("Versuche die Nachricht per CAN zu verschicken\n");
	
	// Versuche nochmal die Nachricht zu verschicken, diesmal per CAN
	if (mcp2515_send_message(&message)) {
		PRINT("Nachricht wurde in die Puffer geschrieben\n\n");
	}
	else {
		PRINT("Fehler: konnte die Nachricht nicht senden\n\n");
	}
	
	PRINT("Warte auf den Empfang von Nachrichten\n\n");
	
	while (1) {
		// warten bis wir eine Nachricht empfangen
		if (mcp2515_check_message()) {
			PRINT("Nachricht empfangen!\n");
			
			// Lese die Nachricht aus dem Puffern des MCP2515
			if (mcp2515_get_message(&message)) {
				print_can_message(&message);
				PRINT("\n");
			}
			else {
				PRINT("Kann die Nachricht nicht auslesen\n\n");
			}
		}
	}
	
	return 0;
}