Please provide me solution using TLE7259 for LIN BUS using arduino nano
If you search the web with your argument:
"TLE7259 for LIN BUS using arduino"
will find the solution.
"Who seeks, finds"
1 Like
If you do not fine then post an annotated schematic of your circuit showing all connections, power and ground. Also post your code.
My Master Code
#include <LIN_master.h>
// Define LIN ID for power window control
#define LIN_ID_POWER_WINDOW 0x15
// Define data values for power window control
#define POWER_WINDOW_UP_CMD 0x01
#define POWER_WINDOW_DOWN_CMD 0x02
// Define pin numbers for buttons
#define BUTTON_FR_LEFT_PIN 2
#define BUTTON_FR_RIGHT_PIN 3
#define BUTTON_RR_LEFT_PIN 4
#define BUTTON_RR_RIGHT_PIN 5
// Instantiate LIN_Master object
LIN_Master linMaster;
void setup() {
// Begin serial communication
Serial.begin(9600);
// Initialize LIN communication
linMaster.begin(19200, LIN_V2, false); // Baudrate 19200, LIN version 2, no background operation
// Initialize button pins as inputs
pinMode(BUTTON_FR_LEFT_PIN, INPUT_PULLUP);
pinMode(BUTTON_FR_RIGHT_PIN, INPUT_PULLUP);
pinMode(BUTTON_RR_LEFT_PIN, INPUT_PULLUP);
pinMode(BUTTON_RR_RIGHT_PIN, INPUT_PULLUP);
}
void loop() {
// Read current state of buttons
bool currentButtonStateFrLeft = digitalRead(BUTTON_FR_LEFT_PIN);
bool currentButtonStateFrRight = digitalRead(BUTTON_FR_RIGHT_PIN);
bool currentButtonStateRrLeft = digitalRead(BUTTON_RR_LEFT_PIN);
bool currentButtonStateRrRight = digitalRead(BUTTON_RR_RIGHT_PIN);
// Check if button FR_LEFT is pressed (active high)
if (currentButtonStateFrLeft == HIGH) {
sendPowerWindowCommand(0x01, POWER_WINDOW_UP_CMD); // Start bit: 0x01
} else {
sendPowerWindowCommand(0x01, POWER_WINDOW_DOWN_CMD); // Start bit: 0x01
}
// Check if button FR_RIGHT is pressed (active high)
if (currentButtonStateFrRight == HIGH) {
sendPowerWindowCommand(0x02, POWER_WINDOW_UP_CMD); // Start bit: 0x02
} else {
sendPowerWindowCommand(0x02, POWER_WINDOW_DOWN_CMD); // Start bit: 0x02
}
// Check if button RR_LEFT is pressed (active high)
if (currentButtonStateRrLeft == HIGH) {
sendPowerWindowCommand(0x03, POWER_WINDOW_UP_CMD); // Start bit: 0x03
} else {
sendPowerWindowCommand(0x03, POWER_WINDOW_DOWN_CMD); // Start bit: 0x03
}
// Check if button RR_RIGHT is pressed (active high)
if (currentButtonStateRrRight == HIGH) {
sendPowerWindowCommand(0x04, POWER_WINDOW_UP_CMD); // Start bit: 0x04
} else {
sendPowerWindowCommand(0x04, POWER_WINDOW_DOWN_CMD); // Start bit: 0x04
}
// Delay to debounce buttons
delay(50);
// Receive response from slave
receivePowerWindowResponse();
}
// Function to send power window control command via LIN
void sendPowerWindowCommand(uint8_t startBit, uint8_t command) {
// Data to be sent
uint8_t data[] = {startBit, 0x01, command}; // Start bit, Data length, Command
// Send a master request frame with LIN header
linMaster.sendMasterRequest(LIN_ID_POWER_WINDOW, sizeof(data), data);
// Print the data being sent
Serial.print("Sent LIN frame: ");
for (int i = 0; i < sizeof(data); i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println();
}
// Function to receive power window response from LIN slave
void receivePowerWindowResponse() {
// Buffer to store received data
uint8_t receivedData[8];
// Receive slave response frame
LIN_error_t error = linMaster.receiveSlaveResponse(LIN_ID_POWER_WINDOW, sizeof(receivedData), receivedData);
// Check for errors
if (error == LIN_SUCCESS) {
// Print received data
Serial.print("Received LIN response: ");
for (int i = 0; i < sizeof(receivedData); i++) {
Serial.print(receivedData[i], HEX);
Serial.print(" ");
}
Serial.println();
} else {
// Print error message
Serial.println("Error receiving LIN response");
}
}
My Slave Code
#include <LINBus_stack.h>
// Define LIN ID for power window control
#define LIN_ID_POWER_WINDOW 0x15
// Define data values for power window control
#define POWER_WINDOW_UP_CMD 0x01
#define POWER_WINDOW_DOWN_CMD 0x02
// Define pin numbers for controlling power windows
#define WINDOW_FR_LEFT_PIN 6
#define WINDOW_FR_RIGHT_PIN 7
#define WINDOW_RR_LEFT_PIN 8
#define WINDOW_RR_RIGHT_PIN 9
// Instantiate LINBus_stack object
LINBus_stack linBus;
void setup() {
// Begin serial communication
Serial.begin(9600);
// Initialize LIN communication
linBus.begin(19200); // Baudrate 19200
// Initialize pins for power windows
pinMode(WINDOW_FR_LEFT_PIN, OUTPUT);
pinMode(WINDOW_FR_RIGHT_PIN, OUTPUT);
pinMode(WINDOW_RR_LEFT_PIN, OUTPUT);
pinMode(WINDOW_RR_RIGHT_PIN, OUTPUT);
}
void loop() {
// Buffer to store received data
uint8_t receivedData[8];
size_t dataLength = 0;
// Receive master request frame
if (linBus.read(receivedData, 8, &dataLength)) {
// Check if the received frame is valid and matches LIN ID for power window control
if (dataLength > 1 && receivedData[0] == LIN_ID_POWER_WINDOW) {
// Extract command
uint8_t command = receivedData[2];
// Process command based on start bit
switch (receivedData[1]) {
case 0x01: // Start bit for FR_LEFT
if (command == POWER_WINDOW_UP_CMD) {
// Move FR_LEFT power window up
digitalWrite(WINDOW_FR_LEFT_PIN, HIGH);
} else if (command == POWER_WINDOW_DOWN_CMD) {
// Move FR_LEFT power window down
digitalWrite(WINDOW_FR_LEFT_PIN, LOW);
}
break;
case 0x02: // Start bit for FR_RIGHT
if (command == POWER_WINDOW_UP_CMD) {
// Move FR_RIGHT power window up
digitalWrite(WINDOW_FR_RIGHT_PIN, HIGH);
} else if (command == POWER_WINDOW_DOWN_CMD) {
// Move FR_RIGHT power window down
digitalWrite(WINDOW_FR_RIGHT_PIN, LOW);
}
break;
case 0x03: // Start bit for RR_LEFT
if (command == POWER_WINDOW_UP_CMD) {
// Move RR_LEFT power window up
digitalWrite(WINDOW_RR_LEFT_PIN, HIGH);
} else if (command == POWER_WINDOW_DOWN_CMD) {
// Move RR_LEFT power window down
digitalWrite(WINDOW_RR_LEFT_PIN, LOW);
}
break;
case 0x04: // Start bit for RR_RIGHT
if (command == POWER_WINDOW_UP_CMD) {
// Move RR_RIGHT power window up
digitalWrite(WINDOW_RR_RIGHT_PIN, HIGH);
} else if (command == POWER_WINDOW_DOWN_CMD) {
// Move RR_RIGHT power window down
digitalWrite(WINDOW_RR_RIGHT_PIN, LOW);
}
break;
default:
// Invalid start bit
break;
}
}
}
}
Your schematic is correct but I doubt you have it wired exactly that way. Have you followed @ruilviana suggestion, it worked for me.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.