2 node CAN bus with SN65HVD230 tranceiver.

Status
Not open for further replies.

Black_Stork

Member
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-CAN-Board-Network-Transceiver-Evaluation-Development-Module-Kit-3-3V-/251042740085?rmvSB=true x2
https://www.seeedstudio.com/CAN-BUS-Shield-V1.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=0B0iA-Iu_GinvU1BWaFVhazllY1U
https://drive.google.com/open?id=0B0iA-Iu_GinvMXo3TnpRamNud1E

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
 

Attachments

  • CAN-sender.ino
    877 bytes · Views: 84
  • CAN-receiver.ino
    1.3 KB · Views: 76
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.
 
Teensy 3.2 does have CAN, it is on pin 3 and 4. All you need is connect it to a CAN transceiver.
 
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_BUS_Shield/tree/master/examples?
If not, would someone write one for me? THANKS
 
Status
Not open for further replies.
Back
Top