Mistake on this page?
Report an issue in GitHub or email us

CAN

CAN class hierarchy

Controller-Area Network (CAN) is a bus standard that allows microcontrollers and devices to communicate with each other without going through a host computer.

Note: You can use the CAN interface to write data words out of a CAN port. It will return the data received from another CAN device. You can configure the CAN clock frequency.

CAN class reference

Public Member Functions
 CAN (PinName rd, PinName td)
 Creates a CAN interface connected to specific pins. More...
 CAN (PinName rd, PinName td, int hz)
 Initialize CAN interface and set the frequency. More...
int frequency (int hz)
 Set the frequency of the CAN interface. More...
int write (CANMessage msg)
 Write a CANMessage to the bus. More...
int read (CANMessage &msg, int handle=0)
 Read a CANMessage from the bus. More...
void reset ()
 Reset CAN interface. More...
void monitor (bool silent)
 Puts or removes the CAN interface into silent monitoring mode. More...
int mode (Mode mode)
 Change CAN operation to the specified mode. More...
int filter (unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
 Filter out incoming messages. More...
unsigned char rderror ()
 Detects read errors - Used to detect read overflow errors. More...
unsigned char tderror ()
 Detects write errors - Used to detect write overflow errors. More...
void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a CAN frame received interrupt is generated. More...
template<typename T >
void attach (T *obj, void(T::*method)(), IrqType type=RxIrq)
 Attach a member function to call whenever a CAN frame received interrupt is generated. More...
template<typename T >
void attach (T *obj, void(*method)(T *), IrqType type=RxIrq)
 Attach a member function to call whenever a CAN frame received interrupt is generated. More...

CAN Hello World!

This example sends a counter from one CAN bus (can1) and listens for a packet on the other CAN bus (can2). Each bus controller should be connected to a CAN bus transceiver. These should be connected together at a CAN bus.

#include "mbed.h"

#if defined (DEVICE_CAN) || defined(DOXYGEN_ONLY)

Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
/** The constructor takes in RX, and TX pin respectively.
  * These pins, for this example, are defined in mbed_app.json
  */
CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
char counter = 0;

void send() {
    printf("send()\n");
    if(can1.write(CANMessage(1337, &counter, 1))) {
        printf("wloop()\n");
        counter++;
        printf("Message sent: %d\n", counter);
    } 
    led1 = !led1;
}

int main() {
    printf("main()\n");
    ticker.attach(&send, 1);
    CANMessage msg;
    while(1) {
        printf("loop()\n");
        if(can2.read(msg)) {
            printf("Message received: %d\n", msg.data[0]);
            led2 = !led2;
        } 
        wait(0.2);
    }
}

#else
  #error CAN NOT SUPPORTED
  
#endif

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.