Hi every body,
i have a test with Arduino Nano v3 with MCP 2515 CAN Interface & OBD2.
i have to connect Arduino Nano v3 with MCP 2515 CAN and connect MCP 2515 CAN Interface with OBD2 (on my car is Chevrolet Captiva 2008) by H-CAN to Pin 1, L-CAN to Pin4.
MCP2515 module :
Wiring Diagram:
and i use library MCP-can-lib-master : https://github.com/coryjfowler/MCP_CAN_lib
My code:
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
if (CAN0.begin(MCP_ANY, CAN_33K3BPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else {
while (true) {
Serial.println("Error Initializing MCP2515...");
delay(1000);
}
}
CAN0.setMode(MCP_NORMAL);
pinMode(CAN0_INT, INPUT);
Serial.println("MCP2515 Library Receive Example...");
}
void loop()
{
if (!digitalRead(CAN0_INT))
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if ((rxId & 0x80000000) == 0x80000000)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if ((rxId & 0x40000000) == 0x40000000) {
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for (byte i = 0; i < len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
}
if(CAN0.checkError() == CAN_CTRLERROR){
Serial.print("Error register value: ");
byte tempErr = CAN0.getError() & MCP_EFLG_ERRORMASK; // We are only interested in errors, not warnings.
Serial.println(tempErr, BIN);
Serial.print("Transmit error counter register value: ");
tempErr = CAN0.errorCountTX();
Serial.println(tempErr, DEC);
Serial.print("Receive error counter register value: ");
tempErr = CAN0.errorCountRX();
Serial.println(tempErr, DEC);
}
}
=============
And i have a problem when run, MCP2515 Initialized Successfully, but allways return length = 0 with ID = 0x000.
and with error check, serial return :
Error register value: 1000
Transmit error counter register value:
Receive error counter register value: 128.
=> please help me what's wrong?
