v0.0.1
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
/*************************************
|
||||
* CAN-bus receive
|
||||
* Author: Michel Bats
|
||||
* Website: www.batssoft.nl
|
||||
* Version: 0.0.1
|
||||
* Revision: 20161122
|
||||
*
|
||||
* Used libraries:
|
||||
* Adafruit_SSD1306\Adafruit_SSD1306 by adafruit
|
||||
* https://github.com/adafruit/Adafruit_SSD1306
|
||||
* Adafruit-GFX-Library\Adafruit_GFX by adafruit
|
||||
* https://github.com/adafruit/Adafruit-GFX-Library
|
||||
* CAN Bus Shield - MCP2515&MCP2551\CAN_BUS_Shield by Seeed-Studio
|
||||
* https://github.com/Seeed-Studio/CAN_BUS_Shield
|
||||
*************************************/
|
||||
#include <SPI.h>
|
||||
#include <mcp_can.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
|
||||
// Bepalingen Display
|
||||
#define OLED_ADDR 60 // 60 = 0x3C / 61 = 0x3D
|
||||
#define OLED_RESET 4
|
||||
|
||||
// Bepalingen CAN-bus interface
|
||||
#define MCP_CAN_SPI_CS_PIN 10
|
||||
|
||||
// Bepalingen interrupt
|
||||
#define INTERRUPT_PIN 3
|
||||
|
||||
// Declaraties diverse variabelen
|
||||
unsigned char lenDisp = 0;
|
||||
unsigned char bufDisp[8];
|
||||
int canIdDisp = 0;
|
||||
int tel = 0;
|
||||
int canIdToReadArray[] = {114, 261, 264, 271, 274,
|
||||
279, 281, 520, 761, 773,
|
||||
781, 840, 845, 909, 973,
|
||||
1037, 1042, 1074, 1101, 1128,
|
||||
1160, 1293, 1294, 1295, 1303,
|
||||
1362, 1401, 1416, 1426, 1544,
|
||||
1551, 1554, 1792, 1928, 1933,
|
||||
1935, 1938, 1943, 2034
|
||||
};
|
||||
int canIdToRead = canIdToReadArray[0];
|
||||
#define CAN_ID_COUNT 38
|
||||
int canIdCount = 0;
|
||||
int telDisp = 0;
|
||||
|
||||
// Initialisatie Adafruit_SSD1306
|
||||
Adafruit_SSD1306 display(OLED_RESET);
|
||||
|
||||
// Initialisatie mcp_can
|
||||
MCP_CAN CAN(MCP_CAN_SPI_CS_PIN);
|
||||
|
||||
// SETUP
|
||||
void setup()
|
||||
{
|
||||
// Start Display
|
||||
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR, HEX);
|
||||
|
||||
// Wis eventuele oude gegevens uit de buffer
|
||||
display.clearDisplay();
|
||||
display.display();
|
||||
|
||||
// Initialisatie van tekenhoogte en kleur
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
|
||||
// Verbinding met de MCP2515 controleren
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS))
|
||||
{
|
||||
// Cursor verplaatsen naar linkerbovenhoek
|
||||
display.setCursor(0, 0);
|
||||
// Tekst tonen dat het initialiseren mislukt is
|
||||
display.println("CAN BUS Shield init fail");
|
||||
display.println(" Init CAN BUS Shield again");
|
||||
// Tekst naar het display wegschrijven
|
||||
display.display();
|
||||
// Display-buffer leegmaken
|
||||
display.clearDisplay();
|
||||
// Pauze
|
||||
delay(100);
|
||||
}
|
||||
// Cursor verplaatsen naar linkerbovenhoek
|
||||
display.setCursor(0, 0);
|
||||
// Tekst tonen dat het initialiseren gelukt is
|
||||
display.println("CAN BUS Shield init ok!");
|
||||
// Tekst naar het display wegschrijven
|
||||
display.display();
|
||||
// Display-buffer leegmaken
|
||||
display.clearDisplay();
|
||||
|
||||
// Initialisatie interrupt
|
||||
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), canIdToReadChange, RISING);
|
||||
|
||||
// Pauze om de initialisatie-tekst op het scherm te kunnen lezen
|
||||
delay(500);
|
||||
}
|
||||
|
||||
// LOOP
|
||||
void loop()
|
||||
{
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
|
||||
if (CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
|
||||
{
|
||||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
|
||||
unsigned int canId = CAN.getCanId();
|
||||
|
||||
|
||||
if (canId == canIdToRead)
|
||||
{
|
||||
bufDisp[0] = buf[0];
|
||||
bufDisp[1] = buf[1];
|
||||
bufDisp[2] = buf[2];
|
||||
bufDisp[3] = buf[3];
|
||||
bufDisp[4] = buf[4];
|
||||
bufDisp[5] = buf[5];
|
||||
bufDisp[6] = buf[6];
|
||||
bufDisp[7] = buf[7];
|
||||
bufDisp[8] = buf[8];
|
||||
canIdDisp = canId;
|
||||
lenDisp = len;
|
||||
telDisp++;
|
||||
}
|
||||
display.setCursor(0, 0);
|
||||
display.println(" SEL CUR DIS");
|
||||
display.print("ID: ");
|
||||
display.print(canIdToRead, HEX);
|
||||
display.print(" ");
|
||||
display.print(canId, HEX);
|
||||
display.print(" ");
|
||||
display.println(canIdDisp, HEX);
|
||||
display.println();
|
||||
|
||||
for (int i = 0; i < lenDisp; i++) // print the data
|
||||
{
|
||||
if (i == 3)
|
||||
{
|
||||
display.println(bufDisp[i], HEX);
|
||||
} else {
|
||||
display.print(bufDisp[i], HEX);
|
||||
display.print(" ");
|
||||
}
|
||||
}
|
||||
display.println();
|
||||
display.println();
|
||||
display.print("Count: ");
|
||||
display.println(telDisp);
|
||||
display.print("Total: ");
|
||||
display.print(tel);
|
||||
display.display();
|
||||
display.clearDisplay();
|
||||
tel++;
|
||||
if (tel > 999999) {
|
||||
tel = 0;
|
||||
}
|
||||
} else if (tel == 0)
|
||||
{
|
||||
display.setCursor(0, 0);
|
||||
display.println(" SEL CUR DIS");
|
||||
display.print("ID: ");
|
||||
display.print(canIdToRead, HEX);
|
||||
display.println();
|
||||
display.println();
|
||||
display.println();
|
||||
display.println();
|
||||
display.print("Count: ");
|
||||
display.println(telDisp);
|
||||
display.print("Total: ");
|
||||
display.print(tel);
|
||||
display.display();
|
||||
display.clearDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void canIdToReadChange() {
|
||||
canIdCount++;
|
||||
if (canIdCount > CAN_ID_COUNT) {
|
||||
canIdCount = 0;
|
||||
}
|
||||
canIdToRead = canIdToReadArray[canIdCount];
|
||||
telDisp = 0;
|
||||
tel = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user