色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32的CAN波特率計(jì)算_轉(zhuǎn)

          STM32的CAN波特率計(jì)算_轉(zhuǎn)

          作者: 時(shí)間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
          STM32里的CAN 支持2.0A,2.0B, 帶有FIFO,中斷等, 這里主要提一下內(nèi)部的時(shí)鐘應(yīng)用.

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

          本文引用地址:http://cafeforensic.com/article/201611/316398.htm

          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測(cè)試通過

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

          120歐姆電阻要加上?。?!

          哦 確實(shí)是

          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

          所以對(duì)于 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%

          所以對(duì)于 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%

          所以對(duì)于 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è)人見解, 僅供參考。

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



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

          評(píng)論


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

          關(guān)閉