微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > can波特率计算

can波特率计算

时间:12-13 来源:互联网 点击:

CAN->BTR = (u32)((u32)CAN_InitStruct->CAN->BTR = (u32)((u32)CAN_InitStruct->CAN_Mode < 30) | ((u32)CAN_InitStruct->CAN_SJW < 24) |

((u32)CAN_InitStruct->CAN_BS1 < 16) | ((u32)CAN_InitStruct->CAN_BS2 < 20) |

((u32)CAN_InitStruct->CAN_Prescaler - 1);

总结一下

Fpclk=36M 时 can波特率为250k 的配置为

/* CAN cell init */

CAN_InitStructure.CAN_TTCM=DISABLE;

CAN_InitStructure.CAN_ABOM=DISABLE;

CAN_InitStructure.CAN_AWUM=DISABLE;

CAN_InitStructure.CAN_NART=DISABLE;

CAN_InitStructure.CAN_RFLM=DISABLE;

CAN_InitStructure.CAN_TXFP=DISABLE;

CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;

CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;

CAN_InitStructure.CAN_Prescaler=9;

CAN_Init(&CAN_InitStructure); 250k

======================================

的:将can总线波特率设置为250k

在官方的can例程上 给出了100k 查询 和500k 中断方式的例子 分别设置如下:

CAN_Polling:

/* CAN cell init */

CAN_InitStructure.CAN_TTCM=DISABLE;

CAN_InitStructure.CAN_ABOM=DISABLE;

CAN_InitStructure.CAN_AWUM=DISABLE;

CAN_InitStructure.CAN_NART=DISABLE;

CAN_InitStructure.CAN_RFLM=DISABLE;

CAN_InitStructure.CAN_TXFP=DISABLE;

CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;

CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;

CAN_InitStructure.CAN_Prescaler=5;

CAN_Init(&CAN_InitStructure); 100k

/* CAN cell init */ CAN_Interrupt

CAN_InitStructure.CAN_TTCM=DISABLE;

CAN_InitStructure.CAN_ABOM=DISABLE;

CAN_InitStructure.CAN_AWUM=DISABLE;

CAN_InitStructure.CAN_NART=DISABLE;

CAN_InitStructure.CAN_RFLM=DISABLE;

CAN_InitStructure.CAN_TXFP=DISABLE;

CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;

CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;

CAN_InitStructure.CAN_Prescaler=1;

CAN_Init(&CAN_InitStructure); //500k

can时钟是RCC_APB1PeriphClock,你要注意CAN时钟频率

CAN波特率 = RCC_APB1PeriphClock/CAN_SJW+CAN_BS1+CAN_BS2/CAN_Prescaler;

如果CAN时钟为8M, CAN_SJW = 1,CAN_BS1 = 8,CAN_BS2 = 7,CAN_Prescaler = 2

那么波特率就是=8M/(1+8+7)/2=250K

=========================================

得到500Kb/s的波特率

CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;

CAN_InitStructure.CAN_Prescaler=1;

每一位的Tq数目 = 1 (固定SYNC_SEG) + 8 (BS1) + 7 (BS2) = 16

如果CAN时钟是 8 MHz : (8M / 1 ) / 16 = 500K

其中:

1 为分频系数

16 为每一位的Tq数目

为了设置为 100K, 把分频系数改为5即可, BS1 BS2 不变

每一位的Tq数目 = 1 (固定) + 8 (BS1) + 7 (BS2) = 16

如果CAN时钟是 8 MHz : (8M / 5 ) / 16 = 100K

如果想得到 1M 的波特率, CAN时钟仍然是 8 MHz的情况下, 分频系数不变

应该改变 BS1 BS2

CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;

每一位的Tq数目 = 1 (固定) + 5 (BS1) + 2 (BS2) = 8

如果CAN时钟是 8 MHz : (8M / 1 ) / 8 = 1000K

另外尽可能的把采样点设置为 CiA 推荐的值:

75% when 波特率 > 800K

80% when 波特率 > 500K

87.5% when 波特率 <= 500K

所以对于 100K 的波特率(假定使用 8MHz 时钟)

可以修改该BS1 BS2 为:

CAN_InitStructure.CAN_Prescaler=5;

CAN_InitStructure.CAN_BS1=CAN_BS1_13tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;

(1+13) / (1+13+2) = 87.5%

所以对于 500K 的波特率(假定使用 8MHz 时钟)

可以修改该BS1 BS2 为:

CAN_InitStructure.CAN_Prescaler=1;

CAN_InitStructure.CAN_BS1=CAN_BS1_13tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;

(1+13) / (1+13+2) = 87.5%

所以对于 1000K 的波特率(假定使用 8MHz 时钟)

可以修改该BS1 BS2 为:

CAN_InitStructure.CAN_Prescaler=1;

CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;

CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;

(1+5) / (1+5+2) = 75%

上边这个公式算出来的就是CAN的速率了.

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top