CAN时钟源的选择有两种,一种为总线时钟,一种为外接时钟
波特率计算公式:F(Baud) = F/(BRP* (1+TSEG2+TSEG1))
波特率设置相关寄存器
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
SJW1 |
SJW0 |
BRP5 |
BRP4 |
BRP3 |
BRP2 |
BRP1 |
BRP0 |
Table1 CANBTR0
SJW1 |
SJW0 |
Synchronization Jump Width |
0 |
0 |
1 Tq clock cycle |
0 |
1 |
2 Tq clock cycle |
1 |
0 |
3 Tq clock cycle |
1 |
1 |
4 Tq clock cycle |
Table2 SJW
BRP5 |
BRP4 |
BRP3 |
BRP2 |
BRP1 |
BRP0 |
Prescaler value |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
2 |
0 |
0 |
0 |
0 |
1 |
1 |
3 |
0 |
0 |
0 |
1 |
1 |
1 |
4 |
.. |
.. |
.. |
.. |
.. |
.. |
.. |
1 |
1 |
1 |
1 |
1 |
1 |
64 |
Table3 Baud Rate Prescaler
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
SAMP |
TSEG22 |
TSEG21 |
TSEG20 |
TSEG13 |
TSEG12 |
TSEG11 |
TSEG10 |
Table4 CANBTR1
TSEG22 |
TSEG21 |
TSEG20 |
Time Segment 2 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
2 |
.. |
.. |
.. |
... |
1 |
1 |
0 |
7 |
1 |
1 |
1 |
8 |
Table5 TSEG2
TSEG13 |
TSEG12 |
TSEG11 |
TSEG10 |
Prescaler value |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
1 |
2 |
0 |
0 |
1 |
0 |
3 |
0 |
0 |
1 |
1 |
4 |
.. |
.. |
.. |
.. |
.. |
1 |
1 |
1 |
0 |
15 |
1 |
1 |
1 |
1 |
16 |
例程
1. 总线时钟40MHZ,设置CAN波特率为500KHZ
40MHZ/500KHZ=80
80 = BRP *(1+TSEG2+TSEG1) =10 * 8;
所以设置BRP =8,TSEG2=4,TSEG1=5. SJW=0x01对应寄存器值
CAN0BTR0=0x47;
CAN0BTR1=0x43;
2. 总线时钟40MHZ,设置CAN波特率为250KHZ
40MHZ/250KHZ=16
80 = BRP *(1+TSEG2+TSEG1) =16 * 8;
所以设置BRP =8,TSEG2=4,TSEG1=5,SJW=0x01对应寄存器值
CAN0BTR0=0x4F;
CAN0BTR1=0x43;