Recently changed pages
mbed interface mbed interface
tag interface, mbed
Homepage Homepage
Compiler Tour Compiler Tour
Media Media
Serial Serial
Terminals Terminals
SerialPC SerialPC
From the mbed microcontroller Handbook.

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

CANMessage
Functions
CANMessageCreates empty CAN message.
CANMessageCreates CAN message with specific content.
CANMessageCreates CAN remote message.
Variables
idThe message id.
dataSpace for 8 byte payload.
lenLength of data in bytes.
formatDefines if the message has standard or extended format.
typeDefines the type of a message.
CANA can bus client, used for communicating with can devices
Functions
CANCreates an CAN interface connected to specific pins.
frequencySet the frequency of the CAN interface
writeWrite a CANMessage to the bus.
readRead a CANMessage from the bus.
resetReset CAN interface.
monitorPuts or removes the CAN interface into silent monitoring mode
rderrorReturns number of read errors to detect read overflow errors.
tderrorReturns number of write errors to detect write overflow errors.
attachAttach a function to call whenever a CAN frame received interrupt is generated.
class CANMessage : public CAN_Message
CANMessage()
Creates empty CAN message.
unsigned int id
The message id.
unsigned char data[8]
Space for 8 byte payload.
unsigned char len
Length of data in bytes.
CANFormat format
Defines if the message has standard or extended format.
CANType type
Defines the type of a message.
class CAN : public Base
A can bus client, used for communicating with can devices
CAN(PinName rd,
PinName td)
Creates an CAN interface connected to specific pins.
int frequency(int hz)
Set the frequency of the CAN interface
int write(CANMessage msg)
Write a CANMessage to the bus.
int read(CANMessage &msg)
Read a CANMessage from the bus.
void reset()
Reset CAN interface.
void monitor(bool silent)
Puts or removes the CAN interface into silent monitoring mode
unsigned char rderror()
Returns number of read errors to detect read overflow errors.
unsigned char tderror()
Returns number of write errors to detect write overflow errors.
void attach(void (*fptr)(void))
Attach a function to call whenever a CAN frame received interrupt is generated.

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.

Resources




calendar Page history
Last modified 21 Jul 2010, by user avatar Dan Ros   tag No tags | 1 reply     Share: Digg Tweet This

1 comment on CAN:

2 weeks, 3 days ago

I'm glad so there is one more CAN in mbed, but you could repair that picture :) http://mbed.org/media/uploads/simon/mbedmicrocontrollerpinout4.png There is drawn only one CAN

Please login to post comments.