hi guys, I'm playing with can bus module mcp2515 - it works but I just don't know how to start module in listen only mode..
I have modified a code a little but now, first it starts in CONFIGURE MODE - good!
code:
START_INIT:
CAN.setMode(0b10000000); //configuration mode
but than, with if(CAN.begin(MCP_8MHz) == CAN_OK) - it switch to normal mode for a little time and then
with code
Serial.println("MODE IS:");
Serial.print(CAN.getMode());
delay(1000);
CAN.setMode(0b1100000); //listen only
Serial.println("MODE IS:");
Serial.print(CAN.getMode());
CAN_125KBPS;
delay(1000);
it switch to listen only.
But I would like to have this module in LISTE ONLY mode all time, from beginning - to be sure that car has no idea about extra attached can bus module.
Have you any experiences how to go from configuration mode to listen mode without being in normal mode for a little time?
/**************************
* Author *
* omarCartera *
* *
* 5/8/2017 *
* *
* c.omargamal@gmail.com *
**************************/
#include <SPI.h> //SPI is used to talk to the CAN Controller
#include <mcp_can.h>
MCP_CAN CAN(10); //set SPI Chip Select to pin 10
unsigned char len = 0;
unsigned char buf[8];
unsigned int canID;
void setup()
{
Serial.begin(115200); //to communicate with Serial monitor
Serial.println("waiting 3 seconds");
delay(3000);
//tries to initialize, if failed --> it will loop here for ever
START_INIT:
CAN.setMode(0b10000000); //configuration mode
// if(CAN_OK == CAN.begin(CAN_125KBPS)) //setting CAN baud rate to 500Kbps
if(CAN.begin(MCP_8MHz) == CAN_OK)
{
Serial.println("CAN BUS Shield init ok!");
Serial.println("MODE IS:");
Serial.print(CAN.getMode());
delay(1000);
CAN.setMode(0b1100000); //listen only
Serial.println("MODE IS:");
Serial.print(CAN.getMode());
CAN_125KBPS;
delay(1000);
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
if(CAN_MSGAVAIL == CAN.checkReceive()) //check if data is coming
{
CAN.readMsgBuf(&len, buf); //read data, len: data length, buf: data buffer
canID = CAN.getCanId(); //getting the ID of the incoming message
Serial.print("ID is: ");
Serial.print(canID, HEX); //printing the ID in its standard form, HEX
Serial.print(" Length is: ");
Serial.print(len);
Serial.println(" ");
Serial.println("ORIGINAL:");
for(int i = 0; i<len; i++) //looping on the incoming data to print them
{
Serial.write(buf[i]); //Serial.write prints the character itself
}
Serial.println("? ");
Serial.println("MY TRY:");
for(int i = 0; i<len; i++) //looping on the incoming data to print them
{
Serial.print(buf[i], DEC);
}
Serial.println("\n\t*****************\n");
}
}