Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Teensy 4.0 and CAN using SN65HVD230

  1. #1
    Junior Member
    Join Date
    Apr 2017
    Posts
    11

    Teensy 4.0 and CAN using SN65HVD230

    Hello everyone. I'm having trouble switching from 5V to 3.3V logic for CAN transceivers. I'm currently using MCP2551s but the 'problem' is that it needs a lot of cabling as I need to drive the voltage down using logic converters. And so I've decided to use a SN65HVD230. I've built up my testing rig and .... *pfffff*. Nothing. I've set up my Teensy 4.0 to use two CAN controllers CAN 1 and CAN 2 (I'm actually driving three but I don't thing that matters here). CAN 2 is connected to the MCP2551 via a logic converter and the SN65HVD230 is connected to CAN1.
    I connect a CAN-2-USB converted to a PC and see if I'm receiving a message.
    Teensy creates an identical message for both buses with the exception that CAN 1 has a message with ID=1 and CAN 2 has a message with ID=2.
    When reading CAN buses I can see CAN 2 messages but not CAN 1.
    Can someone tell me what am I doing wrong? Maybe it has something to do with the library? I'm using a FlexCAN_T4. Maybe I've screwed up my connections? Or maybe the SN65HVD230 just doesn’t play well with Teensy 4.0, though I think I did see examples of people getting it to work.
    If anyone could help me, I'd appreciate it.

    Cheers,
    Rafał Typiak


    Code below
    Code:
    #include <FlexCAN_T4.h>
    
    
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> myCan1;
    FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> myCan2;
    FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> myCan3;
    
    void setup() {
      // put your setup code here, to run once:
    myCan1.begin();
    myCan2.begin();
    myCan3.begin();
    myCan1.setBaudRate(250*1000);
    myCan2.setBaudRate(250*1000);
    myCan3.setBaudRate(250*1000);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    CAN_message_t msg;
    msg.len=8;
    msg.id=1;
    msg.buf[0]=1;
    msg.buf[1]=2;
    msg.buf[2]=3;
    msg.buf[3]=4;
    msg.buf[4]=5;
    msg.buf[5]=6;
    msg.buf[6]=7;
    msg.buf[7]=8;
    
    myCan1.write(msg);
    msg.id=2;
    myCan2.write(msg);
    msg.id=3;
    myCan3.write(msg);
     delay (500);
    
    }
    Connection diagram below

    Click image for larger version. 

Name:	schemat.png 
Views:	40 
Size:	351.3 KB 
ID:	23100
    Click image for larger version. 

Name:	schemat2.png 
Views:	34 
Size:	779.5 KB 
ID:	23101
    Last edited by Rafal_Typiak; 01-05-2021 at 02:28 PM.

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,610
    toggle a led in your loop() to make sure the loop() is still running, also check your CAN lines are terminated

  3. #3
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,610
    Also you may wanna look at the first post here:

    https://forum.pjrc.com/threads/51870...30-transciever!

  4. #4
    Junior Member
    Join Date
    Apr 2017
    Posts
    11
    toggle a led in your loop() to make sure the loop() is still running, also check your CAN lines are terminated
    I could do that but I don't think that's necessary as I know that the loop is running. I am getting CAN messages on CAN 2, just not CAN 1. I'm reading from both at the same time.
    I've modified my board a bit by adding goldpins on Teensy's Rx and Tx for CAN 1 and CAN 2. During operation I've switched between both bus controller outputs and found that the MCP2551 can read data coming from both controllers. It outputs ID 1 frames when connected to CAN 1 and ID 2 frames when connected to CAN 2. The SN65HVD230 - nothing.

    Also you may wanna look at the first post here:
    https://forum.pjrc.com/threads/51870...30-transciever!
    I've also taken a look at the post you've shown but haven't found anything useful there. I've tried several libraries so far, but the one by teachop is sadly for Teensy 3.2. I haven't found any info on T4.0 support.
    If anyone else has any ideas, I'd be grateful.

    Cheers,
    Rafał Typiak

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,610
    Note alot of google results show issue with your transceiver. I am using all 3 CAN busses in my project. One of the issues explained in that post is that transceiver being able to receive but not transmit. If there is an issue with transmit then there can be no ACK, and thus, you will not receive a frame

  6. #6
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    500
    Made a quick setup using a Teensy 4.0 and 2 of those cheap TI SN65HVD230 CAN-bus transceiver modules.
    This is the setup:

    Click image for larger version. 

Name:	IMG_20210107_121346.jpg 
Views:	18 
Size:	94.4 KB 
ID:	23134
    I connected the CANL & CANH signals from both transceivers to a CANbus analyzer [the yellow & green wires].

    This is the transceiver module:

    Name:  TI SN65HVD230 CAN-bus transceiver module.png
Views: 86
Size:  96.0 KB

    With this code...
    Code:
    // https://forum.pjrc.com/threads/65733-Teensy-4-0-and-CAN-using-SN65HVD230
    
    #include <FlexCAN_T4.h>
    
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> myCan1;
    FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> myCan2;
    FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> myCan3;
    
    void setup() {
      // put your setup code here, to run once:
      pinMode(LED_BUILTIN, OUTPUT);          // added to show loop() activity 
      myCan1.begin();
      myCan2.begin();
      myCan3.begin();
      myCan1.setBaudRate(250 * 1000);
      myCan2.setBaudRate(250 * 1000);
      myCan3.setBaudRate(250 * 1000);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      digitalToggle(LED_BUILTIN);          // added to show loop() activity 
      CAN_message_t msg;
      msg.len = 8;
      msg.id = 1;
      msg.buf[0] = 1;
      msg.buf[1] = 2;
      msg.buf[2] = 3;
      msg.buf[3] = 4;
      msg.buf[4] = 5;
      msg.buf[5] = 6;
      msg.buf[6] = 7;
      msg.buf[7] = 8;
    
      myCan1.write(msg);
    
      msg.id = 2;
      myCan2.write(msg);
    
      msg.id = 3;
      myCan3.write(msg);
      delay (500);
    }
    ... I saw this on the CAN bus analyzer:

    Click image for larger version. 

Name:	CANanalyzer.png 
Views:	27 
Size:	52.2 KB 
ID:	23136

    The bottom-right window shows the frames with alternating frameIDs and correct timing.

    So the cheapo transceivers seem to work well with Teensy 4 and the FlexCAN_T4 library. I guess you need to dig into the MCP2551-based transceiver and its wiring.

    Regards,
    Paul

  7. #7
    Junior Member
    Join Date
    Apr 2017
    Posts
    11
    Hi guys,
    sorry for the delay but COVID makes things so hectic at work that it's hard to find the energy for anything more.
    So I've tried a new setup and I still keep failing.
    Here's my new setup. It's a completely new Teensy (the transceivers are the same).
    Click image for larger version. 

Name:	20210113_134250.jpg 
Views:	17 
Size:	132.1 KB 
ID:	23249
    Click image for larger version. 

Name:	20210113_134256.jpg 
Views:	14 
Size:	133.9 KB 
ID:	23250
    Click image for larger version. 

Name:	20210113_134301.jpg 
Views:	8 
Size:	124.6 KB 
ID:	23251
    Click image for larger version. 

Name:	20210113_134310.jpg 
Views:	10 
Size:	122.3 KB 
ID:	23252
    Click image for larger version. 

Name:	20210113_134320.jpg 
Views:	9 
Size:	123.2 KB 
ID:	23253

    I've used the following code:
    Code:
    #include <FlexCAN_T4.h>
    
    
    const int ledPin = 13;
    
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> myCan1;
    FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> myCan2;
    FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> myCan3;
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      pinMode(ledPin, OUTPUT);
      
    myCan1.begin();
    myCan2.begin();
    myCan3.begin();
    myCan1.setBaudRate(250*1000);
    myCan2.setBaudRate(250*1000);
    myCan3.setBaudRate(250*1000);
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    CAN_message_t msg, rmsg;
    msg.len=8;
    msg.id=1;
    msg.buf[0]=1;
    msg.buf[1]=2;
    msg.buf[2]=3;
    msg.buf[3]=4;
    msg.buf[4]=5;
    msg.buf[5]=6;
    msg.buf[6]=7;
    msg.buf[7]=8;
    
    myCan1.write(msg);
    msg.id=2;
    myCan2.write(msg);
    msg.id=3;
    myCan3.write(msg);
    
     if ( myCan1.read(rmsg) ) 
     {
     Serial.print("CAN1 "); 
     Serial.print("  ID: 0x"); Serial.print(rmsg.id, HEX );
     }
     if ( myCan2.read(rmsg) ) 
     {
     Serial.print("CAN2 "); 
     Serial.print("  ID: 0x"); Serial.print(rmsg.id, HEX );
     }
     Serial.println ("Hi!");
    
    digitalWrite(ledPin, !digitalRead(ledPin));
    delay (500);
    }
    Neither controller returns a message information.
    What am I doing wrong? Can it be the library? I know that this library works with the MCP2551 unit as I was able to send CAN data from a PC to Teensy using this lib.
    Wiring seems ok. I've used full rod cables previously but have switched to those seen in the photos as the original ones weren't colour coded.

    Do I need to take out the oscilloscope? If so what should I be looking for? Should data transfers bee only visible on Tx when there are no CAN_L and CAN_H cables connected?

    Thanks for any help you can spare.

    Cheers,
    Rafał Typiak

  8. #8
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    500
    Hi Rafal,

    Saw from the photo's that you copied my setup, good, than we have the same setup hardware-wise. Your wiring seems to be OK.
    Ran your code from message #7 and everything seems to be working well!?
    Here is the serial output:

    Click image for larger version. 

Name:	Serial.png 
Views:	8 
Size:	21.2 KB 
ID:	23254

    and the CAN analyzer output:

    Click image for larger version. 

Name:	CANanalyzer.png 
Views:	11 
Size:	52.4 KB 
ID:	23255

    What version Arduino/Teensyduino are you running? I'm on Arduino 1.8.13, Teensyduino 1.53.
    Teensyduino 1.53 comes with the latest FlexCAN_T4 library. Is there perhaps an other/older FlexCAN_T4 library in your C:\Users\xxxx\Documents\Arduino\Libraries\ folder?

    Using a scope, you should see activity on the CTX & CRX pins. And on the CANL & CANH lines.

    Regards,
    Paul

  9. #9
    Junior Member
    Join Date
    Apr 2017
    Posts
    11
    Hi,
    thanks for your help.
    Yup. I wanted to recreate your setup to check why I'm failing. My Arduino is at: 1.8.13 and Teensyduino is as 1.53. I guess the first step will be to update that.
    I do also have an additional FlexCAN_T4 lib in Documents. So.... yeah..... I'll see if getting those two sorted out would change stuff.

    Thanks again!

    Cheers,
    Rafał Typiak

  10. #10
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    500
    You're welcome.
    No need to update Teensyduino 1.53, that is the latest.
    Yes, please delete any FlexCAN_T4 libraries under Documents\Arduino\Libraries\ to make sure you're using the one that came with Teensyduino. Which should be located here: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\.

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •