4 years, 10 months ago.

Need help with CAN Bus code

Hello. I have tried several of the mbed CAN Bus examples without success. I simply want to send a byte from the CAN transmitter. The byte comes from a counter in the loop that should send a new CAN message every 300 msec. (I printed info to TeraTerm to test the code and commented out the printf statements for a real run. See sample at the end of this post.)

Here's the simple code I created:

#include "mbed.h"
 
DigitalOut led2(LED2);
DigitalOut led4(LED4);
CAN can1(p30, p29); //CAN Transmitter
char counter = 0;
Serial pc(USBTX, USBRX); // tx, rx

int main()
{
    pc.baud(9600);
    can1.frequency(125000);
    can1.reset();
    
    while(1)
    {
        //pc.printf("In while loop\n\r");
        led2 = !led2; 
        wait(0.3);
        //pc.printf("Sending\n\r");
        if (can1.write(CANMessage(0x139, &counter, 1)))
            {
               led2 = !led2; 
               //pc.printf("Message sent: %d\n\r", counter);
               counter++;
            }
        else
            {
                led4 = !led4;
            }                   
    }
}

I do see a burst of pulses every 300 msec, but they look nothing like a CAN message. I see a long logic-0 pulse followed by 15 shorter logic-0 pulses in each "message." My logic-analyzer bit rate and the bit rate in the code match. I can see the same signals on a scope. I've also included the terminal output that shows what the program produces when I use the print statements, so I know the counter increments, although I don't see CAN messages.

I'm lost at this point and would appreciate help. Thanks.

Message sent: 30

In while loop

Sending

Message sent: 31

In while loop

Sending

Message sent: 3 2 In while loop

Sending

etc...

/media/uploads/microguy/img.jpg

I get the same output signal when I run the AIN2CAN program. Jon

posted by Jon Titus 11 Jan 2015
Comment on this question

2 Answers

4 years, 10 months ago.

Have a look at this AP:

http://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf

This is the simplest way to comunicate on CAN bus without a transciever (TJA1040 or similar).

You should use both CAN channels on the MBED and try to send from node 1 to node 2. Otherwise the controller will abort the transmission. You can check this with rderror() and tderror(). If every thing is OK, these two counters equals zero, otherwise they count faults on the bus.

Thanks for your help Gorazd. I appreciate your suggestion and will give it a try later today. I had success getting a short bus set up and operating, so now I'll move on to trying the loop-back test you suggest. Cheers.

posted by Jon Titus 13 Jan 2015
4 years, 10 months ago.

Have you wired up the CAN driver and terminating resistors. CAN is a multi masterbus and it checks to see if the signal on the bus matches the transmission and aborts when this is not the case. That analyser picture looks like aborted messages. When the driver is missing you could try a resistor between tx and rx.

Thanks, Wim. I must remember how the CAN bus works and not get its operation confused with a normal serial bus. I used two TI SN65HVD215 drivers and the CAN signals look right. Now I'll put a receiver on the bus and monitor activity. Again, thanks for your help.

posted by Jon Titus 13 Jan 2015