#include "lin_bus.h"
// Create an IntervalTimer object
IntervalTimer myTimer;
int ledState = LOW; // ledState used to set the LED
unsigned long interval = 200000; // interval at which to blinkLED to run every 0.2 seconds
LIN lin;
int lin_cs = 32; // cs and serial port set for skpang LIN / FDCAN board
int led1 = 23; // sk pang board builtin led
int lin_fault = 28;
uint8_t CRC = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(lin_fault, INPUT);
pinMode(lin_cs, OUTPUT);
digitalWrite(lin_cs, HIGH); // enable MCP2004 LIN transceiver
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
//Serial.print("Heater demo");
lin.begin(&Serial3, 19200);
delay(100); // wait 100ms
pinMode(led1, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
myTimer.begin(blinkLED, interval);
}
void loop() {
// heater
SendLin();
delay(100); // wait 100ms
//Serial.print(" Heater test\n");
}
void blinkLED() {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
digitalWrite(led1, ledState);
}
static void SendLin()
{
//static bool read = true;
uint8_t data[4];
delay(100); // wait 100ms
// read ID27 register
lin.response(0x27, data, 7, lin2x); //
delay(10);
// write ID33 register
uint8_t lindata0[] = { 0x00, 0x10 }; // chechksum 0, counter 0, HvCooltHeatrEnadWhE2EHvchEnad = 1
lin.order(0x33, lindata0, 2, lin2x);
delay(10);
// write ID22 register, HvWtrHeatrPwrCnsAllwd = 0
uint8_t lindata1[] = { 0x00, 0x78, 0xFF, 0xD0 }; // 0W power, 80C target temp, max flow, enable
lin.order(0x22, lindata1, 4, lin2x);
delay(10);
// write ID22 register again, HvWtrHeatrPwrCnsAllwd = 1000W
uint8_t lindata2[] = { 0xB4, 0x78, 0xFF, 0xD0 }; // 7200w power, 80C target temp, max flow, enable
lin.order(0x22, lindata2, 4, lin2x);
delay(10);
// read ID17 register
lin.response(0x17, data, 3, lin2x); // we need 10+10 bits according to spec, so request 3 bytes
Serial.print("responseID:");
Serial.print(0x17, HEX);
Serial.print(",");
Serial.print("Response Data:");
for (int i = 0; i < 3; i++) { // display received data
Serial.printf("0x%.2X ", data[i]);
Serial.println("");
}
// read ID27 register
lin.response(0x27, data, 7, lin2x); //
Serial.print("responseID:");
Serial.print(0x27, HEX);
Serial.print(",");
Serial.print("Response Data:");
for (int i = 0; i < 7; i++) { // display received data
Serial.printf("0x%.2X ", data[i]);
Serial.println("");
}
delay(500);
}