Hi all,
I'm working on a small dashboard to be connected on motorcycles CAN data bus.
The hardware is based on the Sparkfun shield (MCP2251/MCP2515), but shrunk down to a single SMD concept.
It should work in a passive way, reading the cyclic messages on the bus and writing some values on a 128x64 display.
CAN messages are sent from the motorcycle ECU every 100ms.
I am at the moment only interested on two messages IDs (there are ~50 IDs in the bus).
The initial test code is working correctly when I simulate the bus with only the messages I'm interested on, but when I connect it to the real bus, data is updated randomly with delay varying from 20 seconds to 10 minutes.
It looks like a buffer congestion, I tried applying filters but the problem is still there...
Can you maybe give me some suggestions on how to solve the problem??
Here's part of the code:
#include <SPI.h>
#include "mcp_can.h" // sparkfun library
// include the library header
// no font headers have to be included
#include <openGLCD.h>
#define SPI_CS_PIN 10
unsigned int canId = 0x00;
unsigned char len = 0;
unsigned char buf[8];
unsigned char temp;
int data1 = 0; // initializing values
int voltage = 0;
float voltagemin = 0;
float voltagemax = 0;
int temperaturemin = 0;
int temperaturemax = 0;
int pof = 0;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
// Initialize the GLCD
GLCD.Init();
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS init ok!");
}
else
{
Serial.println("CAN BUS init fail");
Serial.println("Init CAN BUS again...");
delay(100);
goto START_INIT;
}
// splash screen
splash();
CAN.init_Mask(0, 0, 0xFFC0000);
CAN.init_Mask(1, 0, 0xFFC0000);
CAN.init_Filt(0, 0, 0x8000000); //0x200
CAN.init_Filt(1, 0, 0x8140000); //0x205
}
void loop()
{
if(CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
canId = CAN.getCanId(); // get CAN ID
if(canId == 0x200){
Serial.print("get data from ID: 0x");
Serial.println(canId,HEX);
temp = buf[4];
data1 = (int)temp;
temp = buf[3];
pof = (int)temp;
}
else if(canId == 0x205){
Serial.print("get data from ID: 0x");
Serial.println(canId,HEX);
temp = buf[1];
voltage = ((float)temp)*25.6;
temp = buf[0];
voltage = voltage + (float)temp/10;
temp = buf[3];
temperaturemin = (int)temp;
temp = buf[4];
temperaturemax = (int)temp;
temp = buf[5];
voltagemin = ((float)temp)/50;
temp = buf[6];
voltagemax = ((float)temp)/50;
}
}
GLCD.SelectFont(CalBlk36);
GLCD.GotoXY(32,10);
GLCD.print(data1);
}