CAN bus receive problem, wiring error?

22 Dec 2011

Hi,

I'm trying to test out the CAN functionality with a simple network:

/media/uploads/s1000rr/canbus.jpg

But I never get a received message. I'm using the sample code:

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
CAN can1(p9, p10);
CAN can2(p30, p29);
char counter = 0;
Ticker ticker;

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


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

I verified that CAN1 and CAN2 were properly connected to the MBED because if the RX/TX were swapped, it would only send 3-4 messages. I also swapped the send/receive for CAN1 and CAN2 (CAN1 would send CAN2 receive and vice versa) but still no luck. I also tried setting the frequency to 500,000 and not setting it. Any ideas?

Cheers, Sebastian

22 Dec 2011

Maybe because you're using high speed termination with low speed?

22 Dec 2011

Wouldn't setting the frequency to 500,000 be considered a high speed CAN bus? What termination do you suggest?

22 Dec 2011

I think your wiring is wrong.

mbed pin 9 and pin 30 are CAN receive data inputs. They should be connected to pin 4 of the MCP2551. That pin 4 is RXD output according to the datasheet.

mbed pin 29 and pin 10 are CAN transmit data outputs. They should be connected to pin 1 of the MCP2551. That pin 1 is TXD input according to the datasheet.

Hope you have not fried the mbed outputs.

22 Dec 2011

I thought about that as well, and based my wiring on the SK Pang breakout board:

/media/uploads/s1000rr/canbrk_config.png

It shows that TX has to go to RX, just like in JTAG where TDI -> TDO -> TDI etc...

But I may be wrong for following the above table....

Could it be a code problem or maybe is my MBED fried?

22 Dec 2011

Check your pictures of the board. You will notice that pin 1 of the driver IC maybe named can_rx but connects to mbed pin 29 just as I proposed. The names are mixed up somehow.

22 Dec 2011

Thanks for noticing this Wim, a fresh set of eyes is what's needed sometimes. I will try this later tonight and post the update.

Cheers, Sebastian

23 Dec 2011

Problem solved, thanks for the help Wim.

23 Dec 2011

CAN you update the drawing, and code, because I would like to get CAN working om my MBED in the next few weks !!

Cheers

Ceri

23 Dec 2011

No problem, here are the changes:

/media/uploads/s1000rr/canbus1.jpg

No changes required to the code, but you can increase the frequency if you wish. I only tested upto 500,000 which is the speed of the CAN network that I will be working with.

Cheers, Sebastian

22 Jun 2015

This is a very old post, but I'm having similar problems and have checked everything mentioned here, but I still only see "Message sent: 1" 2 and 3 being printed out. I've not been able to receive anything. If anyone would be able to take a look and provide suggestions, I would bery much appreciate it: https://developer.mbed.org/questions/54111/This-example-does-not-work-for-me-Does-a/

Thanks.