CAN bit timing and baud rate is a nightmare for anyone to use. You really have to know a lot to understand how it exactly works. In most cases, you do not have to get things perfect as if you need so then you probably understand all CAN parameters (sync, propagation, phase 1, phase 2, SJW, time quanta and more!!!) [url]http://en.wikipedia.org/wiki/Controller_area_network[/url]
This forum is not meant to cover CAN but here is some steps if you need to use CAN bus on our devices:
- Divide the system clock (72Mhz) to get an appropriate clock for the CAN peripheral (this is NOT the baud rate) This is called the BRP
- figure out how many clocks you need to make up one bit. Usually this is 24 or 16. This is called TQ
- Assign values to T1 and T2 where T1+T2+1=TQ
Let us say we need 250Kbps
- From 72Mhz system clock, I want the CAN clock to be 6Mhz so I need to divide by 12 (BRP=12)
- 6Mhz/250kbps = 24 TQ (we usually want 16 or 24)
- T1 = 15, T2 = 8 will give us 15 + 8 + 1 = 24 and this is what we need
I got the T1 and T2 values from [url]http://www.kvaser.com/can/protocol/index.htm[/url]
I picked the first value and subtracted 1 from T1 because the calculator included the sync bit
Here is the code with good comments
// These numbers were calculated using the calculator on this link:
// http://www.kvaser.com/can/protocol/index.htm
// We used the very first value from the calculator output
/////////////////////////////////////////////////////////////////////////////////////////////
// Bitrate 250Kbps
// CLK = 72 Mhz, with BRP = 12 -> 6Mhz CAN clock
// 6Mhz/250Kbps = 24 TQ
// T1 = 16 minus 1 for sync = 15
// T2 = 8
// 15 + 1 + 8 = 24 TQs which is what we need
/////////////////////////////////////////////////////////////////////////////////////////////
BRP = 12;
T1 = 15;
T2 = 8;
// For 500Kbps you can use BRP=6 and for 1Mbps you can use BRP=3 and for 125Kbps use BRP=24...and so on
// Keep T1 and T2 the same to keep the sampling pint the same (between 70% and 80%)
CAN canChannel = new CAN(CAN.CANChannel.Channel_1, ((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0));