Tennsy 3.2 doesn't read CAN messages.

Status
Not open for further replies.

Black_Stork

Member
I am trying to send messages from one teensy to another. I know that teensy which sends messages works correctly since my Arduino with seeedstudio CAN BUS shield reads it perfectly fine.
Both microcontrollesr use TI SN65HVD230 tranceiver.
They are both connected the same way, between CAN lines there is 60 Ohm resistance, so I assume that there is something wrong with my receiver code and not in wiring.
Arduino library is included because i use PlatformIO on Windows 10. I use Collink80's version of FlexCAN library.

RECEIVER(doesn't work):
Code:
#include <FlexCAN.h>
#include <kinetis_flexcan.h>
#include <Arduino.h>


const int baudRate = 500000;   // baud Rate
const int ledPin = 13;        // embedded led
const int delayTime = 100;   // 1/10 second

FlexCAN myCAN(baudRate);

static CAN_message_t msgReceived;  // wiadomosc gdy wcisniety


void setup()
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
  digitalWrite(ledPin, HIGH);
  myCAN.begin(baudRate);
  delay(1500);
  digitalWrite(ledPin, LOW);
  Serial.println("CAN Receiver Initialized");

}

void loop()
{

    myCAN.read(msgReceived);
    while(myCAN.read(msgReceived))
    {
    // toggle LEDs
        digitalWrite(ledPin, !digitalRead(ledPin));
        Serial.print("Receiving: ");
        for(int i=0; i<msgReceived.len; i++)
        {
            Serial.print(msgReceived.buf[i]); Serial.print(" ");
        }

        Serial.println("");

    }

}

SENDER(works fine; sends 2 different messeges depending on button position):
Code:
#include <FlexCAN.h>
#include <kinetis_flexcan.h>
#include <Arduino.h>


const int baudRate = 500000;   // baudRate
const int ledPin = 13;        // embedded led
const int delayTime = 500;   // 
const int button = 8;

FlexCAN myCAN(baudRate);

static CAN_message_t msgButton_P;  // when pressed
static CAN_message_t msgButton_N;  // when NOT pressed

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
  digitalWrite(ledPin, HIGH);
  myCAN.begin(baudRate);
  delay(1500);
  digitalWrite(ledPin, LOW);
  Serial.println("CAN Tranceiver Initialized");

  //P
  msgButton_P.ext = 0;       // 11 bit
  msgButton_P.id = 0xD0;     // adress msgButton_P
  msgButton_P.len = 8;        // length

  msgButton_P.buf[0] = 0x86;  // kolejne bity
  msgButton_P.buf[1] = 0x00;
  msgButton_P.buf[2] = 0x00;
  msgButton_P.buf[3] = 0x00;
  msgButton_P.buf[4] = 0x00;
  msgButton_P.buf[5] = 0x00;
  msgButton_P.buf[6] = 0x00;
  msgButton_P.buf[7] = 0x00;

  //N
  msgButton_N.ext = 0;       // 11 nit
  msgButton_N.id = 0xD0;     // adresa msgButton_N
  msgButton_N.len = 8;        // length

  msgButton_N.buf[0] = 0x06;  !
  msgButton_N.buf[1] = 0x00;
  msgButton_N.buf[2] = 0x00;
  msgButton_N.buf[3] = 0x00;
  msgButton_N.buf[5] = 0x00;
  msgButton_N.buf[6] = 0x00;
  msgButton_N.buf[7] = 0x00;
}

void loop()
{

    digitalWrite(ledPin, HIGH);
    delay(delayTime / 2);

    Serial.print("Sending every ");
    Serial.println(delayTime);
    if (digitalRead(button) == LOW)        // if pressed
    {
        Serial.print(msgButton_P.id);     // print
        Serial.print(' ');
        Serial.println(msgButton_P.len);

        for (int i = 0 ; i < msgButton_P.len ; i++)
        {
            Serial.print(msgButton_P.buf[i]);
            Serial.print(' ');
        }
        Serial.print("\n");

        myCAN.write(msgButton_P);         //send msgButton_P
    }

    else
    {
        Serial.print(msgButton_N.id);     
        Serial.print(' ');
        Serial.println(msgButton_N.len);

        for (int i = 0 ; i <msgButton_N.len ; i++)
        {
            Serial.print(msgButton_N.buf[i]);
            Serial.print(' ');
        }
        Serial.print("\n");

        myCAN.write(msgButton_N);         // msgButton_N
    }

    digitalWrite(ledPin, LOW);
    delay(delayTime / 2);


}
 
Thank you very much tni, it worked!
As far as i understand the custom name is only for this line: "FlexCAN myCAN(baudRate);" and all comands regarding sending and receiving messages is executed by Can0 object or Can1 in case of using Teensy3.5?
 
As far as i understand the custom name is only for this line: "FlexCAN myCAN(baudRate);"
No, don't ever declare your own instance of FlexCAN (or be prepared for things randomly not working). The constructor doesn't even take the baud rate as parameter.

You specify the baud rate when you call begin(), e.g. "Can0.begin(500000);" uses 500'000 baud.

\\

Given how different and incompatible the 'collin80' version of FlexCAN is to the original, it should have been renamed.
 
Status
Not open for further replies.
Back
Top