I'm hoping to get some help with this as I am unable to get the CAN to Initialize.
I have the sparkfun Can-Bus shield: https://www.sparkfun.com/products/13262
and an Arduino Leonardo board.
I'm using the following code to try to connect my 2008 Audi S6 to the Arduino board:
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <Canbus.h>
char UserInput;
int data;
char buffer[456]; //Data will be temporarily stored to this buffer before being written to the file
//********************************Setup Loop*********************************//
void setup(){
Serial.begin(9600);
while(!Serial);
Serial.println("CAN-Bus Demo");
if(Canbus.init(CANSPEED_500)) /* Initialise MCP2515 CAN controller at the specified speed */
{
Serial.println("CAN Init ok");
} else
{
Serial.println("Can't init CAN");
}
delay(1000);
Serial.println("Please choose a menu option.");
Serial.println("1.Speed");
Serial.println("2.RPM");
Serial.println("3.Throttle");
Serial.println("4.Coolant Temperature");
Serial.println("5.O2 Voltage");
Serial.println("6.MAF Sensor");
}
//********************************Main Loop*********************************//
void loop(){
while(Serial.available()){
UserInput = Serial.read();
if (UserInput=='1'){
data=Canbus.ecu_req(VEHICLE_SPEED, buffer);
Serial.print("Vehicle Speed: ");
Serial.print(data);
Serial.println(" km/hr ");
delay(1000);
}
else if (UserInput=='2'){
data= Canbus.ecu_req(ENGINE_RPM, buffer);
Serial.print("Engine RPM: ");
Serial.print(data);
Serial.println(" rpm ");
delay(1000);
}
else if (UserInput=='3'){
data= Canbus.ecu_req(THROTTLE, buffer);
Serial.print("Throttle: ");
Serial.print(data);
Serial.println(" %% ");
delay(1000);
}
else if (UserInput=='4'){
data =Canbus.ecu_req(ENGINE_COOLANT_TEMP, buffer);
Serial.print("Engine Coolant Temp: ");
Serial.print(data);
Serial.println(" degC");
delay(1000);
}
else if (UserInput=='5'){
data=Canbus.ecu_req(O2_VOLTAGE, buffer);
Serial.print("O2 Voltage: ");
Serial.print(data);
Serial.println(" V");
delay(1000);
}
else if (UserInput=='6'){
data=Canbus.ecu_req(MAF_SENSOR, buffer);
Serial.print("MAF Sensor: ");
Serial.print(data);
Serial.println(" g/s");
delay(1000);
}
else
{
Serial.println(UserInput);
Serial.println("Not a valid input.");
Serial.println("Please enter a valid option.");
}
}
}
Overall, I just can't get the CAN to initialize. I've looked online and just can't seem to find a solid solution. I've tried other libraries as well to no avail.
Please assist!
-
Leonardo has SPI pins on the ICSP header. Your shield expects SPI pins on pins 11,12 and 13. Maybe this will help: seeedstudio.com/wiki/…gre_gor– gre_gor2015年11月06日 01:45:20 +00:00Commented Nov 6, 2015 at 1:45
-
I got that far; The strange part is that even if I bring the SPI pins to pins 11,12, and 13 it still doesn't work.. are there more pins I need bring over?calcazar– calcazar2015年11月07日 14:01:19 +00:00Commented Nov 7, 2015 at 14:01
2 Answers 2
Had this problem myself. Found this fixed it
Change the following line...
_delay_us(10);
To...
_delay_us(50);
In the MCP2515.c
file in the library
Increasing that delay in the MCP2515.c file also fixed this issue for me. I am running the sparkfun Can-Bus shield on an Arduino pro-mini, and here is my serial monitor output before the change, and after:
12:56:38.580 -> CAN Read - Testing receival of CAN Bus message 12:56:39.556 -> Can't init CAN 13:01:40.992 -> CAN Read - Testing receival of CAN Bus message 13:01:42.016 -> CAN Init ok