#include <mcp_can.h>
#include <mcp_can_dfs.h>
#include <SPI.h>
byte NTEMPHight;
byte NTEMPLow;
byte TOPLHight;
byte TOPLLow;
bool DMASL;
bool ZAKB;
bool IGN;
bool BK;
bool RBEZ;
bool RUCHN;
bool UTORM;
unsigned int CAN_ID;
byte Byte_01;
byte Byte_02;
byte Byte_03;
byte Byte_04;
byte Byte_05;
byte Byte_06;
byte Byte_07;
byte Byte_08;
const int SPI_CS_PIN= 10;
MCP_CAN CAN(SPI_CS_PIN);
byte _WordToBytes_1_HBO = 0;
byte _WordToBytes_1_LBO = 0;
byte _WordToBytes_2_HBO = 0;
byte _WordToBytes_2_LBO = 0;
void setup()
{
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(2, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
// init serial
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS))
{
delay(1);// ожидаем конекта
}
}
void loop()
{int _tempVariable_int;
//Плата:1
_tempVariable_int = (analogRead (1));
_WordToBytes_1_HBO = highByte(_tempVariable_int);
_WordToBytes_1_LBO = lowByte(_tempVariable_int);
_tempVariable_int = (map(( (analogRead (0))), (250), (785), (50), (0)));
_WordToBytes_2_HBO = highByte(_tempVariable_int);
_WordToBytes_2_LBO = lowByte(_tempVariable_int);
NTEMPHight = _WordToBytes_1_HBO;
NTEMPLow = _WordToBytes_1_LBO;
TOPLHight = _WordToBytes_2_HBO;
TOPLLow = _WordToBytes_2_LBO;
DMASL = !( (digitalRead (6)));
ZAKB = (digitalRead (2));
IGN = (digitalRead (9));
BK = !( (digitalRead (8)));
RBEZ = !( (digitalRead (7)));
RUCHN = !( (digitalRead (4)));
UTORM = !( (digitalRead (5)));
unsigned char len = 0;
byte buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
CAN_ID = CAN.getCanId();
Byte_01 = buf[0];
Byte_02 = buf[1];
Byte_03 = buf[2];
Byte_04 = buf[3];
Byte_05 = buf[4];
Byte_06 = buf[5];
Byte_07 = buf[6];
Byte_08 = buf[7];
}
SendCANFramesToSerial();
}
void SendCANFramesToSerial()
{
byte buf[8];
//Please note that before sending a message I check whether my MCP2515 is on the identifier now, if I don’t do this focus then the 8 bytes received from mcp2515 can go to any of the send addresses) which will cause a twitching effect and cause artifacts on the dashboard (unpredictable data jumps) people write about this and in other branches of this forum this is obvious)))
//rpm
if (CAN_ID == 0x186)
{
buf[0] = (Byte_01 & 0xff);
buf[1] = (Byte_02 & 0xff);
buf[2] = (Byte_06 & 0xff); //TPS
buf[3] = (Byte_07 & 0xff);
SendCANFrameToSerial(390, buf);
}
// Speed AUTO divided by 10 000 Since
// centimeters per hour come from the car
if (CAN_ID == 0x217)
{
buf[0] = (Byte_04 & 0xff);
buf[1] = (Byte_05 & 0xff);
buf[2] = (Byte_06 & 0xff);
SendCANFrameToSerial(535, buf);
}
//ICE temperature “-40”
if (CAN_ID == 0x5DA)
{
buf[0] = (Byte_01 & 0xff);
SendCANFrameToSerial(1498, buf);
}
//Battery voltage “/10”
if (CAN_ID == 0x4AC)
{
buf[0] = (Byte_02 & 0xff);
SendCANFrameToSerial(1196, buf);
}
//SHINE
if (CAN_ID == 0x5DE)
{
buf[0] = (Byte_01 & 0xff); //левый поворот
buf[1] = (Byte_01 & 0xff); //правый поворот
buf[2] = (Byte_01 & 0xff); //дальний свет
buf[3] = (Byte_01 & 0xff); //габариты бит 02
buf[4] = (Byte_01 & 0xff); //ближний
//buf[5] = (Byte_01 & 0xff); //аварийка
buf[6] = (Byte_01 & 0xff); //ПТФ
buf[7] = (Byte_01 & 0xff); //ЗТФ
SendCANFrameToSerial(1502, buf);
}
//Reverse
if (CAN_ID == 0x648)
{
buf[0] = (Byte_07 & 0xff);
SendCANFrameToSerial(1608, buf);
}
//MILEAGE
if (CAN_ID == 0x5D7)
{
buf[0] = (Byte_03 & 0xff);
buf[1] = (Byte_04 & 0xff);
buf[2] = (Byte_05 & 0xff);
buf[3] = (Byte_06 & 0xff);
SendCANFrameToSerial(1495, buf);
}
//DOORS
if (CAN_ID == 0x5DE)
{
buf[0] = (Byte_02 & 0xff); //ВОДИТЕЛЬ 3й бит
buf[1] = (Byte_02 & 0xff); //ПАССАЖИР 1й бит
SendCANFrameToSerial(1505, buf);
}
//HEATING AND CLUTCH
if (CAN_ID == 0x5DD)
{
buf[0] = (Byte_03 & 0xff); // обогрев 3й бит
buf[1] = (Byte_03 & 0xff); // сцепление 5й бит
SendCANFrameToSerial(1501, buf);
}
// PETROL
buf[0] = (TOPLHight & 0xff);
buf[1] = (TOPLLow & 0xff);
SendCANFrameToSerial(2001, buf);
// OUTDOOR TEMPERATURE
buf[0] = (NTEMPHight & 0xff);
buf[1] = (NTEMPLow & 0xff);
SendCANFrameToSerial(2002, buf);
// ANALOGUE VALUES
buf[0] = (DMASL & 0xff);
buf[1] = (ZAKB & 0xff);
buf[2] = (IGN & 0xff);
buf[3] = (BK & 0xff);
buf[4] = (RBEZ & 0xff);
buf[5] = (RUCHN & 0xff);
buf[6] = (UTORM & 0xff);
SendCANFrameToSerial(2003, buf);
}
void SendCANFrameToSerial(unsigned long canFrameId, const byte* frameData)
{
// the 4 byte identifier at the beginning of each CAN frame
// this is required for RealDash to ‘catch-up’ on ongoing stream of CAN frames
const unsigned long serialBlockTag = 0x11223344;
Serial.write((const byte*)&serialBlockTag, 4);
//the CAN frame id number
Serial.write((const byte*)&canFrameId, 4);
// CAN frame payload
Serial.write(frameData, 8);
}