CAN shield connected to an Uno. Trying to do a simple CAN/OBD-II query for engine speed. I’m not sure what I’m doing wrong. The CAN.init() comes back ok, along with all the filter commands. But my receive interrupt never triggers… ignition is on, car ('01 Civic) is running. Also, the R3 resistor on the CAN shield is still in place, does this need to be removed?
// CAN test code
#include <mcp_can.h>
#include <SPI.h>
#define CAN_BCAST 0x7df
unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
//EXTBYTS MODE PID UNUSED
unsigned char canBuf[8] = {0x02, 0x01, 0x0c, 0, 0, 0, 0, 0};
void setup()
{
pinMode(2, INPUT); // Setting pin 2 for /INT input
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.begin(9600);
Serial.println(“Init”);
if(CAN.begin(CAN_500KBPS) == CAN_OK)
Serial.print(“CAN init ok!!\r\n”);
else
Serial.print(“CAN init fail!!\r\n”);
CAN.init_Mask(0, 0, 0x7f0);
CAN.init_Mask(1, 0, 0x7f0);
CAN.init_Filt(0, 0, 0x7e0);
CAN.init_Filt(1, 0, 0x7e0);
CAN.init_Filt(2, 0, 0x7e0);
CAN.init_Filt(3, 0, 0x7e0);
CAN.init_Filt(4, 0, 0x7e0);
CAN.init_Filt(5, 0, 0x7e0);
delay(10);
}
void loop()
{
// SEND //////////////////////////////////
CAN.sendMsgBuf(0x7df, 0, 8, canBuf);
delay(100); // send data per 100ms
// END SEND //////////////////////////////
// RECEIVE ///////////////////////////////
if(Flag_Recv) // check if get data
{
Flag_Recv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println("CAN_BUS GET DATA!");
Serial.print("data len = ");
Serial.println(len);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
// END RECEIVE //////////////////////////
}
void MCP2515_ISR()
{
Flag_Recv = 1;
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/