embeddedcan-bus

CAN bus bit timing calculation


I am kind of confused with a CAN bus bit timing calculation, so from the datasheet for the PIC18F26K83, for 250 kbit/s and a 8 MHz clock, I have calculated these:

SJW = 3, BRP = 0, SEG1PH = 6, PRSEG = 5, PHSEG2 = 4.

However, when I use a CAN bus bit timing calculator program, it gives these results:

SJW = 1; BRP = 2; PHSEG1 = 1; PHSEG2 = 2; PROPSEG = 4;

These are quite different results, and it seems like both gives me around 50 kbit/s instead of 250 kbit/s. Is there an easy way to calculate these constraints? Or is there a more understandable step-by-step calculation guide rather than that are on the Internet or in the datasheet which are quite complex and want you to decide some parameters by yourself, such as SJW!


Solution

  • I haven't used the PIC CAN controllers, but in general it goes something like this:

    Phase seg 2 in relation to the total amount of tq gives the location of the sample point. No matter which CAN application you are using, I would advise to use the standard recommendations of CANopen, which is a sample point of 87.5%, with acceptable locations are between 85% - 90% (it's more lenient at the highest baudrates).

    So 1 - tqPHSEG2 / tqtotal should be near 87.5%. In your case 1 - 4/15 = 0.73. You need to fix this by increasing the size of the other segments or lowering the size of phase seg 2. This is always a bit of trial & error.

    As for baudrate, you probably get some formula along the lines of:

    clock / (tqtotal * baudrate) = prescaler

    Your specific CAN controller will give the exact formula for that particular hardware. Keep in mind what's the clock source, is it the raw oscillator, is it the system clock, is it something else? (PIC are notorious for using various system clock dividers and then you might have PLL adjustment on top of that.)

    SJW doesn't affect the baudrate. It is the allowed re-sync width used for baudrate mismatches, mostly relevant at higher baudrates. At 250kbps, SJW=1 should work just fine.