我正在尝试在 Teensy 4.1 和 Arduino Nano 之间进行 CAN 通信。对于 Teensy 和 Nano 之间的通信,我在 Teensy 端使用 TJA1050 收发器,在 Nano 端使用 MCP2515 CAN 模块。我可以使用 MCP2515 在两个 Nano 之间进行 CAN 通信,所以我知道我的 mcp2515 工作正常。
当我发送数据时,它显示数据是从 Teensy 发送的,但是在 17 次迭代后它停止发送数据,而且 Arduino Nano 也没有接收到任何数据。我还尝试设置相同的比特率,但在 Nano 上看不到任何接收到的东西。我使用过 ACAN_T4 和 ACAN2515 库。
青少年 4.1 的代码
#ifndef __IMXRT1062__
#error "This sketch should be compiled for Teensy 4.x"
#endif
//-----------------------------------------------------------------
#include <ACAN_T4.h>
//-----------------------------------------------------------------
void setup () {
pinMode (LED_BUILTIN, OUTPUT) ;
Serial.begin (9600) ;
while (!Serial) {
delay (50) ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
ACAN_T4_Settings settings (125 * 1000) ; // 125 kbit/s
// settings.mBitRatePrescaler = 6;
//settings.mPropagationSegment = 5;
//settings.mPhaseSegment1 = 5;
//settings.mPhaseSegment2 = 5;
const uint32_t errorCode = ACAN_T4::can1.begin (settings) ;
Serial.print ("Bitrate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Serial.print ("Propagation Segment: ") ;
Serial.println (settings.mPropagationSegment) ;
Serial.print ("Phase segment 1: ") ;
Serial.println (settings.mPhaseSegment1) ;
Serial.print ("Phase segment 2: ") ;
Serial.println (settings.mPhaseSegment2) ;
Serial.print ("RJW: ") ;
Serial.println (settings.mRJW) ;
Serial.print ("Triple Sampling: ") ;
Serial.println (settings.mTripleSampling ? "yes" : "no") ;
Serial.print ("Actual bitrate: ") ;
Serial.print (settings.actualBitRate ()) ;
Serial.println (" bit/s") ;
Serial.print ("Exact bitrate ? ") ;
Serial.println (settings.exactBitRate () ? "yes" : "no") ;
Serial.print ("Distance from wished bitrate: ") ;
Serial.print (settings.ppmFromWishedBitRate ()) ;
Serial.println (" ppm") ;
Serial.print ("Sample point: ") ;
Serial.print (settings.samplePointFromBitStart ()) ;
Serial.println ("%") ;
if (0 == errorCode) {
Serial.println ("can1 ok") ;
} else {
Serial.print ("Error can1: 0x") ;
Serial.println (errorCode, HEX) ;
while (1) {
delay (100) ;
Serial.println ("Invalid setting") ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
}
}
//-----------------------------------------------------------------
static uint32_t gSendDate = 0 ;
static uint32_t gSentCount = 0 ;
//-----------------------------------------------------------------
void loop () {
CANMessage message ;
message.len = 8;
message.data[0] = 127;
message.data[1] = 43;
message.data[2] = 1;
if (gSendDate <= millis ()) {
//message.id = 0x542 ;
const bool ok = ACAN_T4::can1.tryToSend (message) ;
if (ok) {
gSendDate += 2000 ;
gSentCount += 1 ;
Serial.print ("Sent: ") ;
Serial.println (gSentCount) ;
}
}
}
Arduino Nano 代码
#include <ACAN2515.h>
// Error codes and possible causes:
// In case you see "Configuration error 0x1", the Arduino doesn't communicate
// with the 2515. You will get this error if there is no CAN shield or if
// the CS pin is incorrect.
// In case you see succes up to "Sent: 17" and from then on "Send failure":
// There is a problem with the interrupt. Check if correct pin is configured
//——————————————————————————————————————————————————————————————————————————————
static const byte MCP2515_CS = 10 ; // CS input of MCP2515 (adapt to your design)
static const byte MCP2515_INT = 3 ; // INT output of MCP2515 (adapt to your design)
//——————————————————————————————————————————————————————————————————————————————
// MCP2515 Driver object
//——————————————————————————————————————————————————————————————————————————————
ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;
//——————————————————————————————————————————————————————————————————————————————
// MCP2515 Quartz: adapt to your design
//——————————————————————————————————————————————————————————————————————————————
static const uint32_t QUARTZ_FREQUENCY = 16UL * 1000UL * 1000UL ; // 16 MHz
void setup () {
//--- Switch on builtin led
pinMode (LED_BUILTIN, OUTPUT) ;
digitalWrite (LED_BUILTIN, HIGH) ;
//--- Start serial
Serial.begin (9600) ;
//--- Wait for serial (blink led at 10 Hz during waiting)
while (!Serial) {
delay (50) ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
//--- Begin SPI
SPI.begin () ;
//--- Configure ACAN2515
Serial.println ("Configure ACAN2515") ;
ACAN2515Settings settings (QUARTZ_FREQUENCY, 125UL * 1000UL) ; // CAN bit rate 125 kb/s
settings.mRequestedMode = ACAN2515Settings::NormalMode ; // Select normal mode
const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
if (errorCode == 0) {
Serial.print ("Bit Rate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Serial.print ("Propagation Segment: ") ;
Serial.println (settings.mPropagationSegment) ;
Serial.print ("Phase segment 1: ") ;
Serial.println (settings.mPhaseSegment1) ;
Serial.print ("Phase segment 2: ") ;
Serial.println (settings.mPhaseSegment2) ;
Serial.print ("SJW: ") ;
Serial.println (settings.mSJW) ;
Serial.print ("Triple Sampling: ") ;
Serial.println (settings.mTripleSampling ? "yes" : "no") ;
Serial.print ("Actual bit rate: ") ;
Serial.print (settings.actualBitRate ()) ;
Serial.println (" bit/s") ;
Serial.print ("Exact bit rate ? ") ;
Serial.println (settings.exactBitRate () ? "yes" : "no") ;
Serial.print ("Sample point: ") ;
Serial.print (settings.samplePointFromBitStart ()) ;
Serial.println ("%") ;
} else {
Serial.print ("Configuration error 0x") ;
Serial.println (errorCode, HEX) ;
}
}
//----------------------------------------------------------------------------------------------------------------------
static uint32_t gReceivedFrameCount = 0 ;
//——————————————————————————————————————————————————————————————————————————————
void loop () {
CANMessage frame ;
if (can.available ()) {
can.receive (frame) ;
int pwm = frame.data[0];
int angle = frame.data[1];
int dir = frame.data[2];
gReceivedFrameCount ++ ;
Serial.print ("Received: ") ;
Serial.print (gReceivedFrameCount) ;
Serial.print(" pwm ");
Serial.print(pwm);
Serial.print(" angle ");
Serial.print(angle);
Serial.print(" dir ");
Serial.println(dir);
}
}
如果你能帮我解决这个问题,那将是一个很大的帮助。谢谢你。