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

Thread: 2 node CAN bus with SN65HVD230 tranceiver.

  1. #1

    2 node CAN bus with SN65HVD230 tranceiver.

    Good morning!

    I have to make transmitter that will send two diffferent frames, depending on whether button is pushed or not. I've managed to do that with two arduino unos
    with SeeedStudio's CAN bus shields. Library made for these shields is fairly easy to use, so it didn't take a lot of effort to make. Now I want to make same thing with teensys and that's where the problems start.

    1. I don't really understand how to use Flexcan library, so I found someone on youTube that already made bus and tried to copy some code just to start communication between nodes but it doesn't work.
    2. I need to send the frame in 83333 bps and I have no idea how to add new baud rate to the library.

    Equipment

    Windows 10
    Teensy 3.2 x2
    Arduino IDE with teensyduino

    http://www.ebay.com/itm/SN65HVD230-C...085?rmvSB=true x2
    https://www.seeedstudio.com/CAN-BUS-....2-p-2256.html

    Photos (orange-3,3V; green-GRD; red-teensyTX-canRX; blue-teensyRX-canTX; | red-CAN HIGH; green-CAN LOW)

    https://drive.google.com/open?id=0B0...1BWaFVhazllY1U
    https://drive.google.com/open?id=0B0...Xo3TnpRamNud1E

    First code - FlexCAN - sender

    Code:
    #include <FlexCAN.h>
    #include <kinetis_flexcan.h>
    
    const int baudRate = 50000;
    const int ledPin = 13;
    const int delayTime = 2000;
    
    FlexCAN myCAN(baudRate);
    
    CAN_message_t message_send;
    
    void setup() {
      pinMode(ledPin,OUTPUT);
      Serial.begin(9600);
      digitalWrite(ledPin,HIGH);
      delay(100);
      digitalWrite(ledPin,LOW);
      myCAN.begin();
    
    }
    
    void loop() {
      //if(myCAN.available())              // if avaible enabled then it doesnt work at all
      {
        Serial.println("Sending every 2s");
        
        message_send.buf[0] = 1;
        message_send.buf[1] = 2;
        message_send.buf[2] = 3;
        message_send.buf[3] = 4;
        message_send.buf[4] = 5;
        message_send.buf[5] = 6;
        message_send.buf[6] = 7;
        message_send.buf[7] = 8;
       
        
        message_send.id = 0x01;
        message_send.len = 8;
        Serial.print(message_send.id);          //printin stuff that i hope to send
        Serial.print(' ');
        Serial.println(message_send.len);
        
        for(int i = 0 ; i < message_send.len ; i++){
          Serial.print(message_send.buf[i]);
        }
        Serial.print("\n");
        
        digitalWrite(ledPin,HIGH);
        
        myCAN.write(message_send);
        
        
        delay(delayTime);
        digitalWrite(ledPin, LOW);
    
       
      }
     
    
    }
    Second code - FlexCAN - receiver

    Code:
    #include <FlexCAN.h>
    #include <kinetis_flexcan.h>
    
    
    
    const int baudRate = 50000;
    const int ledPin = 13;
    const int delayTime = 2000;
    
    FlexCAN myCAN(baudRate);
    
    CAN_message_t message_send;
    static CAN_message_t rxmsg;
    void setup() {
      pinMode(ledPin,OUTPUT);
      Serial.begin(9600);
      digitalWrite(ledPin,HIGH);
      delay(100);
      //digitalWrite(ledPin,LOW);
      myCAN.begin();
    
    }
    
    void loop() {
      //if(myCAN.available())
      {
        Serial.println("Reading...");
        myCAN.read(rxmsg);
    
        Serial.print(rxmsg.id);                 //displays message id and length
        Serial.print(" ");
        Serial.println(rxmsg.len);
    
        for(int i = 0; i < rxmsg.len; i++){      //displays received frame
          Serial.print( rxmsg.buf[i]);
          Serial.print(" ");
        };
    
        Serial.print("\n");
        
        digitalWrite(ledPin,HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
    
       
      }
      // put your main code here, to run repeatedly:
    
    }
    I also attach Sender an Receiver code from arduino with shields version (It's messy but it works).

    I've already tried messing with tx rx cables and switching CAN LOW AND CAN HIGH, lowered clock rate on teensy, nothing changes so it's almost certainly in my rather improvised code.
    PLEASE HELP
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Detroit, Michigan, USA
    Posts
    104
    I will load this up tonight when I get home. I have 4 teensys on a CAN bus and I can easily reproduce this.

  3. #3
    Thanks for quick response.

    I'll be waiting

  4. #4
    Senior Member
    Join Date
    Dec 2016
    Location
    Detroit, Michigan, USA
    Posts
    104
    My pleasure.

    I ended up sticking around late at work last night and didn't get back to my house until after 10:30Pm. I will test this tonight.

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Detroit, Michigan, USA
    Posts
    104
    I suggest using ColinK's port of the CAN driver. It is easier to use IMHO, and you can be up and running within minutes. The handlers for messages are clean and very effective.

    https://github.com/collin80/FlexCAN_Library

  6. #6
    Senior Member
    Join Date
    Dec 2016
    Location
    Detroit, Michigan, USA
    Posts
    104
    Does the Teensy 3.2 have CAN lines?
    Last edited by Detroit_Aristo; 03-10-2017 at 02:24 PM.

  7. #7
    Senior Member
    Join Date
    Jan 2015
    Location
    UK
    Posts
    174
    Teensy 3.2 does have CAN, it is on pin 3 and 4. All you need is connect it to a CAN transceiver.

  8. #8
    Library description says that allowed baudrate values are from 20k to 1M but supported ones are 50k, 100k, 125k, 250k, 500k and 1M. Because I still don't know how to make this work, I can't check if 83k3 bauderate is already defined there. Are there any examples for teensy 3.2 out there similar to send and receive_check int these examples - https://github.com/Seeed-Studio/CAN_...aster/examples?
    If not, would someone write one for me? THANKS

Posting Permissions

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