阅读量:0
Stm32中CAN传输波特率的计算和配置
计算
CAN波特率的计算首先要理解位时间特性的概念,建议自行理解一下,推荐链接:https://blog.csdn.net/flydream0/article/details/8170185
CAN波特率 = APB总线频率/BRP分频器/(1+tBS1+tBS2)
以最近学习的stm32f407ZGT6开发板为例,需要设置一个波特率为250k的CAN接口。
时钟配置如下:
如上图 APB1时钟 = 42MHz
配置
hcan2.Instance = CAN2; hcan2.Init.Prescaler = 12;//APB总线时钟4分频 hcan2.Init.Mode = CAN_MODE_NORMAL; hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan2.Init.TimeSeg1 = CAN_BS1_7TQ;//tBS1 = 7个tCAN时钟周期 hcan2.Init.TimeSeg2 = CAN_BS2_6TQ;//tBS2 = 6个tCAN时钟周期 hcan2.Init.TimeTriggeredMode = DISABLE; hcan2.Init.AutoBusOff = DISABLE; hcan2.Init.AutoWakeUp = DISABLE; hcan2.Init.AutoRetransmission = DISABLE; hcan2.Init.ReceiveFifoLocked = DISABLE; hcan2.Init.TransmitFifoPriority = DISABLE;
APB1 = 42MHz
CANbps= Fpclk/((Prescaler )*((BS1+BS2+1)
所以这里CANbps=42000/12/(7+6+1) = 250k bps
总体配置方向: Tseg1>=Tseg2 Tseg2>=tq; Tseg2>=2TSJW