pierremolinaro / acan2515

MCP2515 CAN Controller Driver for Arduino
MIT License
74 stars 29 forks source link

ACAN25215 avec le CAN-BUS shield seeedsstudio et Arduino Mega #19

Closed BOBILLEChristophe closed 2 years ago

BOBILLEChristophe commented 4 years ago

Bonjour Pierre,

Je teste votre bibliothèque avec un shield seeeds et un Arduino UNO sans aucun problème. Mais je n'arrive pas avec un MEGA.

Voici mes réglages

`//—————————————————————————————————————————————————————————————————————————————— // 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). //——————————————————————————————————————————————————————————————————————————————

ifdef ARDUINO_AVR_MEGA2560

static const byte MCP2515_SCK = 52 ; // SCK input of MCP2515 static const byte MCP2515_SI = 51 ; // SI output of MCP2515 static const byte MCP2515_SO = 50 ; // S0 output of MCP2515

static const byte MCP2515_CS = 53 ; // CS input of MCP2515 static const byte MCP2515_INT = 2 ; // INT output of MCP2515

endif

ifdef ARDUINO_AVR_UNO

static const byte MCP2515_SCK = 13 ; // SCK input of MCP2515 static const byte MCP2515_SI = 11 ; // SI output of MCP2515 static const byte MCP2515_SO = 12 ; // S0 output of MCP2515

static const byte MCP2515_CS = 10 ; // CS input of MCP2515 static const byte MCP2515_INT = 2 ; // INT output of MCP2515

endif`

Merci pour votre aide.

Christophe

pierremolinaro commented 4 years ago

Bonsoir,

Je n'ai pas d'Arduino Mega, je ne peux pas faire de test.

Toutefois, j'ai vérifié les numéros de broches, cela me semble correct.

Peux-tu me transmettre le code de la fonction setup, où la librairie ACAN est initialisée ?

Amicalement,

Pierre

Le 25 août 2020 à 13:00, BOBILLEChristophe notifications@github.com a écrit :

Bonjour Pierre,

Je teste votre bibliothèque avec un shield seeeds et un Arduino UNO sans aucun problème. Mais je n'arrive pas avec un MEGA.

Voici mes réglages

`//—————————————————————————————————————————————————————————————————————————————— // 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/ http://wiki.seeedstudio.com/CAN-BUS_Shield_V2.0/) with Arduino Uno, // use B connections for MISO, MOSI, SCK, #9 https://github.com/pierremolinaro/acan2515/issues/9 or #10 https://github.com/pierremolinaro/acan2515/issues/10 for CS (as you want), // #2 https://github.com/pierremolinaro/acan2515/issues/2 or #3 https://github.com/pierremolinaro/acan2515/issues/3 for INT (as you want). //——————————————————————————————————————————————————————————————————————————————

ifdef ARDUINO_AVR_MEGA2560

static const byte MCP2515_SCK = 52 ; // SCK input of MCP2515 static const byte MCP2515_SI = 51 ; // SI output of MCP2515 static const byte MCP2515_SO = 50 ; // S0 output of MCP2515

static const byte MCP2515_CS = 53 ; // CS input of MCP2515 static const byte MCP2515_INT = 2 ; // INT output of MCP2515

endif

ifdef ARDUINO_AVR_UNO

static const byte MCP2515_SCK = 13 ; // SCK input of MCP2515 static const byte MCP2515_SI = 11 ; // SI output of MCP2515 static const byte MCP2515_SO = 12 ; // S0 output of MCP2515

static const byte MCP2515_CS = 10 ; // CS input of MCP2515 static const byte MCP2515_INT = 2 ; // INT output of MCP2515

endif`

Merci pour votre aide.

Christophe

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pierremolinaro/acan2515/issues/19, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWKZVF5SWMPEKXU2BTSEQLSCOKWLANCNFSM4QKQOP4A.

BOBILLEChristophe commented 4 years ago

Bonsoir Pierre,

Merci pour ta réponse. Il s'agit d'une petite application de test dont le code est assez succinct. Je te l'adresse donc en entier. L'idée est de se servir d'un Mega qui dispose de nombreux port pour detecter des changement d'état sur des capteurs et envoyer sur le bus l'état de tous les ports.

Bien cordialement

Christophe

` //—————————————————————————————————————————————————————————————————————————————— // ACAN2515 Demo //——————————————————————————————————————————————————————————————————————————————

include

include

include

include

include

define NUM_NOEUD 0x3A

//—————————————————————————————————————————————————————————————————————————————— // 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). //——————————————————————————————————————————————————————————————————————————————

ifdef ARDUINO_AVR_MEGA2560

static const byte MCP2515_SCK = 52 ; // SCK input of MCP2515 static const byte MCP2515_SI = 51 ; // SI output of MCP2515 static const byte MCP2515_SO = 50 ; // S0 output of MCP2515

static const byte MCP2515_CS = 53 ; // CS input of MCP2515 static const byte MCP2515_INT = 2 ; // INT output of MCP2515

endif

ifdef ARDUINO_AVR_UNO

static const byte MCP2515_SCK = 13 ; // SCK input of MCP2515 static const byte MCP2515_SI = 11 ; // SI output of MCP2515 static const byte MCP2515_SO = 12 ; // S0 output of MCP2515

static const byte MCP2515_CS = 10 ; // CS input of MCP2515 static const byte MCP2515_INT = 2 ; // INT output of MCP2515

endif

//—————————————————————————————————————————————————————————————————————————————— // MCP2515 Driver object //——————————————————————————————————————————————————————————————————————————————

ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;

static const uint32_t QUARTZ_FREQUENCY = 16UL 1000UL 1000UL ; // 16 MHz static const uint32_t BUS_FREQUENCY = 125ul * 1000ul;

CANMessage messageCANEmission; CANMessage messageCANReception;

byte oldPortC = 255; byte oldPortD = 255; byte oldPortB = 255; byte portC[] = { A0, A1, A2, A3, A4, A5, A6, A7 };

void funct(const CANMessage & inMessage) { Serial.println("message function");

uint16_t idEmetteur = inMessage.id & 0x0FF; Serial.print("idEmetteur : "); Serial.println(inMessage.id, HEX); Serial.println(); Serial.print("PINC : ") ; Serial.println(inMessage.data[0], BIN); Serial.print("PIND : ") ; Serial.println(inMessage.data[1], BIN); Serial.print("PINB : ") ; Serial.println(inMessage.data[2], BIN); }

const ACAN2515Mask masque = standard2515Mask(0xFF, 0, 0); / 00011111111 / const ACAN2515AcceptanceFilter filtres[] = { { standard2515Filter(0x1E, 0, 0), funct } };

//—————————————————————————————————————————————————————————————————————————————— // 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 Serial.println ("Configure ACAN2515") ; ACAN2515Settings settings(QUARTZ_FREQUENCY, BUS_FREQUENCY); // settings.mReceiveBufferSize = 1; // settings.mTransmitBuffer0Size = 1; const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }, masque, filtres, 1) ; if (errorCode != 0) { Serial.print("CAN erreur : 0x"); Serial.println(errorCode, HEX); while (1); } else { 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 ("%") ; }

// Pullup sur toutes les entrees du portC for (int i = 0; i < 8; i++) { pinMode(portC[i], INPUT_PULLUP); } }

//----------------------------------------------------------------------------------------------------------------------

//——————————————————————————————————————————————————————————————————————————————

void loop () {

bool emission = false;

// Detection if (oldPortC != PINC) { Serial.print("Port C : "); Serial.println(PINC, BIN); emission = true; oldPortC = PINC; } if (oldPortD != PIND) { Serial.print("Port D : "); Serial.println(PIND, BIN); emission = true; oldPortD = PIND; } if (oldPortB != PINB) { Serial.print("Port B : "); Serial.println(PINB, BIN); emission = true; oldPortB = PINB; }

// Emission if (emission) { messageCANEmission.len = 3; uint16_t idMsg = 0x400; // Priorité du message de 0x400 (elevee) a 0x700 (faible) idMsg = idMsg | NUM_NOEUD;
messageCANEmission.id = idMsg; messageCANEmission.data[0] = PINC; messageCANEmission.data[1] = PIND; messageCANEmission.data[2] = PINB; if (can.tryToSend(messageCANEmission)) { Serial.println("message envoye !"); } }

// Reception can.dispatchReceivedMessage(); }

//—————————————————————————————————————————————————————————————————————————————— `

BOBILLEChristophe commented 4 years ago

Pierre,

Le problème venait de l'utilisation d'un clone du CAN-BUS shield de Seeed. Avec une carte officielle

https://wiki.seeedstudio.com/CAN-BUS_Shield_V2.0/

cela fonctionne parfaitement mais il faut placer MCP2515_CS sur la pin 9.

static const byte MCP2515_CS = 9 ; // CS input of MCP2515

Il est probable que sur les clones (bien que gravé V2.0), le bug présent dans la V0.9 n'ait pas été corrigé ce qui est évoqué en bas de cette page https://seeeddoc.github.io/CAN-BUS_Shield/ -> How to Compatible With Arduino Mega and Leonardo

Je vais faire des tests sur les clone et reporterai les résultats ici.

Bien cordialement.

Christophe

pierremolinaro commented 4 years ago

Ah, d'accord, j'ignorais ce point.

Merci pour cette info,

Pierre

Le 26 août 2020 à 02:07, BOBILLEChristophe notifications@github.com a écrit :

Pierre,

Le problème venait de l'utilisation d'un clone du CAN-BUS shield de Seeed. Avec une carte officielle

https://wiki.seeedstudio.com/CAN-BUS_Shield_V2.0/ <x-msg://1/url> cela fonctionne parfaitement mais il faut placer MCP2515_CS sur la pin 9.

static const byte MCP2515_CS = 9 ; // CS input of MCP2515

Il est probable que sur les clones (bien que gravé V2.0), le bug présent dans la V0.9 n'ait pas été corrigé ce qui est évoqué en bas de cette page https://seeeddoc.github.io/CAN-BUS_Shield/ <x-msg://1/url> -> How to Compatible With Arduino Mega and Leonardo

Je vis faire des tests sur les clone et reporterai les résultats ici.

Bien cordialement.

Christophe

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pierremolinaro/acan2515/issues/19#issuecomment-680329573, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWKZVHDSGKTQIOXBKYFDOLSCRG5LANCNFSM4QKQOP4A.