You are viewing an older revision! See the latest version
CAN
CAN or Controller-area Network is a bus standard designed to allow microcontrollers and devices to communicate with each other without a host computer.
Hello World!¶
Loopback example
#include "mbed.h" Ticker ticker; DigitalOut led1(LED1); DigitalOut led2(LED2); CAN can1(p9, p10); CAN can2(p30, p29); void send() { printf("send()\n"); char counter = 0; 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); } }
This example sends from one CAN bus (can1) an counter while it is listen on the other CAN bus (can2) to receive a packet. Each bus controller should be connected to a CAN bus transceiver. These should be connected together at a CAN bus.
API¶
API summary
Details¶
The CAN Interface can be used on mbed pins p9/p19 and p30/p29
The CAN Interface can be used to write data words out of a CAN port and will return the data received from another CAN device. The CAN clock frequency can be configured.