How do I set the baud rate for the CAN bus?
Created May 4, 2012
Tim Rohaly
There is no method to directly set the baud rate - instead you must set the appropriate combination of bus timing parameters. The baud rate is calculated as:
baud rate (bits per second) = 18.432 x 10^6 / BRP / (1 + TSEG1 + TSEG2)where 18.432 MHz is the frequency of the clock crystal on the TINI and where BRP, TSEG1, and TSEG2 can be set using methods in the com.dalsemi.comm.CanBus class:
- setBaudRatePrescaler() changes BRP to values between 1 and 256
- setTSEG1() changes Timing Segment 1 to values between 2 and 16
- setTSEG2() changes Timing Segment 2 to values between 2 and 8
CanBus bus = new CanBus(CanBus.CANBUS0); bus.setBaudRatePrescaler(BRP); bus.setTSEG1(TSEG1); bus.setTSEG2(TSEG2);In order to comply with the CAN specification, the calculated baud rate must match the desired baud rate to within 0.5% in order for two CAN devices to properly communicate. Some combinations of BRP, TSEG1, and TSEG2 for commonly-used baud rates are:
Baud Rate | BRP | TSEG1 | TSEG2 |
10 Kbps | 88 | 13 | 7 |
20 Kbps | 71 | 5 | 7 |
20 Kbps | 44 | 13 | 7 |
50 Kbps | 41 | 3 | 5 |
50 Kbps | 23 | 10 | 5 |
125 Kbps | 7 | 13 | 7 |