Hello
I am working in a project where I am using Arduino UNO + MCP2515 to receive CAN messages that are coming from CAN high & CAN low of a sensor. CAN h & CAN L of the sensor are connected to MCP2515.
It's probably something simple, but I can't get a CAN bus filter working for CAN messages and even not receiving any CAN msg but 0 . The code I have written is compiling successfully. Problem is the CAN data received in the MCP2515 is not showing on the LCD or on Serial Monitor. Messages shown on Serial monitor : CAN check Not ok . Here is the code:
[code]
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <mcp2515.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int y;
struct can_frame canMsg;
MCP2515 mcp2515(10); //SPI chip select (CS) pin 10
void setup() {
// lcd.init(); //initialize I2C LCD
lcd.clear();
lcd.backlight();
//delay(1000);
SPI.begin();
Serial.begin(115200); //begins baud rate at this rate
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
//mcp2515.setBitrate(MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("------- CAN Read ----------");
Serial.println("ID DLC DATA");
}
void loop() {
Serial.println("can check");
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)// to receive data
{
Serial.println("------- CAN Read ----------");
Serial.print(canMsg.can_id); // print ID
Serial.print(" ");
Serial.println("ID DLC DATA");
Serial.print(canMsg.can_dlc); // print DLC
Serial.print(" ");
if(canMsg.can_id==0x18FF02)
{
for(int i=0; i<canMsg.can_dlc; i++) //print the data for id 18ff02
{
//Serial.print(canMsg.data[i],Hex);
Serial.print(canMsg.data[6]);
y=canMsg.data[6];
}
}
// for (int i = 0; i<canMsg.can_dlc; i++)
// { // print the data
// Serial.print(canMsg.data[i],HEX);
// Serial.print(" ");
// }
Serial.println();
lcd.setCursor(1,0);
lcd.print("CAN received...");
lcd.setCursor(2,1);
lcd.print(y);
lcd.setCursor(2,2);
Serial.print(canMsg.can_id);
delay(200);
}
else
Serial.println("not ok");
}
[/code]
Please help me to get these CAN messages of the sensor on screen as well as serial monitor and filtering out the particular CAN ID from all messages.