UNO - Mega
Hardware: UNO, Mega2560, zwei Stück MCP2515
Bibliothek: CAN_BUS_Shield mit mcp_can.h
Software: UNO und Mega mit den Bibliotheksbeispielen send_Blink und receive_Blink:
// UNO
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
const int ledHIGH = 1;
const int ledLOW = 0;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_125KBPS)) // init can bus : baudrate
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}
byte stmp[] = {0, 0, ledHIGH, 1, 2, 3, ledLOW, 5};
byte len = sizeof(stmp);
void loop()
{ Serial.println("In loop");
// send data: id = 0x70, standard frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x70, 0, len, stmp);
delay(1000); // send data once per second
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
// Mega2560
// SCK - 52
// SI - 51
// SO - 50
// CS - 53
// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 53;
const int LED = 8;
boolean ledON = 1;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
pinMode(LED,OUTPUT);
while (CAN_OK != CAN.begin(CAN_125KBPS)) // init can bus : baudrate
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}
void loop()
{
byte len = 0;
byte buf[16];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.print("get data from ID: 0x");
Serial.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
if(ledON && i==2)
{
digitalWrite(LED, buf[i]);
ledON = 0;
delay(500);
}
else if((!(ledON)) && i==6)
{
digitalWrite(LED, buf[i]);
ledON = 1;
}
}
Serial.println();
}
}
//END FILE
Ausgabe serieller Monitor Mega:
-----------------------------
get data from ID: 0x70
0 0 1 1 2 3 0 5
So weit, so gut
UNO - Mega - Teensy
zusätzliche Hardware: Teensy 3.2, TJA1050
Bildquelle: liegt der Bibliothek bei
Teensy Pin 3 (CAN TX) - TJA1050 CAN TX
Teensy Pin 4 (CAN RX) - TJA1050 CAN RX
Bibliothek: FlexCAN (Die mit teensyduino gelieferte Bibliothek enthält nur Beispiele für Teensy 3.6, daher fliegt die leider raus.)
Software: Bibliotheksbeispiel CANtest
// -------------------------------------------------------------
// CANtest for Teensy 3.1
// by teachop
//
// This test is talking to a single other echo-node on the bus.
// 6 frames are transmitted and rx frames are counted.
// Tx and rx are done in a way to force some driver buffering.
// Serial is used to print the ongoing status.
//
#include <Metro.h>
#include <FlexCAN.h>
Metro sysTimer = Metro(1);// milliseconds
int led = 13;
FlexCAN CANbus(125000);
static CAN_message_t msg, rxmsg;
static uint8_t hex[17] = "0123456789abcdef";
int txCount, rxCount;
unsigned int txTimer, rxTimer;
// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while ( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working >> 4 ] );
Serial.write( hex[ working & 15 ] );
}
Serial.write('\r');
Serial.write('\n');
}
// -------------------------------------------------------------
void setup(void)
{
Serial.begin(115200);
CANbus.begin();
pinMode(led, OUTPUT);
digitalWrite(led, 1);
delay(2000);
Serial.println(F("Hello Teensy 3.1 CAN Test."));
sysTimer.reset();
}
// -------------------------------------------------------------
void loop(void)
{
// service software timers based on Metro tick
if ( sysTimer.check() ) {
if ( txTimer ) {
--txTimer;
}
if ( rxTimer ) {
--rxTimer;
}
}
// if not time-delayed, read CAN messages and print 1st byte
if ( !rxTimer ) {
while ( CANbus.read(rxmsg) ) {
hexDump( sizeof(rxmsg), (uint8_t *)&rxmsg );
Serial.write(rxmsg.buf[0]);
rxCount++;
}
}
/*
// insert a time delay between transmissions
if ( !txTimer ) {
// if frames were received, print the count
if ( rxCount ) {
Serial.write('=');
Serial.print(rxCount);
rxCount = 0;
}
txTimer = 100;//milliseconds
msg.len = 8;
msg.id = 0x222;
for ( int idx = 0; idx < 8; ++idx ) {
msg.buf[idx] = '0' + idx;
}
// send 6 at a time to force tx buffering
txCount = 6;
digitalWrite(led, 1);
Serial.println(".");
while ( txCount-- ) {
CANbus.write(msg);
msg.buf[0]++;
}
digitalWrite(led, 0);
// time delay to force some rx data queue use
rxTimer = 3;//milliseconds
}
*/
}
Den Teil zum Senden habe ich auskommentiert, da ich zunächst nur Empfangen möchte.
Serieller Monitor des Teensys:
Hello Teensy 3.1 CAN Test.
Es wird also nichts empfangen.
Mir ist aufgefallen, daß die Struktur beim Teensy von der des UNOs abweicht, da beim UNO timeout fehlt:
typedef struct CAN_message_t {
uint32_t id; // can identifier
uint8_t ext; // identifier is extended
uint8_t len; // length of data
uint16_t timeout; // milliseconds, zero will disable waiting
uint8_t buf[8];
} CAN_message_t;
Aber empfangen werden müßte doch irgendwas, hatte ich gehofft.
Was mache ich falsch, wie bekomme ich Daten empfangen?