STM32 CAN 波特率、采样点设置


为了总结在STM32上设置CAN波特率和采样点,所以整理自己的理解如下:

一,CAN波特率

1.CAN位时序

STM32 CAN 波特率、采样点设置_第1张图片


2.公式推导

这里Tpclk理解成CAN时钟的周期(实际上CAN使用APB1时钟)

根据图中公式有:

(1)  正常的位时间(Tnbt) = (1+Tbs1 + Tbs2)*Tq

(2)  Tq = (BRP+1)*Tpclk 

设CAN时钟频率为Fpclk,则有

(3)  Tq = (BRP+1)/Fpclk

由(1),(3)有:

(4)  Tnbt =(1+Tbs1 + Tbs2)* (BRP+1) / Fpclk

由(4)知CAN输出波特率(Fcan = 1 / Tnbt )为:

Fcan = Fpclk / ( (1+Tbs1 + Tbs2)* (BRP[9:0]+1) )


3.程序配置

a.预备知识:

在stm32手册中BTR寄存器内容如下:

STM32 CAN 波特率、采样点设置_第2张图片

STM32 CAN 波特率、采样点设置_第3张图片

公式:Fcan = Fpclk / ( (1+Tbs1 + Tbs2)* (BRP[9:0]+1) )结合上表有:

Fcan = Fpclk  / ( (1 + (TS1[3:0] + 1) + (TS2[2:0] + 1) ) * (BRP[9:0]+1))


在 stm32f10x.h中有:

    /*******************  Bit definition for CAN_BTR register  ********************/
#define  CAN_BTR_BRP                         ((uint32_t)0x000003FF)        /*!< Baud Rate Prescaler */
#define  CAN_BTR_TS1                         ((uint32_t)0x000F0000)        /*!< Time Segment 1 */
#define  CAN_BTR_TS2                         ((uint32_t)0x00700000)        /*!< Time Segment 2 */
#define  CAN_BTR_SJW                         ((uint32_t)0x03000000)        /*!< Resynchronization Jump Width */
#define  CAN_BTR_LBKM                        ((uint32_t)0x40000000)        /*!< Loop Back Mode (Debug) */
#define  CAN_BTR_SILM                        ((uint32_t)0x80000000)        /*!< Silent Mode */

在stm32f10x_can.h中有:

#define CAN_BS1_1tq                 ((uint8_t)0x00)  /*!< 1 time quantum */
#define CAN_BS1_2tq                 ((uint8_t)0x01)  /*!< 2 time quantum */

...

#define CAN_BS2_1tq                 ((uint8_t)0x00)  /*!< 1 time quantum */
#define CAN_BS2_2tq                 ((uint8_t)0x01)  /*!< 2 time quantum */

...

由上可知表示值比实际值大1,Tbs1 = Tq*(TS1[3:0] + 1) 等价于 Tbs1 = Tq*CAN_BS1_xtq,CAN_BS1_xtq即为表示值


在stm32f10x_can.c中CAN_Init接口中设置BTR部分有:

        /* Set the bit timing register */
        CANx->BTR = ( uint32_t )(( uint32_t )CAN_InitStruct->CAN_Mode << 30 ) | \
                    (( uint32_t )CAN_InitStruct->CAN_SJW << 24 ) | \
                    (( uint32_t )CAN_InitStruct->CAN_BS1 << 16 ) | \
                    (( uint32_t )CAN_InitStruct->CAN_BS2 << 20 ) | \
                    (( uint32_t )CAN_InitStruct->CAN_Prescaler - 1 );

由上边代码可知BTR的BRP[9:0]的值为CAN_Prescaler - 1

从2中所推导的公式可知:

CAN波特率 = CAN时钟/( (1 + CAN_BS1 + CAN_BS2)  * CAN_Prescaler)


举个例子:

/* 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时钟为8M, CAN_BS1 = 8,CAN_BS2 = 7,CAN_Prescaler = 5

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


二,CAN采样点

有上面可知得到相同的波特率  CAN_BS1,CAN_BS2会有多种组合,而采样点由这2个参数确定,计算公式为:

sample = ( 1 +  CAN_BS1) /  (1 +  CAN_BS1  CAN_BS2)

参看这篇文章,采样点设置在80%到80.75%之间比较好。


参考资料:

http://lib.csdn.net/article/embeddeddevelopment/29438

http://www.61ic.com/Technology/embed/201103/31046.html

 《ISO 11898-1-2003》

《STM32_RM_CH_V10_1》

你可能感兴趣的:(工作)