Arduino Mega Canbus 2515 module problem

I am using 2515 can bus modules with arduino mega.

I made all the connections right. but I'm having trouble sending data.

I don't know how to include the ext and rtr partitions in the id! also there is no place to write ext and rtr.

mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCU_8MHZ);
mcp2515.setNormalMode();

canMsg1.can_id = 0x0F6;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x8E;
canMsg1.data[1] = 0x87;
canMsg1.data[2] = 0x32;
canMsg1.data[3] = 0xFA;
canMsg1.data[4] = 0x26;
canMsg1.data[5] = 0x8E;
canMsg1.data[6] = 0xBE;
canMsg1.data[7] = 0x86;
mcp2515.sendMessage(&canMsg1);

Welcome to the forum

Have a look at the following file.

/* special address description flags for the CAN_ID */
#define CAN_EFF_FLAG 0x80000000UL /* EFF/SFF is set in the MSB */
#define CAN_RTR_FLAG 0x40000000UL /* remote transmission request */

It looks like the MSB bits of the ID are used. The ID is only 29 bits and stored in a 32 bit variable which leaves 3 bits for additional information.

Just a SWAG but CAN will not work without a responder. You made no mention of anything on the network, its topology etc. A schematic, not a frizzy drawing would help as would all of the code.

Klaus_K:
Welcome to the forum

Have a look at the following file.

arduino-mcp2515/can.h at master · autowp/arduino-mcp2515 · GitHub

/* special address description flags for the CAN_ID */

#define CAN_EFF_FLAG 0x80000000UL /* EFF/SFF is set in the MSB /
#define CAN_RTR_FLAG 0x40000000UL /
remote transmission request */




It looks like the MSB bits of the ID are used. The ID is only 29 bits and stored in a 32 bit variable which leaves 3 bits for additional information.

yes I saw this too, but I did not understand how to use it. I wrote from Github, but the detailed sample code was not provided.

All I want to do is include ext and rtr in the id.

As I read it you just need to set the flag. e.g.

canMsg1.can_id = 0x0F6; // standard ID

canMsg1.can_id = 0x800000F6UL; // extended ID

You could use the define and logic OR it with the ID if you wanted to make the code more explicit. I did not try this, but I believe this should work. This will be computed at compile time and should result in the same code as above.

canMsg1.can_id = 0xF6 | CAN_EFF_FLAG; // extended ID

Klaus_K:
Ben okurken sadece bayrağı ayarlamanız gerekiyor. Örneğin , kodu daha açık hale getirmek istiyorsanız, tanım ve mantığı VEYA bunu ID ile kullanabilirsiniz. Bunu denemedim ama işe yaraması gerektiğine inanıyorum. Bu, derleme zamanında hesaplanacak ve yukarıdaki ile aynı kodla sonuçlanacaktır.

canMsg1.can_id = 0x0F6; // standard ID

canMsg1.can_id = 0x800000F6UL; // extended ID








canMsg1.can_id = 0xF6 | CAN_EFF_FLAG; // extended ID

thank you so much. What if ext and rtr were used at the same time?

I'll even tell you my rtr and ext code. Can you adapt it?
rtr 0
ext 1

fatihpc:
What if ext and rtr were used at the same time?

You simply OR the flag for the RTR bit.

canMsg1.can_id = 0xF6 | CAN_EFF_FLAG | CAN_RTR_FLAG; // extended ID + RTR

In case you do not know/remember how binary logic works, it is not complicated and very useful for microcontroller programming. You can use the Windows 10 Calc to play with it. Switch to programmers mode and you will find all the basic functions (AND, OR, ..., Shift) as well as numbers in all the number formats (binary, decimal, hexadecimal).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.