8 STM32 CAN 匯流排的使用

2021-07-24 23:33:40 字數 3567 閱讀 6285

can 匯流排是一種在汽車領域常見的通訊協議匯流排。stm32 支援 can 協議。這裡簡單的對 stm32 平台上 can 匯流排的使用做乙個示例,起到拋磚引玉的作用。首先明確乙個概念 :can 匯流排中不是以時鐘或者邏輯電平來表示'0'和'1'的,而是採用隱性位和顯性位來表示 :

l、隱性位與顯性位

can匯流排為」隱性」(邏輯1)時, can_h 和 can_l的電平為2.5v(電位差為0v);

can匯流排為」顯性」(邏輯0)時, can_h 和 can_l的電平為3.5v和1.5v(電位差為2.5);

2、stm32 總 can 匯流排波特率設定

can 波特率

引數設定

5 kbps

can_sjw = can_sjw_2tq

can_bs1 = can_bs1_6tq

can_bs2 = can_bs2_4tq

can_prescaler = 600

10 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 600

20 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 300

25 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 240

40 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 150

50 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 120

62.5 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 96

80 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 75

100 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 60

125 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 48

200 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 30

250 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 24

400 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_5tq

can_bs2 = can_bs2_3tq

can_prescaler = 10

500 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 12

800 kbps

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_5tq

can_bs2 = can_bs2_3tq

can_prescaler = 5

1mhz

can_sjw = can_sjw_1tq

can_bs1 = can_bs1_3tq

can_bs2 = can_bs2_2tq

can_prescaler = 6

計算公式 :hclk  / 2 / prescaler / (sjw + bs1 + bs2)

72mhz / 2 / 5 / (1+5+3) = 800khz

stm32 can 匯流排程式設計流程

1、初始化系統時鐘 2、

使能對應 gpio 埠的時鐘

3、使能can匯流排時鐘

4、使能埠復用時鐘

5、配置 gpio 埠

輸出配置為復用推挽輸出、輸入為上拉輸入

6、can匯流排配置

1) 預設初始化 can_deinit(can1);

2) 配置can_inittypedef結構體的基本引數 :主要配置工作模式與波特率。

3) 初始化 can_init(can1, &can_initstruct);

4) 配置過濾器 :選擇模式、位寬、過濾器設定、關聯的緩衝區等。

can_filterinit(&can_filterstruct);

can_itconfig(can1,can_it_fmp0, enable);  /* 配置接收中斷*/

7、中斷配置

配置通道為 :.nvic_irqchannel= usb_lp_can1_rx0_irqn;

1) cantxmsgtxmessage;  /* 傳送訊息結構體*/

2) canrxmsgrxmessage;  /* 接收訊息結構體*/

3) can_receive(can1,can_fifo0, &rxmessage); /* 接收訊息 */

4) can_transmit(can1,&txmessage);           /* 傳送訊息*/

注意 :中斷處理函式在 st 官方庫的標頭檔案中的宣告有誤

初始化** :

/* can 匯流排配置 */

void can_config()

接收中斷處理

void usb_lp_can1_rx0_irqhandler(void)

STM32的CAN匯流排除錯

1.使用stm32的can匯流排,回環程式寫好之後,在無外接收發晶元的情況下,使程式不斷發資料,使用示波器可以看到tx引腳有波形,而rx引腳沒有波形 2.模式改為正常模式,在沒有接收發晶元的情況下再次測一下tx與rx的波形,但是發現使用示波器採集不到波形,而且在發了幾次資料後會進行標準庫的斷言,一直...

stm32內部的CAN匯流排

bxcan是基本擴充套件can basic extended can 的縮寫,它支援can協議2.0a和2.0b 它的設計目標是以最小的cpu負載來高效處理大量的報文。它也支援報文傳送的優先順序要求 優先順序可軟體配置 bxcan模組可以完全自動地接收和傳送can報文,且完全支援標準識別符號 11位...

STM32CAN匯流排協議講解

月報 最近學習了stm32的can匯流排協議,使用的是野火的開發板 can 是controller area network 的縮寫 can bus 具有佈線簡單 典型的匯流排型結構 可最大限度的節約佈線與維護成本 穩定可靠 實時 抗干擾能力強 傳輸距離遠等特點,是一種成功的匯流排。不僅在汽車行業得...