Go Down

Topic: Need a help for arduino nano and CAN bus (Read 109 times) previous topic - next topic

caneiv

Hello Arduino friends. I am new here and need a little help.

I use a arduino nano + mcp2515 and want to read some message over 95kBPS Mid-Speed Can-Bus. I try with canhacker but this baud rate missing, what library and configuration I need to use to read this.

I hope someone will help me.

Klaus_K

The CAN bus speed is set by the crystal frequency and the configuration registers in the MCP2515.

You can use any library you like but very likely need to calculate the registers values yourself and create some definitions in a way your library does.

If you let me know which CAN library you would like to use and where to find it (Library Manager or Github) and if you give me the crystal frequency of your MCP2515, I can tell you whether the speed is possible and how you need to modify the library or what settings to use.

caneiv

#2
Jan 16, 2020, 08:57 am Last Edit: Jan 16, 2020, 11:57 am by caneiv
The CAN bus speed is set by the crystal frequency and the configuration registers in the MCP2515.

You can use any library you like but very likely need to calculate the registers values yourself and create some definitions in a way your library does.

If you let me know which CAN library you would like to use and where to find it (Library Manager or Github) and if you give me the crystal frequency of your MCP2515, I can tell you whether the speed is possible and how you need to modify the library or what settings to use.
I use library from here https://github.com/autowp and my crystal frequency of MCP2515 is 8kHz. Somewhere I read that have with 16kHz and have just a crystal if need to change.

Klaus_K

#3
Jan 16, 2020, 09:27 pm Last Edit: Jan 16, 2020, 10:19 pm by Klaus_K
As you already noticed 95kb/s is missing from most libraries. The reason is that you cannot derive that bitrate from the same crystal used for all the other standard CAN bitrates.

The bitrate is derived by dividing the clock frequency non-fractional. When you work backwards, you can see that you would need to divide 16 and 8MHz by a fraction to get 95kHz.

16MHz / 95kHz = 168.42
8MHz / 95kHz = 84.21

With an 8MHz crystal you could get a bit rate of 95.238kb/s when you divide by 84.

The MCP2515 divides the clock by 2 and then you can use a 1:2 prescaler and 21TQ = 84.
The configuration registers would need to be CNF1 = 0x01, CNF2 = 0xBB, CNF3 = 0x07.

There is a high likelihood this will not work. The CAN bit timing requires a high precision.

The better way would be to get a crystal that has the right frequency so the bitrate can be created accurately. 9.5MHz and 19MHz would be the first obvious choices. But there are others possible.

9.5MHz - CNF1 = 0x01, CNF2 = 0xBF, CNF3 = 0x07.
19MHz  - CNF1 = 0x03, CNF2 = 0xBF, CNF3 = 0x07.

To modify the library, you need to

- add a new clock speed to the CAN_CLOCK enum in mcp2515.h
- add some defines just above CAN_CLOCK the enum

#define MCP_19MHz_95kBPS_CFG1 (0x03)
#define MCP_19MHz_95kBPS_CFG2 (0xBF)
#define MCP_91MHz_95kBPS_CFG3 (0x07)

- In the mcp2515.cpp file create a new case in the following function. You only need to create one speed setting for 95kb/s.

MCP2515::ERROR MCP2515::setBitrate(const CAN_SPEED canSpeed, CAN_CLOCK canClock)

Let me know when you have any questions.

Go Up