sherzaad:
Had a look at the library and you are right about it. It does not allow you to modify rtr. However since as suggested in reply #9, rtr=0, IMHO you should be able to still use this library; just specify the CAN ID and dlc. By default, the library sets rtr=0.
if you want to play about with the rtr bit you can alway try to use the MCP2515 library I put together.
hope that helps....
Thank you for the help! Now I have some sort of communication to my MCP25050!! I used your library and it seems to work. I will call MCP25050 slave now on.
The problem is now to find the suitable speed. When I change speeds, the reply from slave changes. It seems like 500 kbps gives the most reliable data but how do I know the right speed?
Also the slave answers with the same message to any address. So If I send with ID 11 I get an answer. If I change the ID to 20 I still get the same answer. And the answer is not correct.
Example:
My device ID is 0x11. I send RTR = 1, IDE = 0, DLC = 8. This is the question to ask for "Read A/D regs". I will get an answer: ID 464, data bytes = 0 when I should get ID 0x11, data bytes = 8.
What could be wrong? Below is the code I used with arduino and a snippet from the terminal.
#include <mcp2515.h>
#include <mcp2515_defs.h>
MCP2515 CAN_Ch1(10, 2); //SPI CS, interrupt
//MCP25150 - Arduino: INT = 2, SCK = 13, SI = 11, SO = 12, CS = 10
void setup() {
pinMode(2, INPUT);
tCAN message;
uint8_t std_data[7] ={0,0,0,0,0,0,0};
//define CAN standard message
message.id.std = 0b00100010000;//get A/D regs
message.header.rtr = 1;
message.header.ide = 0;
message.header.dlc = 8;
memcpy(message.data,std_data,sizeof(std_data));
Serial.begin(115200);
Serial.println("LAite kaynnistetty. CAN tsekkaaja!");
if (CAN_Ch1.CAN_init(CANSPEED_500kBPS)) //käynnistetään MCP nopeudella x
Serial.println("CAN1 Init ok");
else
Serial.println("Can't Init CAN1");
}
void loop() {
tCAN message;
CAN_Ch1.send_message(&message);
Serial.println("Data lahetetty");
if (CAN_Ch1.check_message()) {
if (CAN_Ch1.get_message(&message)) {
Serial.print("CAN_1 ID: ");
if(message.header.ide) { Serial.print(message.id.ex, DEC); Serial.print("EX");} //extended CAN ID
else {Serial.print(message.id.std, DEC); Serial.print("STD");} //standard CAN ID
Serial.print(", ");
Serial.print("Data[");
Serial.print(message.header.dlc, DEC);
Serial.print("]: ");
for (int i = 0; i < message.header.dlc; i++) {
Serial.print(message.data[i], DEC);
Serial.print(" ");
}
Serial.println("");
}
}
delay(2000);
}
Snippet with the above code in the terminal:
LAite kaynnistetty. CAN tsekkaaja!
CAN1 Init ok
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237
Data lahetetty
CAN_1 ID: 903STD, Data[2]: 116 237