-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp2515_sender.ino
47 lines (41 loc) · 1.65 KB
/
mcp2515_sender.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <CAN.h>
void setup() {
int cs=53;
int irq = 2;
int rx = 4;
int tx = 5;
Serial.begin(9600);
while (!Serial);
CAN.setPins(cs, irq);
// CAN.setPins(rx, tx)
Serial.println("CAN Sender");
// Start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// Send packet: id is 11 bits, packet can contain up to 8 bytes of data
Serial.print("Sending packet ... ");
CAN.beginPacket(0x12); // Start building a packet with ID 0x12
CAN.write('h'); // Write character 'h' to the packet
CAN.write('e'); // Write character 'e' to the packet
CAN.write('l'); // Write character 'l' to the packet
CAN.write('l'); // Write character 'l' to the packet
CAN.write('o'); // Write character 'o' to the packet
CAN.endPacket(); // Send the packet
Serial.println("done");
delay(1000);
// Send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
Serial.print("Sending extended packet ... ");
CAN.beginExtendedPacket(0xabcdef); // Start building an extended packet with ID 0xabcdef
CAN.write('w'); // Write character 'w' to the packet
CAN.write('o'); // Write character 'o' to the packet
CAN.write('r'); // Write character 'r' to the packet
CAN.write('l'); // Write character 'l' to the packet
CAN.write('d'); // Write character 'd' to the packet
CAN.endPacket(); // Send the packet
Serial.println("done");
delay(1000);
}