5 years, 5 months ago.

CAN Basics

Hello,

I'm new to using CAN so I thought I would start with a basic program (see below). I am using an EA LPC4088 and with a SN65HVD230 CAN Board wired to each CAN port. The following is the output from the program and what I don't understand is the CAN 1 tderror value and why there a two received messages for each message sent.

Any help would be appreciated.

Regards,

Tom Doyle

Program Output:

Message sent: 0

CAN1 rderror: 0, tderror: 127

CAN2 rderror: 0, tderror: 0

Message received: 0

Message received: 0

Message sent: 1

CAN1 rderror: 0, tderror: 127

CAN2 rderror: 0, tderror: 0

Message received: 1

Message received: 1

Message sent: 2

CAN1 rderror: 0, tderror: 127

CAN2 rderror: 0, tderror: 0

Message received: 2

Message received: 2

Message sent: 3

CAN1 rderror: 0, tderror: 127

CAN2 rderror: 0, tderror: 0

Message received: 3

Message received: 3

Program:

#include "mbed.h"
 
Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10);
CAN can2(p34, p33);
char counter = 0;

void send()
{
    if (can1.write(CANMessage(1337, &counter, 1)))
    {
        printf("Message sent: %d\r\n", counter);
        counter++;
    }
    
    printf("CAN1 rderror: %d, tderror: %d\r\n", can1.rderror(), can1.tderror() );
    printf("CAN2 rderror: %d, tderror: %d\r\n", can2.rderror(), can2.tderror() );
    
    led1 = !led1;
}
 
int main()
{
    ticker.attach(&send, 1);
    CANMessage msg;

    while(1)
    {
        if (can2.read(msg))
        {
            printf("Message received: %d\r\n", msg.data[0]);
            led2 = !led2;
        }

        wait(0.2);
    }
}

1 Answer

5 years, 5 months ago.

Hi Tom,

[Suggestion: just as you wrapped the code in the <<code>><</code>> tags, if you do this for the "listing", it may compress and be easier to read.]

Are you wired like in Handbook - CAN?

You mention you are new to CAN - I put together a very simple CAN - getting started page. You may already be well beyond that page.

Now, to your issue, I don't know why you are printing 2 received messages, unless one is related to the error in your transmit. So, focusing on that first, I wonder if you have everything wired correctly. I suspect the transmitter (can1) is not seeing the acknowledge from the receiving node (can2). This could be due to a wiring error between the transceiver and the mbed, or perhaps lack of termination between CAN H and CAN L. Since it appears you actually are receiving the message, this is even more perplexing.

I'm not familiar with the EA LPC4088, so I also wonder if there is something else involved in that. In a quick scan for that platform, this would not seem to be the case.

Each of your SN65HVD230 CAN Boards is pretty low power. Do they draw their power from your EA LPC4088, or from another 3.3v power source? If you are on a breadboard, noise or current spikes may be killing the quality of the communication. Maybe you can add a small filter cap between the 3.3v and ground pins as close to the SN64HVD230 as you can get.

I also came across this post, which may be relevant - they also had errors in their transmit counters.

Thanks for the reply. I think the wiring is correct. I was thinking as you did that wiring may be an issue with the TX error. However, I swapped the rx and tx (via the pin assignments) and the tx error was still there.

I have ordered different transceivers (MCP2551) and will give them a try. I am also going to dig into the MBED code via LPCXpresso to see what I can find.

Regards,

Tom

posted by Tom Doyle 03 Jul 2014