話說目前都在研究CAN BUS
然後,就想到一個 [如何讓產線方便測試CAN BUS ] 的想法
----CAN BUS 測試需求----
bit rate預設為 500 K bps
發送端: 我是利用 can bus analyzer 發送 SID: 7FF , DATA 最多 8 bytes
接收端: Arduino 接收傳送端的資料
當接收到的資料為 SID: 7FF , buf[0]=1就會亮燈; SID: 7FF , buf[0]=0就會滅燈
----
於是利用手上的Arduino,額外再購買一個 CAN BUS Shield V2.0 for Arduino的板子回來
板子應該是長這樣
與arduino 結合後長這樣
---硬體設定--
因為我是用 USB供給 arduino電,所以 power switch 需要調整為 OFF
我額外在pin7接了一個LED來控制亮滅
--軟體安裝--
先到以下網站下載 [CAN BUS LIBRARY]
https://github.com/Seeed-Studio/CAN_BUS_Shield
下載後為ZIP檔案
選擇[草稿碼] - > [匯入程式庫] -> [加入 .zip 程式庫] ,去匯入下載的[CAN BUS LIBRARY]
然後開啟接收端的範例程式,選擇 [檔案]->[範例]->[CAN-BUS Shield] - > [revive_check]
因為我們的板子CS pin是用 pin 10所以必須修改一下 const int SPI_CS_PIN = 10; 為第10pin,CAN BUS SHIELD才能正常運作
整個完整的 arduino source code為
---
// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;//9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
int ledPin = 7; // led use pin 7
void setup()
{
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
pinMode(ledPin, OUTPUT);
SERIAL.println("CAN BUS Shield init ok!");
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
SERIAL.println("-----------------------------");
SERIAL.print("Get data from ID: 0x");
SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
SERIAL.print(buf[i], HEX);
SERIAL.print("\t");
}
SERIAL.println();
//add code to check CANID=7ff ,and buf[0]=1 to turn on LED,buf[0]=1 to turn on LED
if(canId==0x7ff){
if(buf[0]==0){
SERIAL.println("CANID=7ff, buf[0]=0, set led [OFF]");
digitalWrite(ledPin, LOW);
}else if(buf[0]==1){
SERIAL.println("CANID=7ff, buf[0]=1, set led [ON]");
digitalWrite(ledPin, HIGH);
}else{
//do nothing
}
}
SERIAL.println();
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
--
將CAN_L、CAN_H、LED、USB全部接上的狀態如下:
==開始測試==
傳送端發送 SID = 7FF ,第一個 DATA為 1就開啟LED燈
傳送端發送 SID = 7FF ,第一個 DATA為 0就關閉LED燈
傳送端發送 非7FF 的SID,CAN BUS SHIELD 都不會有反應
有了以上簡單的治具,就可以很簡單測試 CAN BUS的通訊了。
以下為相關資料:
CAN BUS Shield V2.0 for Arduino購買網址
CAN BUS Shield V2.0 for Arduino WIKI網址
https://wiki.dfrobot.com/CAN-BUS_Shield_V2__SKU__DFR0370_#target_5
追加如果要傳送EXTENDED ID,可以換成以下的CODE
----CAN BUS 測試需求----
bit rate預設為 1000 K bps
發送端: 我是利用 can bus analyzer 發送 SID: 1fffffff, DATA 最多 8 bytes
接收端: Arduino 接收傳送端的資料
當接收到的資料為 SID: 1fffffff, buf[0]=1就會亮燈; SID: 1fffffff, buf[0]=1就會滅燈
----
===
// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
#define EID_MAX 0x1fffffff //0x7fff
#define ENABLE_DEBUG 1 // 0
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
int ledPin = 7; // led use pin 7
void setup()
{
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_1000KBPS)) // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
pinMode(ledPin, OUTPUT);
SERIAL.println("CAN BUS Shield init ok!");
//add code to setup mask and filter
CAN.init_Mask(0,1,0);
CAN.init_Mask(1,1,0);
//set filter ,we can receive all EID
CAN.init_Filt(0,1,EID_MAX);
CAN.init_Filt(1,1,EID_MAX);
CAN.init_Filt(2,1,EID_MAX);
CAN.init_Filt(3,1,EID_MAX);
CAN.init_Filt(4,1,EID_MAX);
CAN.init_Filt(5,1,EID_MAX);
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
#if ENABLE_DEBUG
SERIAL.println("-----------------------------");
SERIAL.print("Get data from ID: 0x");
SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
SERIAL.print(buf[i], HEX);
SERIAL.print("\t");
}
SERIAL.println();
#endif
//add code to check CANID=7ff ,and buf[0]=1 to turn on LED,buf[0]=1 to turn on LED
if(canId==EID_MAX){
if(buf[0]==0){
digitalWrite(ledPin, LOW);
#if ENABLE_DEBUG
SERIAL.print("CANID=");
SERIAL.print(EID_MAX,HEX);
SERIAL.println(",buf[0]=0, set led [OFF]");
#endif
}else if(buf[0]==1){
digitalWrite(ledPin, HIGH);
#if ENABLE_DEBUG
SERIAL.print("CANID=");
SERIAL.print(EID_MAX,HEX);
SERIAL.println(",buf[0]=0, set led [ON]");
#endif
}else{
//do nothing
}
}
SERIAL.println();
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
===