CAN-Bus with SN65HVD230D tranceiver Teensy 4.1

pm1

Member
Hi,

I want to communicate with automotive sensors via CAN-Bus on the teensy 4.1. I'm using the SN65HVD230D with the FlexCAN_T4 library. Receiving works, sending messages doesn't. As soon as I use the FlexCAN_T4 .write() function receiving no longer works, either. The .write() function has no physical influence on the CAN bus. I measured it per Oscilloscope.
This code is not working. However if I comment out the sendCANtestMessage() function, receiving is working:

Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;

elapsedMillis timer = 0;
void setup() {
  Serial.begin(115200);
  Serial.println("setup start");
  can1.begin();
  can1.setBaudRate(500000);
}

void loop() {
  if (timer > 100) {
    timer = 0;
    sendCANtestMessage();
    Serial.println("Send Test CAN Message");

    CAN_message_t inMsg;
    if (can1.read(inMsg)) {
      Serial.print("CAN1 ");
      Serial.print("MB: ");
      Serial.print(inMsg.mb);
      Serial.print("  ID: 0x");
      Serial.print(inMsg.id, HEX);
      Serial.print("  EXT: ");
      Serial.print(inMsg.flags.extended);
      Serial.print("  LEN: ");
      Serial.print(inMsg.len);
      Serial.print(" DATA: ");
      for (uint8_t i = 0; i < 8; i++) {
        Serial.print(inMsg.buf[i]);
        Serial.print(" ");
      }
      Serial.print("  TS: ");
      Serial.println(inMsg.timestamp);
    }
  }
}

void sendCANtestMessage() {
  CAN_message_t outMsg;
  outMsg.id = 0xFF;
  outMsg.len = 8;
  outMsg.flags.extended = 0;
  for (byte i = 0; i < 8; i++) {
    outMsg.buf[i] = i;  
  }
  can1.write(outMsg);
}

Any ideas why the .write() function is not working here? Bildschirmfoto 2023-03-19 um 18.38.54.png
 
When you connect the oscilloscope to pin 22 [CTX1], do you see a signal there?

Paul
 
The code is working.

I would move the read into the main loop and not inside the timer.

Like this:
Code:
void loop() {
   CAN_message_t inMsg;
   if (can1.read(inMsg)) {
      Serial.print("CAN1 ");
      Serial.print("MB: ");
      Serial.print(inMsg.mb);
      Serial.print("  ID: 0x");
      Serial.print(inMsg.id, HEX);
      Serial.print("  EXT: ");
      Serial.print(inMsg.flags.extended);
      Serial.print("  LEN: ");
      Serial.print(inMsg.len);
      Serial.print(" DATA: ");
      for (uint8_t i = 0; i < 8; i++) {
        Serial.print(inMsg.buf[i]);
        Serial.print(" ");
      }
      Serial.print("  TS: ");
      Serial.println(inMsg.timestamp);
    }
    
  if (timer > 100) {
    timer = 0;
    sendCANtestMessage();
    Serial.println("Send Test CAN Message");

   
    
  }
}
 
Check you have two 120R terminator on your network.

One by the SN65HVD230D and another by the sensor end.
 
One 120 Ohm resistor on the SN65HVD230D and one 100 Ohm resistor at the other end because of lack of 120 Ohm resistors. But I will try it with 120 Ohm.
 
Could be a fault with the SN65HVD230D or the automotive sensor.

Post a photo of the SN65HVD230D.
 
The automotive steering wheel angle sensor is working correctly. I need to send values to calibrate the sensor. With PCAN-View Software this is working without problems on the same CAN bus with a USB adapter. However this task should be executed by the teensy. Receiving values is working as long as I'm not using the write() function in any part of the code.
2023-03-19_sn65hvd230-1.jpg
2023-03-19_sn65hvd230-2.jpg
 
Assumed you have wired them up correctly.

If you see waveform on CTX and steady state on CAN_H and CAN_L then the SN65HVD230D breakout board most likely to be faulty.
 
Yes, thanks for supporting me. I measured the CTX1 values on the SN65HVD230D CTX pin. I will order new SN65HVD230D breakout boards.
 
Back
Top