Alternative Baud Rate Options for CANbus Shield

Hello All,
I wanted to shortly ask you if it is possible to communicate with the alternative baud rate values (which are not existing in the list of seeed-supported baudrates)? The reason is, I have to “listen” another product which is sending data at 31.25kbps. Here comes the question, is it possible and if so where to start? :question: :unamused:

Regards…

This will require some bit calculations and then modifying the library.

In “mcp_can_dfs.h” add the following around line ~248:

#define MCP_16MHz_31k25BPS_CFG1 (0x0F)
#define MCP_16MHz_31k25BPS_CFG2 (0xF1)
#define MCP_16MHz_31k25BPS_CFG3 (0x85)

and in the same file around line ~336:

#define CAN_31K25BPS   13

In “mcp_can.cpp” add the following around line ~185:

case (CAN_31K25BPS): cfg1 = MCP_16MHz_31k25BPS_CFG1; cfg2 = MCP_16MHz_31k25BPS_CFG2; cfg3 = MCP_16MHz_31k25BPS_CFG3; break;

Note: the above snippet in “mcp_can.cpp” MUST be placed in the baud switch & case correctly or it will fail to function. Just as long as the above is pasted like this:

[existing code...] break; (Paste the code here!) case(some_baud_rate): [...existing code]

This should allow you to use ‘CAN_31K25BPS’ as a baud rate. I dont have anything to test that rate with, but the bit timing calculations should be correct if your MCP2515 is using a 16MHz crystal (Which all CAN_Shields I’ve seen are).

Or you can use the library here, as I just added support for it: github.com/coryjfowler/MCP2515_lib

cory.j.fowler thank you very much for support. I have currently updated my libs and bot of the shields are communicating now with the new baud rate. Moreover can you briefly explain how you found out the values for a given baud rate? I do not want to disturb you guys everytime when I need something else… :slight_smile:

The datasheet (section 5) explains it the best that I could even try to.

Basically, the first six bits of register CNF1 are used for the Baud Rate Prescaler (BRP) which generates the Time Quantum (TQ). The time quantum is just a time duration that we will add together to create the Bit Time.

The Bit Time is composed up of:
SyncSeg with a fixed duration of one time quantum
PropSeg with a configurable duration of one to eight time quantum
PhaseSeg1 (PS1) with a configurable duration of one to eight time quantum
PhaseSeg2 (PS2) with a configurable duration of one to eight time quantum

Section 5.3 shows a few crucial requirements about the relationship of PropSeg, PS1, PS2, TDelay, and SJW.

PropSeg + PS1 >= PS2 PropSeg + PS1 >= Tdelay PS2 > SJW
So for a BRP of 15 and using a 16MHz crystal, you end up with a 2 microsecond TQ.
At a baudrate of 31.25k, you would have a Bit Time of 32 microseconds.
Dividing the Bit Time by the TQ gives the total number of TQ needed to create the baudrate.

Essentially,

PS1 7 PS2 6 PropSeg 2 SyncSeg 1 SJW 1 Total 16
So, I ended up making a spreadsheet to calculate the bits…