Has anyone ever attempted to hook up one of the many available Arduino CAN shields that utilizes the Microchip MCP2515 CAN controller?
Seeed Studios has code available that is supposed to work at 1Mbps, but I have not been able to get it to work on the robot’s CAN bus. The code is supposed to read data on the bus and transmit it over the UART interface to my computer.
I have checked that the example code does indeed work using my car at 500kbps.
I thought maybe they had the calculations wrong, but they appear to be correct.
The Seeed Studio shield uses a 16MHz crystal oscillator. That would give a TQ of 2/16MHz, or 1/8MHz, Of course, the inverse of Hertz, or cycles / second, is seconds / cycle, giving the time base.
The three registers that select the CAN baud rate are cnf1, cnf2, and cnf3.
The “+ 1” are referenced from the datasheet, such that it will increment the value internally by 1, so if the register is set to 0, the interpreted number will really be 1.
cnf1 = 0x00: SJW = 0; BRP = 0 + 1
cnf2 = 0xD0: BTLMODE = 1; SAM = 1; PHSEG1 = 2 + 1; PRSEG = 0 + 1
cnf3 = 0x82: SOF = 1; WAKFIL = 0; PHSEG2 = 2 + 1
So now that you know what the registers are set to, let’s do the math on what that means for bit timings.
For 1 Mbps, I need a bit timing of 1x10^-6 seconds, or 1 / 1 MHz (which is why I kept it in that silly format above, it makes the math easier), or again, for math terms, let’s assume 8 / 8 MHz.
Tbit = Tsync_seg + Tprop_seg + Tps1 + Tps2
Tsync_seg = 1 * TQ, or 1 / 8 MHz (1/8)
Tprop_seg = (0 + 1) TQ, or 1 / 8 MHz (1/8)
Tps1 = (2 + 1) TQ, or 3 / 8 MHz (3/8)
Tps2 = (2 + 1) TQ, or 3 / 8 MHz (3/8)
From here, it’s simple addition: (1/8) + (1/8) + (3/8) + (3/8) == (8/8).
So, those registers seem fine, and from what I can tell, follow all the rules the datasheet specified. I don’t know why it’s not working. The red RX light is flashing, so it’s seeing data coming in. I know the code works because it works in my car from the OBDII port at 500kbps. Bus impedance was measured at 56 Ohms (a little shy of 60 Ohms, but should still be fine).
One other puzzling thing is my SALAE logic can read the bus just fine at 1 Mbps. The only thing I can note is that the bus uses the extended frame format with the 29-bit identifiers rather than the standard frame with 11-bit.
Any ideas?