This is an illustration how CAN (Controller Area Network) protocol interface works with Microcontroller. CAN protocol is methodology of communication between electronics devices, commonly found in Automotive, these device is the responsible for the car engine management system, Gear Control, Active Suspension, ABS, Lightning and Control, Air Condition, Air Bags, Security System, Central Locking, ETC. As you can see below diagram its uses 2 microcontroller and 2 CAN module here you will see how microcontroller interface with CAN (MCP2515) and drive as 2 wire RX/TX base data communication.
The MCP2515 is a stand-alone CAN Controller Area Network that implements the CAN specification v2.0B. These device is capable of transmitting and receiving both standard and extended data/remote frames and it has standard SPI (Serial Peripheral Interface). MCP2515 has 2 acceptance mask and 6 acceptance filters that are used to filter out unwanted messages, reducing the host MCU overhead.
Required Component
- 2x Arduino Micro-controller
- 2x MCP2515 Module
- Jumper Wires / DuPont Wires
- Solder-less Breadboard
Importing the required code libraries
Test Code / Test Sketch Code
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 |
// demo: CAN-BUS Shield, receive data with check mode // send data coming to fast, such as less than 10ms, you can use this way #include <SPI.h> #include "mcp_can.h" // the cs pin of the version after v1.1 is default to D9 // v0.9b and v1.0 is default D10 const int SPI_CS_PIN = 10; // Default is 9 MCP_CAN CAN(SPI_CS_PIN); // Set CS pin void setup() { Serial.begin(115200); // Set baud rate 115200 START_INIT: if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k { Serial.println("14CORE | CAN BUS TEST CODE"); Serial.println("CAN BUS MODULE IS RUNNING"); Serial.println(".........................."); } else { Serial.println("CAN BUS MODULE NOT RESPONDING"); Serial.println("INITIALIZING CAN MODULE PLEASE WAIT..."); delay(100); goto START_INIT; } } void loop() { unsigned char len = 0; unsigned char buf[8]; if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming { CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf unsigned char canId = CAN.getCanId(); Serial.println("-----------------------------"); Serial.println("get data from ID: "); Serial.println(canId); for(int i = 0; i<len; i++) // print the data { Serial.print(buf[i]); Serial.print("\t"); } Serial.println(); } } |
Downloads