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的速率了.
評論
查看更多