新聞中心

can波特率計(jì)算

作者: 時(shí)間:2016-12-13 來源:網(wǎng)絡(luò) 收藏
  假設(shè)我們先不考慮BTR0中的SJW位和BTR1中的SAM位。那么,BTR0和BTR1就是2個(gè)分頻系數(shù)寄存器;它們的乘積是一個(gè)擴(kuò)展的分頻系數(shù)。即:

  BTR0×BTR1=F_BASE/Fbps (1)

本文引用地址:http://butianyuan.cn/article/201612/329398.htm

  其中:內(nèi)部頻率基準(zhǔn)源F_BASE = Fclk/2,即外部晶振頻率Fclk的2分頻。注意任何應(yīng)用中,當(dāng)利用外部晶振作為基準(zhǔn)源的時(shí)候,都是先經(jīng)過2分頻整形的。

  式中,當(dāng)晶振為16M時(shí),F(xiàn)_BASE=8000K;當(dāng)晶振為12M時(shí),F(xiàn)_BASE=6000K,Fbps就是我們所希望得到的CAN總線頻率。單位為K。

  設(shè)式中BTR0=m,BTR1=n,外部晶振16M,則有:m • n =8000/ Fbps

  這樣,當(dāng)Fbps取我們希望的值時(shí),就會得到一個(gè)m * n的組合值。當(dāng)n選定,m值也唯一。 n值CAN規(guī)范中規(guī)定8~25。(也就是BTR1的值)基本原則為:Fbps值越高時(shí),選取n(通過設(shè)置BTR1)值越大。其原因不難理解。

  我假定一般應(yīng)用中選取n=10,也就是:同步段+相位緩沖段1+相位緩沖段2 =1+5+4 ,則(2)式簡化為m=800/Fbps;m的最大設(shè)置值為64,SJA1000($2.8080)最大分頻系數(shù)m*n=64x25=1600。因此標(biāo)準(zhǔn)算法中通常以16M晶振為例。其實(shí)有了公式(1),任何晶振值(6M~24M)都很容易計(jì)算。

  SAM的確定:低頻時(shí),選SAM=1,即采樣3次。高頻100K以上時(shí),取SAM=0,即采樣1次。SJA重同步跳寬選取: 與數(shù)字鎖相環(huán)技術(shù)有關(guān)。n值選得大時(shí),SJA可以選得大,即一次可以修正多個(gè)脈沖份額Tscl。n值小或頻率低時(shí),選SJA=1。即BTR0.7和BTR0.6都設(shè)為0。

  STM32($18.3200)的CAN波特率計(jì)算

  STM32里的CAN 支持2.0A,2.0B, 帶有FIFO,中斷等, 這里主要提一下內(nèi)部的時(shí)鐘應(yīng)用.

  bxCAN掛接在APB1總線上,采用總線時(shí)鐘,所以我們需要知道APB1的總線時(shí)鐘是多少. 我們先看看下圖,看看APB1總線時(shí)鐘:

  

  APB1時(shí)鐘取自AHB的分頻, 而AHB又取自系統(tǒng)時(shí)鐘的分頻, 系統(tǒng)時(shí)鐘可選HSI,HSE, PLLCLK, 這個(gè)在例程的RC設(shè)置里都有的,

  然后再看看有了APB1的時(shí)鐘后,如何算CAN的總線速率, 先看下圖:

  

  有了上邊的這個(gè)圖,基本就清楚了.

  總線時(shí)鐘MHz (3+TS1+TS2)*(BRP+1)

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

  下面是我的計(jì)算:

  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

  CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;

  注意//#define CAN_BS1_3tq ((uint8_t)0x02) /*!< 3 time quantum */

  CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;

  CAN_InitStructure.CAN_Prescaler = 4;//2

  nominal bit time(3+5+1)tq=9tq

  關(guān)于分頻系數(shù) 查看 system_stm32f10x.c下面的

  static void SetSysClockTo72(void) 函數(shù)

  /* HCLK = SYSCLK */

  /* PCLK2 = HCLK */

  /* PCLK1 = HCLK/2 */

  所以can時(shí)鐘 72MHZ/2/4=9 Mhz

  tq=1/36Mhz

  波特率為 1/nominal bit time= 9/9=1MHZ

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

  -----------------------------------------------

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

  void CAN_Configuration(void)

  {

  CAN_InitTypeDef CAN_InitStructure;

  CAN_FilterInitTypeDef CAN_FilterInitStructure;

  /* CAN register init */

  CAN_DeInit();

  CAN_StructInit(&CAN_InitStructure);

  /* 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_Normal;

  CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;

  CAN_InitStructure.CAN_BS1=CAN_BS1_9tq;

  CAN_InitStructure.CAN_BS2=CAN_BS2_8tq;

  CAN_InitStructure.CAN_Prescaler=200;

  CAN_Init(&CAN_InitStructure);

  /* CAN filter init */

  CAN_FilterInitStructure.CAN_FilterNumber=0;

  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;

  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit;

  CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;

  CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;

  CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;

  CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;

  CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;

  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;

  CAN_FilterInit(&CAN_FilterInitStructure);

  }

  注意//#define CAN_BS1_3tq ((uint8_t)0x02) /*!< 3 time quantum */

  撥特率10K,公式:72MHZ/2/200/(1+9+8)=0.01,即10K,和SJA1000測試通過

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

  120歐姆電阻要加上!!!

  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);

  總結(jié)一下

  Fpclk=36M 時(shí) 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總線波特率設(shè)置為250k

  在官方的can例程上 給出了100k 查詢 和500k 中斷方式的例子 分別設(shè)置如下:

  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時(shí)鐘是RCC_APB1PeriphClock,你要注意CAN時(shí)鐘頻率

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

  如果CAN時(shí)鐘為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數(shù)目 = 1 (固定SYNC_SEG) + 8 (BS1) + 7 (BS2) = 16

  如果CAN時(shí)鐘是 8 MHz : (8M / 1 ) / 16 = 500K

  其中:

  1 為分頻系數(shù)

  16 為每一位的Tq數(shù)目

  為了設(shè)置為 100K, 把分頻系數(shù)改為5即可, BS1 BS2 不變

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

  如果CAN時(shí)鐘是 8 MHz : (8M / 5 ) / 16 = 100K

  如果想得到 1M 的波特率, CAN時(shí)鐘仍然是 8 MHz的情況下, 分頻系數(shù)不變

  應(yīng)該改變 BS1 BS2

  CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;

  CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;

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

  如果CAN時(shí)鐘是 8 MHz : (8M / 1 ) / 8 = 1000K

  另外盡可能的把采樣點(diǎn)設(shè)置為 CiA 推薦的值:

  75% when 波特率 > 800K

  80% when 波特率 > 500K

  87.5% when 波特率 <= 500K

  所以對于 100K 的波特率(假定使用 8MHz 時(shí)鐘)

  可以修改該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 時(shí)鐘)

  可以修改該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 時(shí)鐘)

  可以修改該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%

  上邊這個(gè)公式算出來的就是CAN的速率了.



關(guān)鍵詞: can波特率計(jì)

評論


技術(shù)專區(qū)

關(guān)閉