HelloWorld CAN bus with 2 Teensy3.1-s using FlexCAN

Status
Not open for further replies.

a.hinsen

Member
Hi,
I built a basic "Hello world" example to try the CAN functionality using 2 teensy3.1s and 2 MCP2551 CAN drivers:
Transmitter teensy (USB connected to PC): On the click of the button, a 1 byte message is sent to the bus.
Receiver teensy (USB connected to power adapter): Blink the LED when any message is detected.
It is not working:
On the 1st button click the CANbus.write returns 1(success), on all subsequent clicks it returns 0(fail) and also the led blinks for the length of the timeout.
For me this means it can't transmit the content of the transmit buffer (only one buffer is used when timeout>0, and the number of successes is 8 if I use 0 timeout)

One thing I ask is the usual question: Anybody any idea what could be wrong?
Another thing I ask is ideas to debug the thing:
- Can I retreive an error code that tells me what the exact problem was? (e.g. the message could not be started, or didn't receive ack, ...)
- Is there a way (other than buying an oscilloscope) to check parts of the system (e.g. I could check the transmitter side alone, if it didn't expect an ack from the receiver)

Code:
//#define receive_CAN 0
#define transmit_CAN 0
#ifdef transmit_CAN//-------------TRANSMITTER -------------------

#include <FlexCAN.h>
#include <kinetis_flexcan.h>
#define BUTTON 0
FlexCAN CANbus(50000);
CAN_message_t msg;
int x;

void setup() {
  CANbus.begin();
  pinMode(BUTTON,INPUT_PULLUP);   // Button to GND
  pinMode(LED_BUILTIN,OUTPUT);    // LED
  Serial.begin(9600);
}

void loop() {
  while(digitalRead(BUTTON)); //Wait falling edge
  digitalWrite(LED_BUILTIN,HIGH); //blink while transmitting
  msg.buf[0]= 1;
  msg.len = 1;
  msg.timeout=100;
  msg.id = 1;
  x=CANbus.write(msg);
  Serial.write(x+48); //1:success, 0:fail
  digitalWrite(LED_BUILTIN,LOW);
  delay(50); // Button debounce
  while(!digitalRead(BUTTON));
  delay(50); // Button debounce
}
#endif
#ifdef receive_CAN//-------------RECEIVER -------------------

#include <FlexCAN.h>
#include <kinetis_flexcan.h>
FlexCAN CANbus(500000);
CAN_message_t rxmsg;

void setup() {
  CANbus.begin();
  pinMode(LED_BUILTIN,OUTPUT);    // LED
}

void loop() {
  if (CANbus.read(rxmsg)){
    digitalWrite(LED_BUILTIN,HIGH); //blink LED
    delay(10);
    digitalWrite(LED_BUILTIN,LOW);
    delay(10);
  }
}
#endif
(using arduino+teensyduino+FlexCAN library on Win XP)

layout.jpg

Thanks
Andras
 
You´ve configured 50k baudrate on the transmitter side and 500k on the reciever side.
Make sure your transmitter is not in silent mode and that your filters are setup correctly on the reciever side.
Try sending a 8byte package instead.
Configure for Standard ID ( SID ).

An oscilloscope is almost mandatory at this point.
Make sure you have connected the Teensy TX pin (pin3) to MCP2551 TXD (pin1) and vice versa with the RX pins.
 
Ooh yes, I messed up the baudrates, you are right. Now it works fine using either baud rate, as long as the same is used on both sides :)
Thank you Erikk!
Which FlexCAN function would I use to set silent mode, and standard ID ?
 
Status
Not open for further replies.
Back
Top