Skip to content

Commit 027e8f3

Browse files
committed
add Module
1 parent a1b9bae commit 027e8f3

File tree

35 files changed

+62810
-0
lines changed

35 files changed

+62810
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Please add MCP_CAN_LIB to your library first........
3+
MCP_CAN_LIB file in M5stack lib examples -> modules -> COMMU -> MCP_CAN_lib.rar
4+
*/
5+
6+
#include <M5Core2.h>
7+
#include <mcp_can.h>
8+
#include "m5_logo.h"
9+
10+
/**
11+
* variable for loop
12+
*/
13+
14+
byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
15+
16+
17+
/**
18+
* variable for CAN
19+
*/
20+
long unsigned int rxId;
21+
unsigned char len = 0;
22+
unsigned char rxBuf[8];
23+
char msgString[128]; // Array to store serial string
24+
25+
#define CAN0_INT 2 // Set INT to pin 2
26+
MCP_CAN CAN0(27); // Set CS to pin 27
27+
28+
void init_can();
29+
void test_can();
30+
31+
void setup() {
32+
M5.begin(true,true,false,true,kMBusModeOutput);
33+
// kMBusModeOutput,powered by USB or Battery
34+
// kMBusModeInput,powered by outside input
35+
36+
Serial.begin(9600);
37+
Serial2.begin(9600, SERIAL_8N1, 13, 14);
38+
// The 13(RX), 14(TX) pins of the CORE2 correspond to the 16(RX), 17(TX) pins of the COMX
39+
//Please make sure that the dialing switch of COMX is set to 16(RX), 17(TX).
40+
41+
M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
42+
delay(500);
43+
M5.Lcd.setTextColor(BLACK);
44+
// M5.Lcd.setTextSize(1);
45+
46+
init_can();
47+
Serial.println("Test CAN...");
48+
}
49+
50+
void loop() {
51+
if(M5.BtnA.wasPressed())
52+
{
53+
M5.Lcd.clear();
54+
M5.Lcd.printf("CAN Test A!\n");
55+
M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
56+
init_can();
57+
Serial.println("Test CAN...");
58+
}
59+
test_can();
60+
M5.update();
61+
}
62+
63+
void init_can(){
64+
M5.Lcd.setTextSize(1);
65+
M5.Lcd.setCursor(0, 10);
66+
M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
67+
delay(500);
68+
69+
M5.Lcd.printf("CAN Test A!\n");
70+
M5.Lcd.printf("Receive first, then testing for sending function!\n");
71+
72+
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
73+
if(CAN0.begin(MCP_ANY, CAN_1000KBPS, MCP_8MHZ) == CAN_OK)
74+
Serial.println("MCP2515 Initialized Successfully!");
75+
else
76+
Serial.println("Error Initializing MCP2515...");
77+
78+
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
79+
80+
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
81+
82+
Serial.println("MCP2515 Library Receive Example...");
83+
}
84+
85+
void test_can(){
86+
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
87+
{
88+
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
89+
90+
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
91+
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
92+
else
93+
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
94+
95+
Serial.print(msgString);
96+
M5.Lcd.printf(msgString);
97+
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
98+
sprintf(msgString, " REMOTE REQUEST FRAME");
99+
Serial.print(msgString);
100+
} else {
101+
for(byte i = 0; i<len; i++){
102+
sprintf(msgString, " 0x%.2X", rxBuf[i]);
103+
Serial.print(msgString);
104+
M5.Lcd.printf(msgString);
105+
}
106+
}
107+
M5.Lcd.printf("\n");
108+
Serial.println();
109+
}
110+
}

examples/Module/COMMU/CAN/commu_can_receiver/m5_logo.h

Lines changed: 9603 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
please add MCP_CAN_LIB to your library first........
3+
MCP_CAN_LIB file in M5stack lib examples -> modules -> COMMU -> MCP_CAN_lib.rar
4+
*/
5+
6+
#include <M5Core2.h>
7+
#include <mcp_can.h>
8+
#include "m5_logo.h"
9+
10+
/**
11+
* variable for loop
12+
*/
13+
14+
byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
15+
16+
/**
17+
* variable for CAN
18+
*/
19+
long unsigned int rxId;
20+
unsigned char len = 0;
21+
unsigned char rxBuf[8];
22+
char msgString[128];
23+
24+
#define CAN0_INT 2 // Set INT to pin 2
25+
MCP_CAN CAN0(27); // Set CS to pin 27
26+
27+
void init_can();
28+
void test_can();
29+
30+
31+
void setup() {
32+
M5.begin(true,true,false,true,kMBusModeOutput);
33+
// kMBusModeOutput,powered by USB or Battery
34+
// kMBusModeInput,powered by outside input
35+
36+
Serial.begin(9600);
37+
Serial2.begin(9600, SERIAL_8N1, 13, 14);
38+
// The 13(RX), 14(TX) pins of the CORE2 correspond to the 16(RX), 17(TX) pins of the COMX
39+
//Please make sure that the dialing switch of COMX is set to 16(RX), 17(TX).
40+
41+
M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
42+
delay(500);
43+
M5.Lcd.setTextColor(BLACK);
44+
// M5.Lcd.setTextSize(1);
45+
46+
init_can();
47+
Serial.println("Test CAN...");
48+
}
49+
50+
void loop() {
51+
if(M5.BtnA.wasPressed())
52+
{
53+
M5.Lcd.clear();
54+
M5.Lcd.printf("CAN Test B!\n");
55+
M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
56+
init_can();
57+
}
58+
test_can();
59+
M5.update();
60+
}
61+
62+
void init_can(){
63+
M5.Lcd.setTextSize(1);
64+
M5.Lcd.setCursor(0, 10);
65+
M5.Lcd.pushImage(0, 0, 320, 240, (uint16_t *)gImage_logoM5);
66+
M5.Lcd.printf("CAN Test B!\n");
67+
68+
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
69+
if(CAN0.begin(MCP_ANY, CAN_1000KBPS, MCP_8MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
70+
else Serial.println("Error Initializing MCP2515...");
71+
72+
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
73+
}
74+
75+
void test_can(){
76+
// send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
77+
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
78+
if(sndStat == CAN_OK){
79+
Serial.println("Message Sent Successfully!");
80+
M5.Lcd.printf("Message Sent Successfully!\n");
81+
} else {
82+
Serial.println("Error Sending Message...");
83+
M5.Lcd.printf("Error Sending Message...\n");
84+
}
85+
delay(200); // send data per 200ms
86+
}

0 commit comments

Comments
 (0)