SET THE BAUD RATE¶
This function is used to initialize the baud rate of the CAN Bus system.
The available baud rates are listed as follows:
#define CAN_5KBPS 1
#define CAN_10KBPS 2
#define CAN_20KBPS 3
#define CAN_25KBPS 4
#define CAN_31K25BPS 5
#define CAN_33KBPS 6
#define CAN_40KBPS 7
#define CAN_50KBPS 8
#define CAN_80KBPS 9
#define CAN_83K3BPS 10
#define CAN_95KBPS 11
#define CAN_100KBPS 12
#define CAN_125KBPS 13
#define CAN_200KBPS 14
#define CAN_250KBPS 15
#define CAN_500KBPS 16
#define CAN_666kbps 17
#define CAN_1000KBPS 18SET RECEIVE MASK AND FILTER¶
There are 2 receive mask registers and 5 filter registers on the controller chip that guarantee you getting data from the target device. They are useful especially in a large network consisting of numerous nodes.
We provide two functions for you to utilize these mask and filter registers. They are:
Mask:
init_Mask(unsigned char num, unsigned char ext, unsigned char ulData);Filter:
init_Filt(unsigned char num, unsigned char ext, unsigned char ulData);- num represents which register to use. You can fill 0 or 1 for mask and 0 to 5 for filter.
- ext represents the status of the frame. 0 means it’s a mask or filter for a standard frame. 1 means it’s for a extended frame.
- ulData represents the content of the mask of filter.
CHECK RECEIVE¶
The MCP2515 can operate in either a polled mode, where the software checks for a received frame, or using additional pins to signal that a frame has been received or transmit completed.
Use the following function to poll for received frames.
INT8U MCP_CAN::checkReceive(void);The function will return 1 if a frame arrives, and 0 if nothing arrives.
GET CAN ID¶
When some data arrive, you can use the following function to get the CAN ID of the «send» node.
INT32U MCP_CAN::getCanId(void)RECEIVE A FRAME¶
The following function is used to receive data on the ‘receive’ node:
CAN.readMsgBuf(unsigned char len, unsigned char buf);In conditions that masks and filters have been set. This function can only get frames that meet the requirements of masks and filters.
- len represents the data length.
- buf is where you store the data.
Arduino code¶
We provide an Arduino library for CANBed.
There’re many examples for the library, which is consist of,
- OBDII-PIDs — Get data from OBD-II interface
- send — Send a frame to CAN Bus
- recv — Receive a frame from CAN Bus
- set_mask_filter_recv — Receive a frame from CAN Bus with mask and filter setting
Contact us
If you have any question, please feel free to contact support@longan-labs.cc
Db9 connector¶
The DB9 interface of CAN Bus has two different protocols, OBD and CAN Open. Their definition is as follows,
| pin# | OBD(default) | CAN OPEN |
|---|---|---|
| 1 | GND | N.C |
| 2 | GND | CAN_L |
| 3 | CANH | GND |
| 4 | N.C | N.C |
| 5 | CANL | GND |
| 6 | N.C | N.C |
| 7 | N.C | CAN_H |
| 8 | N.C | N.C |
| 9 | CAN_V | CAN_V |
If you want to use the OBD protocol, you don’t need to make any changes to the hardware.
If you need to use the CAN Open protocol, first we look at the back side of the PCB board, you can see the Pads below,
These pads are connected to the OBD side by default. We have to prepare a knife to disconnect the OBD side, and then use an electric soldering iron to solder the CAN Open side of the pads.
Examples
here are many examples implemented in this library. One of the examples is below. You can find other examples here
// SEND EXAMPLE OF SERIAL CAN MODULE// unsigned char send(unsigned long id, uchar ext, uchar rtrBit, uchar len, const uchar *buf);// SUPPORT: joney.sui@longan-labs.cc#include<Serial_CAN_Module.h>#include<SoftwareSerial.h>Serial_CAN can;
#definecan_tx2// tx of serial can module connect to D2#definecan_rx3// rx of serial can module connect to D3voidsetup()
{ Serial.begin(9600); can.begin(can_tx, can_rx, 9600); // tx, rx Serial.println("begin");
}unsignedchar dta[8] = {1, 2, 3, 4, 5, 6, 7, 8};// send(unsigned long id, byte ext, byte rtrBit, byte len, const byte *buf);voidloop()
{ can.send(0x55, 0, 0, 8, dta); // SEND TO ID:0X55delay(100);
}// END FILEFunctions
- begin()
- sendMsgBuf()
- readMsgBuf()
- readMsgBufID()
- init_Mask()
- init_Filt()
- checkReceive()
- getCanId()
- isRemoteRequest()
- isExtendedFrame
Installation
- Download the library
- Extract the zip file
- In the Arduino IDe, navigate to Sketch > Include Library > Add .ZIP Library
License
MIT License
Copyright (c) 2021 @ Longan Labs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.Part list¶
- CANBed M0 PCBA
- sub-D connector
- 4PIN Terminal
- 4PIN 2.0 Connector x 2
- 9×2 2.54 Header x 1
- 3×3 2.54 Header x 1
Pin out¶
1.9×2 IO Pin OUT:
The IO of ATSAMD21G18 is listed out here.
2.ATSAM21G18A:
Cortex M0 core controller
2.Reset Button:
Reset the on-board Atmega chip.
3.Micro USB connector for programming
4. SPI interface
5.CAN RX/TX Indicator
6.sub-D connector or Terminal for CAN Bus
D-Sub CANbus PinOut
| pin# | Signal names | Signal Description |
|---|---|---|
| 1 | Reserved | Upgrade Path |
| 2 | CAN_L | Dominant Low |
| 3 | CAN_GND | Ground |
| 4 | Reserved | Upgrade Path |
| 5 | CAN_SHLD | Shiled, Optional |
| 6 | GND | Ground,Optional |
| 7 | CAN_H | Dominant High |
| 8 | Reserved | Upgrade Path |
| 9 | CAN_V | Power, Optional |
7.Switch for the 120Ω terminal resistor for CAN Bus
If you use this board on the end of the CAN bus, please put this switch to 120Ω. For more detail about the CAN bus protocol, please refer to the
NI CAN Physical Layer and Termination Guide
8.Grove connector for UART
9.Grove connector for I2C
A.Reset Button
SEND A FRAME¶
CAN.sendMsgBuf(INT8U id, INT8U ext, INT8U len, data_buf);It is a function to send data onto the bus. In which:
- id represents where the data comes from.
- ext represents the status of the frame. ‘0’ means standard frame. ‘1’ means extended frame.
- len represents the length of this frame.
- data_buf is the content of this message.
For example, In the ‘send’ example, we have:
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
CAN.sendMsgBuf(0x00, 0, 8, stmp); //send out the message 'stmp' to the bus and tell other devices this is a standard frame from 0x00.