0% found this document useful (0 votes)
10 views6 pages

Arduino CAN Bus Shield Lab Guide

The document outlines a lab experiment for studying the Arduino CAN-Bus shield, focusing on CAN communication. It includes procedures for connecting circuits, writing code, and performing experiments to send and receive data using CAN protocol. Additionally, it poses questions regarding CAN ID significance, baud rate effects, and differences between Standard and Extended Frames.

Uploaded by

bigbig2016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Arduino CAN Bus Shield Lab Guide

The document outlines a lab experiment for studying the Arduino CAN-Bus shield, focusing on CAN communication. It includes procedures for connecting circuits, writing code, and performing experiments to send and receive data using CAN protocol. Additionally, it poses questions regarding CAN ID significance, baud rate effects, and differences between Standard and Extended Frames.

Uploaded by

bigbig2016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course: ENG29 3123 Microcontroller (Lab) Term: 2/2567 Date: Section: .

Name: Student ID: .

Unit 9 CAN bus Shield

Objective
1.1) To study the use of Arduino CAN-Bus shield.
1.2) To applied modules for project.

Experiment 9.1 : CAN communication

Figure 9.1
Procedure
1) Connect the circuit by figure 9.1
2) Write code program 9.1, complied and upload
Code for experiment 9.1
#include <SPI.h>
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin

void CANRecvData() {
char len = 0;
unsigned char Recv[8];
if (CAN_MSGAVAIL == [Link]()) { // check if data coming
[Link](&len, Recv); // read data, len: data length, buf: data buf
long canRecvId = [Link]();
[Link]("-----------------------------");
[Link]("Get data from ID: 0x");
[Link](canRecvId, HEX);
for (int i = 0; i < len; i++) { // print the data
[Link](Recv[i], HEX);
[Link]("\t");
}
[Link]();
}
}

void CANSendData(long SendId,char len,unsigned char Msg[8]) {


// send data: id = 0x00, standrad frame, data len = 8, Msg: data buf
[Link](SendId, 0, len, Msg);
[Link]("-----------------------------");
[Link]("Send data by ID: 0x");
[Link](SendId, HEX);
for (int i = 0; i < len; i++) { // print the data
[Link](Msg[i],HEX);
[Link]("\t");
}
[Link]();
}

void setup() {
[Link](115200);
while (CAN_OK != [Link](CAN_500KBPS)) // init can bus : baudrate = 500k
{
[Link]("CAN init fail, retry...");
delay(100);
}
[Link]("CAN init ok!");
}

void loop() {
for (int i=0; i<3;i++) {
CANRecvData();
}
unsigned char Data[8]={0,100,200,255,255,200,100,0} ;
long IdData = 0xAA ; // change ID
CANSendData(IdData,8,Data);
delay(100);
}
Code for experiment 9.2
#include <SPI.h>
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin

void CANRecvData() {
char len = 0;
unsigned char Recv[8];
if (CAN_MSGAVAIL == [Link]()) { // check if data coming
[Link](&len, Recv); // read data, len: data length, buf: data buf
long canRecvId = [Link]();
[Link]("-----------------------------");
[Link]("Get data from ID: 0x");
[Link](canRecvId, HEX);
for (int i = 0; i < len; i++) { // print the data
[Link](Recv[i], HEX);
[Link]("\t");
}
[Link]();
}
}

void CANSendData(long SendId,char len,unsigned char Msg[8]) {


// send data: id = 0x00, standrad frame, data len = 8, Msg: data buf
[Link](SendId, 0, len, Msg);
[Link]("-----------------------------");
[Link]("Send data by ID: 0x");
[Link](SendId, HEX);
for (int i = 0; i < len; i++) { // print the data
[Link](Msg[i],HEX);
[Link]("\t");
}
[Link]();
}

void setup() {
[Link](115200);
while (CAN_OK != [Link](CAN_500KBPS)) // init can bus : baudrate = 500k
{
[Link]("CAN init fail, retry...");
delay(100);
}
[Link]("CAN init ok!");
}

void loop() {
for (int i=0; i<3;i++) {
CANRecvData();
}
if ([Link]()>0) {
String letter = [Link]();
unsigned char Data[] = {} ;
for (int i=0;i<8;i++) {
Data[i] = letter[i] ;
}
long IdData = 0xAA ; // change ID
CANSendData(IdData,8,Data);
}
delay(1000);
}
Result of experiment 9.1
Node 1
CAN ID :
Index 0 1 2 3 4 5 6 7
Hex
Char

Node 2
CAN ID :
Index 0 1 2 3 4 5 6 7
Hex
Char

Node 3
CAN ID :
Index 0 1 2 3 4 5 6 7
Hex
Char

Node 4
CAN ID :
Index 0 1 2 3 4 5 6 7
Hex
Char
Questions
1) CAN ID คืออะไร และมีความสำคัญอย่างไรในระบบ CAN bus?

2) การตั้งค่าความเร็วบัส (Baud Rate) ใน CAN bus มีผลต่อการสื่อสารอย่างไร?

3) อธิบายความแตกต่างระหว่าง Standard Frame และ Extended Frame ?

---------------------------------------------- End Unit 9 --------------------------------------------

You might also like