Hello,
I am trying to demonstrate can bus communication using arduino uno(2 units) and sparkfun canbus sheild(2 units). I am trying to send the basic examples given in sparkfun canbus library.
i am getting “Can init Ok” message on serial monitor but no message is being displayed. can anyone help me in this?
thanks in advance
my code for transmitter:
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
//********************************Setup Loop*********************************//
void setup() {
Serial.begin(9600);
Serial.println("CAN Write - Testing transmission of CAN Bus messages");
delay(1000);
if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok");
else
Serial.println("Can't init CAN");
delay(1000);
}
//********************************Main Loop*********************************//
void loop()
{
tCAN message;
message.id = 0x631; //formatted in HEX
message.header.rtr = 0;
message.header.length = 8; //formatted in DEC
message.data[0] = 0x40;
message.data[1] = 0x05;
message.data[2] = 0x30;
message.data[3] = 0xFF; //formatted in HEX
message.data[4] = 0x00;
message.data[5] = 0x40;
message.data[6] = 0x00;
message.data[7] = 0x00;
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message);
delay(1000);
}
my code for reciever is:
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
//********************************Setup Loop*********************************//
void setup() {
Serial.begin(9600); // For debug use
Serial.println("CAN Read - Testing receival of CAN Bus message");
delay(1000);
if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok");
else
Serial.println("Can't init CAN");
delay(1000);
}
//********************************Main Loop*********************************//
void loop(){
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
//if(message.id == 0x620 and message.data[2] == 0xFF) //uncomment when you want to filter
//{
Serial.print("ID: ");
Serial.print(message.id,HEX);
Serial.print(", ");
Serial.print("Data: ");
Serial.print(message.header.length,DEC);
for(int i=0;i<message.header.length;i++)
{
Serial.print(message.data[i],HEX);
Serial.print(" ");
}
Serial.println("");
//}
}}
}
my pin connection are:
Transmitter side:
Arduino uno ---- canbus sheild
D8-D13 ------------ D8-D13
Gnd ----------------- gnd
5v ------------------- 5v
D2 ------------------ D2
ioref ------------- ioref
reset ------------ reset
Reciever side:
Arduino uno ---- canbus sheild
D8-D13 ------------ D8-D13
Gnd ----------------- gnd
5v ------------------- 5v
D2 ------------------ D2
ioref ------------- ioref
reset ------------ reset
connections between transmitter can to reciever can is via CAN_L and CAN_H and Gnd.
I am using sparkfun canbus library.
Thanks in Advance