-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCANExampleLCD.ino
88 lines (72 loc) · 1.91 KB
/
CANExampleLCD.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Arduino with MCP2515 and 16x2 I2C LCD to show info from other arduinos on CAN Bus
#include <SPI.h>
#include <mcp2515.h>
#include <stdlib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define col 16
#define lin 2
#define address 0x3F
struct can_frame canMsgRcvd;
struct can_frame canMsgSend;
MCP2515 mcp2515(10);
LiquidCrystal_I2C lcd(address,col,lin);
int temp = 0;
int hum = 0;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("------- CAN Read ----------");
Serial.println("ID DLC DATA");
canMsgSend.can_id = 0x00;
canMsgSend.can_dlc = 8;
canMsgSend.data[0] = 0x00;
canMsgSend.data[1] = 0x00;
canMsgSend.data[2] = 0x00;
canMsgSend.data[3] = 0x00;
canMsgSend.data[4] = 0x00;
canMsgSend.data[5] = 0x00;
canMsgSend.data[6] = 0x00;
canMsgSend.data[7] = 0x00;
}
void loop() {
sendCANMessage(1,1);
sendCANMessage(2,1);
if (mcp2515.readMessage(&canMsgRcvd) == MCP2515::ERROR_OK) {
Serial.print(canMsgRcvd.can_id, HEX); // print ID
Serial.print(" ");
Serial.print(canMsgRcvd.can_dlc, HEX); // print DLC
Serial.print(" ");
for (int i = 0; i<canMsgRcvd.can_dlc; i++) { // print the data
Serial.print(canMsgRcvd.data[i],HEX);
Serial.print(" ");
}
Serial.println();
switch(canMsgRcvd.can_id){
case 0x04:
temp = canMsgRcvd.data[0];
break;
case 0x05:
hum = canMsgRcvd.data[0];
break;
}
lcd.setCursor(1,0);
lcd.print("Temperature = ");
lcd.print(temp);
lcd.setCursor(2,0);
lcd.print("Humidity = ");
lcd.print(hum);
delay(200);
}
}
void sendCANMessage(int canID,int value){
canMsgSend.can_id = canID;
canMsgSend.data[0] = value;
mcp2515.sendMessage(&canMsgSend);
}