|
| 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 | +} |
0 commit comments