Je ne comprends pas très bien pourquoi ton Bus CAN ne fonctionnerait pas. Pour utiliser ces bus, il faut soit ajouter un transceiver (attention choisir un transceiver CAN 3.3V):
Soit utiliser un simple montage avec 2 diodes et une résistance comme indiqué dans l’Application Note de Siemens, ça fonctionne jusqu’à 2 à 3 mètres. Bien entendu c’est plutôt à réserver à une phase de tests(Attention à relier au 3.3V, PAS au 5V comme sur l’AP):
La bibliothèque CAN à utiliser est celle de Collin80:
A noter que comme la carte DUE dispose de 2 bus CAN, il est possible de réaliser un montage avec une seule carte pour dialoguer du CAN0 au CAN1 (voir les exemples avec la bibiothèque) et dans ce cas le montage avec diodes et résistance se justifie encore plus.
Tu peux par exemple légèrement changer l’un des codes fournis avec la bibliothèque pour lire une entrée analogique (tu liras du bruit qui change tout le temps) et envoyer le résultat d’un CAN vers l’autre:
#include "variant.h"
#include <due_can.h>
#define TEST1_CAN_TRANSFER_ID 0x11AE756A //random 29 bits
#define TEST1_CAN0_TX_PRIO 15
#define CAN_MSG_DUMMY_DATA 0x11BFFA4E
// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN 8
uint32_t sentFrames, receivedFrames;
//Leave this defined if you use the native port or comment it out if you use the programming port
//#define Serial SerialUSB
CAN_FRAME frame1, frame2, incoming;
void setup() {
Serial.begin(250000);
// Verify CAN0 and CAN1 initialization, baudrate is 1Mb/s:
if (Can0.begin(CAN_BPS_1000K) &&
Can1.begin(CAN_BPS_1000K)) {
}
else {
Serial.println("CAN initialization (sync) ERROR");
}
//Initialize the definitions for the frames we'll be sending.
//This can be done here because the frame never changes
frame1.id = TEST1_CAN_TRANSFER_ID;
frame1.length = MAX_CAN_FRAME_DATA_LEN;
//Below we set the 8 data bytes in 32 bit (4 byte) chunks
//Bytes can be set individually with frame1.data.bytes[which] = something
frame1.data.low = 0x20103040;
frame1.data.high = CAN_MSG_DUMMY_DATA;
//We are using extended frames so mark that here. Otherwise it will just use
//the first 11 bits of the ID set
frame1.extended = 1;
frame2.id = TEST1_CAN_TRANSFER_ID + 0x200;
frame2.length = MAX_CAN_FRAME_DATA_LEN;
frame2.data.low = 0xB8C8A8E8;
frame2.data.high = 0x01020304;
frame2.extended = 1;
//Both of these lines create a filter on the corresponding CAN device that allows
//just the one ID we're interested in to get through.
//The syntax is (mailbox #, ID, mask, extended)
//You can also leave off the mailbox number: (ID, mask, extended)
Can1.watchFor(TEST1_CAN_TRANSFER_ID + 0x200);
Can0.watchFor(TEST1_CAN_TRANSFER_ID);
//test_1();
}
// Test rapid fire ping/pong of extended frames
/*static */void test_1(void)
{
CAN_FRAME inFrame;
uint32_t counter = 0;
// Send out the first frame
Can0.sendFrame(frame2);
//sentFrames++;
while (1) {
delay(1000);
if (Can0.available() > 0) {
Can0.read(incoming);
Serial.print(" Received by CAN0 = ");Serial.println(incoming.data.low);Serial.println(" ");
frame2.data.low = analogRead(A0);
Serial.print(" Sent by CAN0 = ");Serial.println(frame2.data.low);
Can0.sendFrame(frame2);
//delayMicroseconds(100);
//sentFrames++;
//receivedFrames++;
//counter++;
}
if (Can1.available() > 0) {
Can1.read(incoming);
Serial.print(" Received by CAN1 = ");Serial.println(incoming.data.low);Serial.println(" ");
frame1.data.low = analogRead(A0);
Serial.print(" Sent by CAN1 = ");Serial.println(frame1.data.low);
Can1.sendFrame(frame1);
//delayMicroseconds(100);
//sentFrames++;
// receivedFrames++;
// counter++;
}
}
}
// can_example application entry point
void loop()
{
test_1();
}
Sinon, tu peux très facilement relier 2 cartes DUE en liaison serie, mais attention à la longueur des câbles (je n’ai jamais essayé avec des câbles de 2 m) par exemple:
Sur la DUE1 tu connectes RX1/TX1 à TX2/RX2 de la DUE2 (croiser les R et T) et bien sur relier les masses. Je te conseille là encore de commencer par un montage dans lequel tu n’utlises qu’une seule carte au départ. Un exemple simple de cette manip:
/*********************************************************************/
/* Jumper between RX1 and TX2, and another one between TX1 and RX2 */
/*********************************************************************/
char c = 0;
void setup() {
Serial.begin(250000);
Serial1.begin(2500000); // USART0
Serial2.begin(2500000); // USART1
Serial2.print("Hello");
}
void loop() {
String s;
s.reserve(10);
s = "";
while (Serial1.available() > 0) {
c = Serial1.read();
s += c;
}
if (s.length() > 0) {
Serial.println(s);
Serial2.print(s);
}
delay(1000);
}