This commit is contained in:
Meydin87
2022-09-04 07:22:05 +02:00
parent f9ce4589b9
commit 204d7580c3
10 changed files with 1607 additions and 0 deletions

34
Software/Protocol.txt Normal file
View File

@@ -0,0 +1,34 @@
MeyCan: PROTOCOL
ArbitartionID:
|<-- 13 bits -->| |<-- 16 bits -->|
COMMAND DEVICE_ID
ABC 0x14 6A
COMMAND: The Command or data type transfered. -> 2^11 = 2048 Package-Types
DEVICE_ID: A unique id for the device within the CAN Network (Home) -> 2^18 bit = 65.536 devices allowed
Packages
---------------------------------
0xF00: HELLO->ADOPT
Payload:
2 bytes | DeviceId -> DeviceId Explicit
2 bytes | Byte -> Software-Version (1.0)
2 bytes | Byte -> Hw Version (4.0
0x050: Input Switch Triggered
-> Broadcast
---------------------------------
Payload:
1 byte| PinId -> Id of the pin of the local board.
1 byte| State -> 1 for Switched, 0 for Unswitched
0x055: Command Set Switch
-> Broadcast
---------------------------------
2 bytes | TargetDeviceId -> The id of the board
1 byte | PinId -> Idof the pin of the board
1 byte | state -> 1 to swtich on, 0 to switch off

View File

@@ -0,0 +1,115 @@
#include <SPI.h>;
#include <mcp2515.h>;
struct can_frame _frame;
MCP2515 mcp2515(10);
bool flag = false;
void setup() {
Serial.begin(9600);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(A3, OUTPUT);
}
int i = 0;
bool debugState = false;
void SendCanPackage(bool state)
{
_frame.can_id = 0x001;
_frame.can_dlc = 4;
_frame.data[0] = 5;
_frame.data[1] = state;
_frame.data[2] = 0x00;
_frame.data[3] = 0x00;
mcp2515.sendMessage(&_frame);
}
void loop() {
int analogValue = analogRead(A3);
if (analogValue > 756 && !debugState)
{
debugState = true;
SendCanPackage(debugState);
}
if (analogValue < 300 && debugState)
{
debugState = false;
SendCanPackage(debugState);
}
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
{
int meyPinId = _frame.data[0];
int state = _frame.data[1] > 0;
Serial.println("CAN FRAME ------------");
Serial.print("0x");
Serial.println( _frame.can_id ^ CAN_EFF_FLAG, HEX);
Serial.println( _frame.can_dlc);
for (int i = 0; i < _frame.can_dlc ; i++)
{
Serial.println( _frame.data[i], HEX);
}
Serial.println("----------------------");
if (meyPinId == 1)
{
digitalWrite(2, state);
}
if (meyPinId == 2)
{
digitalWrite(3, state);
}
if (meyPinId == 3)
{
digitalWrite(4, state);
}
if (meyPinId == 4)
{
digitalWrite(5, state);
}
if (meyPinId == 5)
{
digitalWrite(6, state);
}
if (meyPinId == 6)
{
digitalWrite(7, state);
}
if (meyPinId == 7)
{
digitalWrite(8, state);
}
if (meyPinId == 8)
{
digitalWrite(9, state);
}
}
delay(20);
}