//—————————————————————————————————————————————————————————————————————————————— // ACAN2515 Demo in loopback mode //—————————————————————————————————————————————————————————————————————————————— #include //—————————————————————————————————————————————————————————————————————————————— // MCP2515 connections: // - standard SPI pins for SCK, MOSI and MISO // - a digital output for CS // - interrupt input pin for INT //—————————————————————————————————————————————————————————————————————————————— // If you use CAN-BUS shield (http://wiki.seeedstudio.com/CAN-BUS_Shield_V2.0/) with Arduino Uno, // use B connections for MISO, MOSI, SCK, #9 or #10 for CS (as you want), // #2 or #3 for INT (as you want). //—————————————————————————————————————————————————————————————————————————————— // 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 = 5 ; // CS input of MCP2515 (adapt to your design) static const byte MCP2515_INT = 26 ; // 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 = 8UL * 1000UL * 1000UL ; // 16 MHz //static const uint32_t QUARTZ_FREQUENCY = 16UL * 1000UL * 1000UL ; // 16 MHz //—————————————————————————————————————————————————————————————————————————————— // SETUP //—————————————————————————————————————————————————————————————————————————————— void setup () { //--- Switch on builtin led pinMode (LED_BUILTIN, OUTPUT) ; digitalWrite (LED_BUILTIN, HIGH) ; //--- Start serial Serial.begin (115200) ; //--- 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 delay(500); Serial.println ("Configure ACAN2515") ; //ACAN2515Settings settings (QUARTZ_FREQUENCY, 125UL * 1000UL) ; // CAN bit rate 125 kb/s ACAN2515Settings settings (QUARTZ_FREQUENCY, 10UL * 1000UL) ; // CAN bit rate 125 kb/s settings.mRequestedMode = ACAN2515Settings::NormalMode ; // Select loopback mode //settings.mRequestedMode = ACAN2515Settings::LoopBackMode ; // Select loopback 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 gBlinkLedDate = 0 ; static uint32_t gReceivedFrameCount = 0 ; static uint32_t gSentFrameCount = 0 ; //—————————————————————————————————————————————————————————————————————————————— void loop () { CANMessage frame ; if (can.receive (frame)) { gReceivedFrameCount ++ ; Serial.print (" id: ");Serial.println (frame.id,HEX); Serial.print (" ext: ");Serial.println (frame.ext); Serial.print (" rtr: ");Serial.println (frame.rtr); Serial.print (" len: ");Serial.println (frame.len); Serial.print (" data: "); for(int x=0;x