為什麼選擇CanBus?
CAN-BUS是一種常見的工業總線,因為它的行程距離長,通訊速度中等可靠性。它常見於現代工具機上,例如汽車診斷總線。
在本教程中,我將使用SeedStudio CanBus模塊。它與SPI接口配合使用,並且添加了OBD-II轉換器電纜並導入了OBD-II庫,您可以構建板載診斷設備或數據記錄器。
硬體概述
Arduino Uno R3
CanBus模塊
DB9接口
V_OBD
LED指示燈
終端 - CanH,CanL
Arduino Uno Pinout
串行槽連接器
I2C槽連接器
ICSP引腳
收發
結果
可以總線消息
讓我向您解釋一下CanBus消息。每條消息都包含一個ID和一些數據。 Id的起始位置為0x000,十六進位為0x7FF或十進位為0至2047.
每條消息的數據可以是1到8個字節, 每個字節的值可以是0到255之間的值。
CAN總線可以以高達1 Mbit/s的幾種不同速度運行。典型速率為100 kbit/s,125 kbit/s和500 kbit/s。較慢的速率允許更長的總線。 總線上的所有設備必須以相同的速度傳輸。
Arduino代碼
讓我們開始編寫代碼。我將簡單地將一個電位計數據和按鈕數據發送到CanBus模塊上的另一個Arduino。確保你使用一個Arduino作為主(發件人)而另一個作為奴隸(接收者)。
你應該從這裡下載CanBus庫。
現在我們開始編寫 Master (發件人) 代碼。
//Canbus Send Data (MASTER)
#include
#include
#include
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
int potPin = A0;
int btnPin = 8;
int potValue = 0;
int cantxValue = 0;
int btnValue = 0;
我們包括 和用於Canbus模塊的 庫。 「const int SPI_CS_PIN = 10;」MCP_CAN CAN(SPI_CS_PIN);「 代碼用於初始化模塊。最後我們添加變量。 potPin,potValue,cantxValue 整數用於讀取和發送電位計值和 btnPin,btnValue 整數用於讀取和發送按鈕值
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // baudrate 500kbps
{
Serial.println(「CAN BUS Shield init fail」);
Serial.println(「Init CAN BUS Shield again」);
delay(100);
}
Serial.println(「CAN BUS Initialisation Succesful!」);
}
在設置功能中,我們啟動Serial Comminication並檢查模塊是否正常工作。
void loop()
{
potValue = analogRead(potPin);
btnValue = digitalRead(btnPin);
cantxValue = map(potValue,0,1025,0,255);
Serial.print(「cantxValue: 」);
Serial.println(cantxValue);
Serial.print(「btnValue: 」);
Serial.println(btnValue);
//Create CanBus data pack
unsigned char canMsg[8] = {cantxValue, btnValue, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//Send Data Construction: id = 0x07B --- standart Flame --- data lenght = 8 ---- stmp:data buf
CAN.sendMsgBuf(0x07B, 0, 8, canMsg);
delay(100);
}
在循環功能中,我們讀取了電位計值和按鈕值。我已經提到消息值應該在0到255之間。如你所知,potensiometer值可能在0 - 1025之間。因此我們將potensiometer值從0-1025映射到0-255,代碼為「cantxValue = map(potValue,0,1025,0,255);「。
我們是串行列印值,以檢查everthing工作正常。然後我們創建一個數據包來發送Slaver(Receiver)。數據包可以是8個字節,但我們只使用2個字節。
創建數據包後,我們將它傳遞給Slaver。 「CAN.sendMsgBuf(0x07B,0,8,canMsg);」命令有4個參數,我在代碼中解釋過。
多數,我們將數據發送給Slaver。你可以在和看到完整的代碼。
現在,讓我們來吧寫 Slaver(接收器)代碼 來讀取我們的數據。
// CAN-BUS Receive Data
#include 「mcp_can.h」
#include
#include
#define INT8U unsigned char
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
INT8U len = 0;
INT8U buf[8];
unsigned char canId;
char str[20];
int btnvalue;
int potvalue;
再次,我們正在添加必要的庫。我們創建變量以協助收到數據。
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // canbus baudrate 500kbps
{
Serial.println(「CAN BUS Shield init fail!!!」);
Serial.println(「Init CAN BUS Shield again.。.」);
delay(100);
}
Serial.println(「CAN BUS Initialisation Succesful」);
}
我們再次檢查模塊是否正常工作。
void loop()
{
while (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
canId = CAN.getCanId();
potvalue = buf[0];
btnvalue = buf[1];
}
Serial.print(「 Potensiometer Value : 」);
Serial.print(potvalue);
Serial.print(「 Button Value : 」);
Serial.println(btnvalue);
}
}
在循環函數中,我們檢查是否有任何帶代碼的消息「while(CAN_MSGAVAIL == CAN.checkReceive())」如果有任何接收數據,我們會讀取發送方CanBus ID,並將緩衝區分配給要在函數中使用的變量。然後我們串行列印值以檢查它們是否正確。
打開APP閱讀更多精彩內容聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容圖片侵權或者其他問題,請聯繫本站作侵刪。 侵權投訴