0
\$\begingroup\$

I want to connect a number of ATmega328P microcontrollers over a CAN bus. I found out that I will need some MCP2515 and MCP2551 ICs. In different schemas I sometimes see a 8 MHz crystal on the MCP2515 and sometimes a 16 MHz crystal.

This is the desired setup:

Multiple ATmega328P chips for a home automation project. The amount of data to be sent is really limited: switch on relay, switch off relay... so the speed on the CAN bus can be low.

What frequency should I take? Or doesn't it matter? Do I need to specify anything else needed to make the decision?

Is the use of a crystal on the ATmega required? Or can I use the internal clock of 8 MHz or 16 MHz?

\$\endgroup\$
12
  • \$\begingroup\$ Why are you using a bad MCU and external CAN controllers? Why can't you use a good MCU with built-in CAN controller? What does ATmega328P add to the project, apart from head ache? \$\endgroup\$
    – Lundin
    Mar 2 '18 at 12:10
  • \$\begingroup\$ @Lundlin Nothing wrong with the 328, but just not the correct choice here. And it's almost certainly because they're using Arduino. \$\endgroup\$
    – awjlogan
    Mar 2 '18 at 12:26
  • \$\begingroup\$ @awjlogan It's a horrible 8-bit microcontroller from the stone age. Today everyone programs in C, not assembler, and programming 8-bit microcontrollers in C is both dangerous and painful. It is not a task for beginners, they should program 32 bit MCUs, which is multiple times easier, as you avoid numerous classic pitfalls. \$\endgroup\$
    – Lundin
    Mar 2 '18 at 12:30
  • 1
    \$\begingroup\$ @Lundin That's a rather myopic viewpoint. Nothing wrong with 8bit micros or programming them in C. It's certainly more dangerous/painful to program them in ASM, especially for the beginner. \$\endgroup\$
    – awjlogan
    Mar 2 '18 at 13:02
  • 1
    \$\begingroup\$ @Lundlin Those things are mostly irrelevant as performance = enough to do the job, given other constraints (cost etc). Why use a more capable [expensive] chip than you need? If you've got a 64 kB program you should move up to a different architecture; 1 kB, why bother? Arduino-ificiation is a separate issue, but what's harmful about, say, an artist (not a programmer/hardware person and with no desire to be) using it for an installation? There's nothing inherently 'wrong' with 8 bit micros: they have strengths and limitations, much like any processor. \$\endgroup\$
    – awjlogan
    Mar 2 '18 at 16:15
1
\$\begingroup\$

The clock frequency must be picked so that it suits the divisor hardware of the CAN controller. What's suitable is different from controller to controller, but often the clock depends on how many time quanta you use per baud rate. You should aim to use one of the CANopen standardized baud rates and sample points, since these are industry standard (like 250 kbps or 125 kbps). The standard is available for free somewhere at https://www.can-cia.org if you register and download.

When you have determined how many time quanta that's needed to achieve a certain baud rate, you should pick a clock that gives a divisor error of less than 1%. CAN is picky with this: inaccurate divisors may cause the whole node to fail. Therefore you must always use crystal oscillators for CAN communication; built-in RC oscillators are too bad to be used.

\$\endgroup\$
2
  • \$\begingroup\$ Some vendors (unfortunately) use 0.5% ceramic oscillators (even though 0.1% ones are nearly the same price). \$\endgroup\$ May 8 '18 at 12:18
  • \$\begingroup\$ @PeterMortensen I believe that 0.5% should be sufficient for CAN, although cost is not really much of a reason to use an external ceramic resonator over a crystal. \$\endgroup\$
    – Lundin
    May 8 '18 at 12:35
1
\$\begingroup\$

You need to actually read the datasheet. After you do, the answers to your questions will be obvious.

Table 13-2, Oscillator Timing Characteristics, on page 71 tells you the min and max allowable frequencies outright. If you could be running the part anywhere within its valid supply range, then those limits are 1 to 25 MHz.

There is also a whole chapter, section 8.0, page 53, about the oscillator. I'm not going to read it for you.

The only remaining question is then what frequency you want to use within the allowable limits. Again, the datasheet describes exactly how the oscillator frequency is used to derive the CAN bit timing. In fact, there's a whole chapter on that too. See section 5.0, Bit Timing, on page 37. Read it.

Basically, you pick a oscillator frequency that can be divided down to the bit time quanta you want to use. The number of quanta per bit is something you have some leeway over. Generally you want to be in the 9 to 20 quanta per bit range if I remember right.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.