硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4378|回复: 4
收起左侧

[RL-CAN] CAN总线位时间特性参数配置以及CAN波特率配置

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106760
QQ
发表于 2015-9-25 09:10:34 | 显示全部楼层 |阅读模式
手册说明:
==============================================
1.png

2.png
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106760
QQ
 楼主| 发表于 2015-9-25 09:14:02 | 显示全部楼层
1.png

2.png

3.png
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106760
QQ
 楼主| 发表于 2015-9-25 10:52:06 | 显示全部楼层
/*--------------------------- CAN_set_timing --------------------------------
*  Setup the CAN timing with specific parameters
*
*  Parameter:  ctrl:       Index of the hardware CAN controller (1 .. x)
*              tseg1:      Specifies Time Segment before the sample point
*              tseg2:      Time Segment after the sample point
*              sjw:        Synchronisation Jump Width
*              brp:        Baud Rate Prescaler
*---------------------------------------------------------------------------*/
static void CAN_set_timing (U32 ctrl, U32 tseg1, U32 tseg2, U32 sjw, U32 brp) {
  CAN_TypeDef *CANx = CAN_CTRL[ctrl-1];


  CANx->BTR &= ~(((          0x03) << 24) | ((            0x07) << 20) | ((            0x0F) << 16) | (          0x3FF));
  CANx->BTR |=  ((((sjw-1) & 0x03) << 24) | (((tseg2-1) & 0x07) << 20) | (((tseg1-1) & 0x0F) << 16) | ((brp-1) & 0x3FF));
}
==============================================================================
1.png

2.png
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106760
QQ
 楼主| 发表于 2015-9-25 11:15:00 | 显示全部楼层
波特率设置,实现方法巧妙:

/*--------------------------- CAN_set_baudrate ------------------------------
*  Setup the requested baudrate
*
*  Parameter:  ctrl:       Index of the hardware CAN controller (1 .. x)
*              baudrate:   Baudrate
*
*  Return:     CAN_ERROR:  Error code
*---------------------------------------------------------------------------*/
static CAN_ERROR CAN_hw_set_baudrate (U32 ctrl, U32 baudrate)  {
  U32 brp;

  /* Note: this calculations fit for PCLK1 = 36MHz */
  /* Determine which nominal time to use for requested baudrate and set
     appropriate bit timing                                                  */
  if (baudrate <= 500000)  {
    brp  = (CAN_CLK / 18) / baudrate;----------->这里较巧妙,18正好对应18个TQ,所以只需设置参数brp即可

    /* Load the baudrate registers BTR                                       */
    /* so that sample point is at about 72% bit time from bit start          */
    /* TSEG1 = 12, TSEG2 = 5, SJW = 4 => 1 CAN bit = 18 TQ, sample at 72%    */
    CAN_set_timing(ctrl, 12, 5, 4, brp);
  }  else if (baudrate <= 1000000)  {
    brp  = (CAN_CLK / 9) / baudrate; ----------->这里较巧妙,9正好对应9个TQ,所以只需设置参数brp即可

    /* Load the baudrate registers BTR                                       */
    /* so that sample point is at about 72% bit time from bit start          */
    /* TSEG1 = 5, TSEG2 = 3, SJW = 3 => 1 CAN bit = 9 TQ, sample at 66%      */
    CAN_set_timing(ctrl,  5, 3, 3, brp);
  }  else  {
    return CAN_BAUDRATE_ERROR;
  }

  return CAN_OK;
}
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106760
QQ
 楼主| 发表于 2015-11-17 18:05:16 | 显示全部楼层
1.png

======================================================
开发板是这么注释的,计算波特率时候跟SJW参数没问题,SJW应该用固定数值1来代替

    /*
        CAN 波特率 = RCC_APB1Periph_CAN1 / Prescaler / (SJW + BS1 + BS2);
        
        SJW = synchronisation_jump_width
        BS = bit_segment
        
        本例中,设置CAN波特率为1 Mbps        
        CAN 波特率 = 42000000 / 2 / (1 + 12 + 8) / = 1 Mbps        
    */
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_12tq;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
    CAN_InitStructure.CAN_Prescaler = 2;
    CAN_Init(CAN1, &CAN_InitStructure);
=======================================================
正确的应该是:

    /*
        CAN 波特率 = RCC_APB1Periph_CAN1 / Prescaler / (1+ BS1 + BS2);
        
        SJW = synchronisation_jump_width
        BS = bit_segment
        
        本例中,设置CAN波特率为1 Mbps        
        CAN 波特率 = 420000000 / 2 / (1 +12 + 8) / = 1 Mbps        
    */
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-5 07:33 , Processed in 0.218434 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表