I've got a project to listen to CAN (RV-C using 29-bit addressing) traffic and light some LEDs accordingly.
I've got some hardware that works exactly as intended. But it's bulky.
3.3V/8MHz (running on 5V) Pro Mini and a generic MCP2515/8MHz board.
Working Code:
#include <FastLED.h>
#include <mcp_can.h>
#include <SPI.h>
#define DEBUG // Enable debug output
#define BUTTON_PIN 7 // Alarm cancel Button pin (Low Active)
#define CAN0_INT 2 // Set INT to pin
#define SPI_CS_PIN 10 // Which pin to use for SPI CS with CAN bus interface
uint8_t len = 0;
uint8_t buf[8];
uint32_t canId = 0x000;
char outbuf[8];
char outbuf2[32];
char hexDGN[6];
unsigned int bin2int(char * digits);
char* int2bin(uint32_t d);
MCP_CAN CAN0(SPI_CS_PIN); // Set CS pin
typedef struct {
char dgnhi[10];
char dgnlo[9];
#ifdef DEBUG // These are only needed when sending serial data out. No sense wasting memory
char prio[4];
char srcAD[9];
#endif
} packetmeta;
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#endif
START_INIT:
#ifdef DEBUG
Serial.println(F("CAN Begin..."));
#endif
if (CAN_OK == CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ)){
CAN0.setMode(MCP_NORMAL);
CAN0.init_Mask(0, 1, 0);
CAN0.init_Mask(1, 1, 0);
for (int i = 0; i < 6; i++) {
CAN0.init_Filt(i, 1, 0);
}
#ifdef DEBUG
Serial.println(F("CAN BUS Shield init ok!"));
#endif
} // Setup
void loop() {
if (CAN_MSGAVAIL == CAN0.checkReceive()) { // Read data from the MCP, serial comms heavy
CAN0.readMsgBuf(&canId, &len, buf);
char *binCanID=int2bin(canId);
packetmeta p;
memcpy(p.dgnhi, &binCanID[4], 9);
memcpy(p.dgnlo, &binCanID[13],8);
#ifdef DEBUG // These are only needed when sending serial data out. No sense wasting memory
memcpy(p.srcAD, &binCanID[21],8);
memcpy(p.prio, &binCanID[0], 3);
#endif
// Terminate all the char strings
p.dgnhi[9]='\0';
p.dgnlo[8]='\0';
#ifdef DEBUG // These are only needed when sending serial data out. No sense wasting memory
p.srcAD[8]='\0';
p.prio[3]='\0';
#endif
free(binCanID);
sprintf(hexDGN, "%03X%02X", // Construct full hexDGN
bin2int(p.dgnhi),
bin2int(p.dgnlo));
#ifdef DEBUG
sprintf(outbuf2, "%X,%03X%02X,%02X,%d,",
bin2int(p.prio),
bin2int(p.dgnhi),
bin2int(p.dgnlo),
bin2int(p.srcAD),
len);
//Serial.print(millis()); Serial.print(",");
Serial.println(canId, HEX);
Serial.print(outbuf2);
for (int i = 0; i < len; i++) {
sprintf(outbuf, "%02X", buf[i]);
Serial.print(outbuf);
}
Serial.println();
#endif
<Snipped for length>
But when I try to port this to another 3.3V/8MHz (running on 5V) Pro Mini with an MCP25625/16MHz
Found here:https://www.tindie.com/products/geraldjust/mcp25625-mini-can-bus-shield-mcp2515-compatible/
I get nothing on the serial output after the configuration messages. The Only difference is the MCP_8MHZ vs MCP_16MHZ.
Has this code been tested on the MCP25625?
#include <FastLED.h>
#include <mcp_can.h>
#include <SPI.h>
#define DEBUG // Enable debug output
#define BUTTON_PIN 7 // Alarm cancel Button pin (Low Active)
#define CAN0_INT 2 // Set INT to pin
#define SPI_CS_PIN 10 // Which pin to use for SPI CS with CAN bus interface
uint8_t len = 0;
uint8_t buf[8];
uint32_t canId = 0x000;
char outbuf[8];
char outbuf2[32];
char hexDGN[6];
unsigned int bin2int(char * digits);
char* int2bin(uint32_t d);
MCP_CAN CAN0(SPI_CS_PIN); // Set CS pin
typedef struct {
char dgnhi[10];
char dgnlo[9];
#ifdef DEBUG // These are only needed when sending serial data out. No sense wasting memory
char prio[4];
char srcAD[9];
#endif
} packetmeta;
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#endif
START_INIT:
#ifdef DEBUG
Serial.println(F("CAN Begin..."));
#endif
if (CAN_OK == CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_16MHZ)){
CAN0.setMode(MCP_NORMAL);
CAN0.init_Mask(0, 1, 0);
CAN0.init_Mask(1, 1, 0);
for (int i = 0; i < 6; i++) {
CAN0.init_Filt(i, 1, 0);
}
#ifdef DEBUG
Serial.println(F("CAN BUS Shield init ok!"));
#endif
} // Setup
void loop() {
if (CAN_MSGAVAIL == CAN0.checkReceive()) { // Read data from the MCP, serial comms heavy
CAN0.readMsgBuf(&canId, &len, buf);
char *binCanID=int2bin(canId);
packetmeta p;
memcpy(p.dgnhi, &binCanID[4], 9);
memcpy(p.dgnlo, &binCanID[13],8);
#ifdef DEBUG // These are only needed when sending serial data out. No sense wasting memory
memcpy(p.srcAD, &binCanID[21],8);
memcpy(p.prio, &binCanID[0], 3);
#endif
// Terminate all the char strings
p.dgnhi[9]='\0';
p.dgnlo[8]='\0';
#ifdef DEBUG // These are only needed when sending serial data out. No sense wasting memory
p.srcAD[8]='\0';
p.prio[3]='\0';
#endif
free(binCanID);
sprintf(hexDGN, "%03X%02X", // Construct full hexDGN
bin2int(p.dgnhi),
bin2int(p.dgnlo));
#ifdef DEBUG
sprintf(outbuf2, "%X,%03X%02X,%02X,%d,",
bin2int(p.prio),
bin2int(p.dgnhi),
bin2int(p.dgnlo),
bin2int(p.srcAD),
len);
//Serial.print(millis()); Serial.print(",");
Serial.println(canId, HEX);
Serial.print(outbuf2);
for (int i = 0; i < len; i++) {
sprintf(outbuf, "%02X", buf[i]);
Serial.print(outbuf);
}
Serial.println();
#endif
<Snipped for length>