Skip to content

Commit faa6638

Browse files
author
Max 'MaxMax' Mönikes
committed
Added some examples how to use the lib
1 parent 4506435 commit faa6638

File tree

5 files changed

+395
-20
lines changed

5 files changed

+395
-20
lines changed

examples/TLE9012_Library/TLE9012_Library.ino renamed to examples/Direct_Register_Access/Direct_Register_Access.ino

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,60 @@ SOFTWARE.
2424

2525

2626
#include "TLE9012.h"
27-
//#include <Serial.h>
27+
#include "TLE9012_Makros.h"
2828

29+
#define TXPIN 17
30+
#define RXPIN 16
2931

3032
TLE9012 tle9012;
3133

32-
ntc_config_t ntc_config = {.ntc_resistance=100000,.ntc_b_value=3000};
3334

35+
/**
36+
* This Example illustrates how to use low level register access to implement functionallity with no
37+
* corresponding higher level library function.
38+
*
39+
* This example was written for a ESP32-Node MCU Development Board
40+
*/
3441

3542
void setup() {
36-
// put your setup code here, to run once:
37-
tle9012.init(&Serial2, 2000000,16,17);
43+
44+
//To keep the example short, the initilization is performed using higher level functions
45+
tle9012.init(&Serial2, 2000000,RXPIN,TXPIN);
3846
Serial.begin(115200);
3947
Serial.println("Boot completed");
4048
tle9012.wakeUp();
4149
delay(200);
42-
tle9012.setExtendedWatchdog(0);
50+
4351
tle9012.setNodeID(0, 1, 1);
4452
tle9012.writeMailboxRegister(1, 0xAA55);
4553

4654
uint16_t mailbox = tle9012.readMailboxRegister(1);
4755
if(mailbox == 0xAA55)
48-
Serial.println("Mailbox Check completed");
56+
Serial.println("Connection Check completed");
57+
58+
tle9012.writeRegisterSingle(1,PART_CONFIG,0x0FFF); // Set Part Config Register
4959

50-
tle9012.setNumberofCells(1, 12);
51-
tle9012.setTempSensorsConfig(1, 5, ntc_config);
52-
uint16_t ptconf = 0;
53-
tle9012.readRegisterSingle(1, 0x01, &ptconf);
54-
Serial.println(ptconf);
5560
}
5661

5762
void loop() {
5863

5964
// put your main code here, to run repeatedly:
60-
tle9012.readTemperatures(1);
61-
for(uint8_t n = 0; n < 5; n++)
65+
tle9012.writeRegisterBroadcast(WDOG_CNT,0x007F); // Broadcast Write Command to reset Watchdog Counter
66+
67+
tle9012.writeRegisterSingle(1, MEAS_CTRL, 0xEE61); //Start cell measurement
68+
delay(10); //Wait till measurement is completed
69+
70+
uint16_t cell0_voltage = 0;
71+
iso_uart_status_t status = isoUART_OK;
72+
73+
status = tle9012.readRegisterSingle(1,PCVM_0,&cell0_voltage); //Read cell voltage and communication status
74+
75+
//Check if the bus action was transfered successfully
76+
if(status != isoUART_OK)
6277
{
63-
Serial.println("-------------------------------------------");
64-
Serial.print("Resistance ");
65-
Serial.print(n+1);
66-
Serial.print(" : ");
67-
Serial.print(tle9012.devices[0].ntc_resistances[n]);
68-
Serial.print("\n");
69-
Serial.println("-------------------------------------------");
78+
Serial.print("Error during bus communication");
7079
}
80+
7181
delay(1000);
7282

7383
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2024 Maximilian Mönikes
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
26+
#include "TLE9012.h"
27+
28+
29+
#define N_CELLS 12
30+
#define TXPIN 17
31+
#define RXPIN 16
32+
33+
#define OVERVOLTAGE_THRESHOLD 4.2
34+
#define UNDERVOLTAGE_THRESHOLD 2.5
35+
36+
TLE9012 tle9012;
37+
38+
/**
39+
* This example shows how to register error handlers and perform periodic checks
40+
*
41+
*/
42+
43+
void setup() {
44+
45+
tle9012.init(&Serial2, 2000000,RXPIN,TXPIN); //Initialize driver with 2Mbit
46+
47+
Serial.begin(115200);
48+
Serial.println("Boot completed");
49+
50+
tle9012.wakeUp(); //Issue wakeup command
51+
delay(200); //Wait some time to ensure wakeup was completed
52+
53+
tle9012.setNodeID(0, 1, 1); //Set device address from 0 to 1 and set device 1 as final Node
54+
55+
tle9012.writeMailboxRegister(1, 0xAA55); //Write and Readback Mailbox register to check if communication works
56+
uint16_t mailbox = tle9012.readMailboxRegister(1);
57+
58+
if(mailbox == 0xAA55) //Show Successfull communication
59+
Serial.println("Connection Check completed");
60+
61+
tle9012.setNumberofCells(1, N_CELLS); //Configure the number of cells
62+
63+
tle9012.setOvervoltageThreshold(1,VOLTAGE_TO_OV_UV_LIMIT(OVERVOLTAGE_THRESHOLD)); //Set voltage Limits
64+
tle9012.setUndervoltageThreshold(1,VOLTAGE_TO_OV_UV_LIMIT(UNDERVOLTAGE_THRESHOLD));
65+
66+
tle9012.attachErrorHandler(UNDERVOLTAGE_ERROR,underVoltageErrorHandler); //Register Error Handlers
67+
tle9012.attachErrorHandler(OVERVOLTAGE_ERROR,overVoltageErrorHandler);
68+
tle9012.resetWatchdog(); //Reset Watchdog Timer
69+
70+
}
71+
72+
void loop() {
73+
74+
tle9012.resetWatchdog(); //Reset Watchdog Timer
75+
tle9012.readCellVoltages(1); //Optional -> Read out cell voltages. Errors will trigger independent of this measurement
76+
tle9012.checkErrors(1); //Check for Errors and Call Handlers
77+
78+
delay(1000);
79+
}
80+
81+
/**
82+
* @brief Error handler function that gets called in case of undervoltage errors
83+
*
84+
* @param nodeID address of the device with a fault
85+
* @param uv_flags bitmask containing what cells are below the undervoltage threshold
86+
*/
87+
void underVoltageErrorHandler(uint8_t nodeID, uint16_t uv_flags)
88+
{
89+
Serial.println("Undervoltage Error Detected");
90+
Serial.print("Undervoltage Flag Mask: ");
91+
Serial.println(uv_flags,BIN);
92+
tle9012.resetErrors(1);
93+
}
94+
95+
96+
/**
97+
* @brief Error handler function that gets called in case of overvoltage errors
98+
*
99+
* @param nodeID address of the device with a fault
100+
* @param uv_flags bitmask containing what cells are above the overvoltage threshold
101+
*/
102+
103+
void overVoltageErrorHandler(uint8_t nodeID, uint16_t ov_flags)
104+
{
105+
Serial.println("Overvoltage Error Detected");
106+
Serial.print("Overvoltage Flag Mask: ");
107+
Serial.println(ov_flags,BIN);
108+
tle9012.resetErrors(1);
109+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2024 Maximilian Mönikes
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
26+
#include "TLE9012.h"
27+
#include <math.h>
28+
29+
#define TXPIN 17
30+
#define RXPIN 16
31+
#define N_TEMPS 5
32+
33+
TLE9012 tle9012;
34+
35+
ntc_config_t ntc_cfg = {.ntc_resistance=1000,.ntc_b_value=3348 ,.basetemp = 298.15};
36+
37+
void setup() {
38+
39+
tle9012.init(&Serial2, 2000000,RXPIN,TXPIN); //Initialize driver with 2Mbit
40+
41+
Serial.begin(115200);
42+
Serial.println("Boot completed");
43+
44+
tle9012.wakeUp(); //Issue wakeup command
45+
delay(200); //Wait some time to ensure wakeup was completed
46+
47+
tle9012.setNodeID(0, 1, 1); //Set device address from 0 to 1 and set device 1 as final Node
48+
49+
tle9012.writeMailboxRegister(1, 0xAA55); //Write and Readback Mailbox register to check if communication works
50+
uint16_t mailbox = tle9012.readMailboxRegister(1);
51+
52+
if(mailbox == 0xAA55) //Show Successfull communication
53+
Serial.println("Connection Check completed");
54+
55+
tle9012.setTempSensorsConfig(1,N_TEMPS,ntc_cfg); //Set temperature sensor config
56+
tle9012.resetWatchdog(); //Reset Watchdog Timer
57+
58+
}
59+
60+
void loop() {
61+
62+
tle9012.resetWatchdog(); //Reset Watchdog Timer
63+
tle9012.readTemperatures(1); //Measure Temperatures
64+
65+
for(uint8_t n = 0; n < N_TEMPS; n++) //Convert and Print Temperature over Serial
66+
{
67+
float ntc_resistance = NTC_MEASUREMENT_TO_R(tle9012.devices[0].ntc_resistances[n],100); //get ntc resistance assuming 100 Ohm Rf for NTC
68+
Serial.print("NTC Resistance: ");
69+
Serial.print(ntc_resistance);
70+
Serial.print("Ohm | Temperature: ");
71+
float temperature = r_to_temp(ntc_cfg,ntc_resistance); //Calculate temperature from ntc resistance
72+
Serial.print(temperature);
73+
Serial.println("°C");
74+
}
75+
Serial.println("-----------------------------------------------------------------------");
76+
77+
delay(1000);
78+
}
79+
80+
/**
81+
* This function is deliberatly not included in the library to prevent a forced inclusion of math.h
82+
*/
83+
84+
float r_to_temp(ntc_config_t cfg, float resistance)
85+
{
86+
return 1/((log(resistance/cfg.ntc_resistance)/cfg.ntc_b_value) + (1/cfg.basetemp))-273.15;
87+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2024 Maximilian Mönikes
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
26+
#include "TLE9012.h"
27+
28+
#define N_CELLS 12
29+
#define TXPIN 17
30+
#define RXPIN 16
31+
32+
TLE9012 tle9012;
33+
34+
/**
35+
* This Example shows how to do a basic configuration of a single TLE9012 and read cell voltages
36+
*
37+
*/
38+
39+
void setup() {
40+
41+
42+
tle9012.init(&Serial2, 2000000,RXPIN,TXPIN); //Initialize driver with 2Mbit
43+
44+
Serial.begin(115200);
45+
Serial.println("Boot completed");
46+
47+
tle9012.wakeUp(); //Issue wakeup command
48+
delay(200); //Wait some time to ensure wakeup was completed
49+
50+
tle9012.setNodeID(0, 1, 1); //Set device address from 0 to 1 and set device 1 as final Node
51+
52+
tle9012.writeMailboxRegister(1, 0xAA55); //Write and Readback Mailbox register to check if communication works
53+
uint16_t mailbox = tle9012.readMailboxRegister(1);
54+
55+
if(mailbox == 0xAA55) //Show Successfull communication
56+
Serial.println("Connection Check completed");
57+
58+
tle9012.setNumberofCells(1, N_CELLS); //Configure the number of cells
59+
tle9012.resetWatchdog(); //Reset Watchdog Timer
60+
61+
}
62+
63+
void loop() {
64+
65+
tle9012.resetWatchdog(); //Reset Watchdog Timer
66+
tle9012.readCellVoltages(1); //Read all cell voltages from device 1
67+
68+
for(uint8_t n = 0; n < N_CELLS; n++) //Print cell voltages to Serial Monitor
69+
{
70+
Serial.print("Cell Voltage ");
71+
Serial.print(n+1);
72+
Serial.print(": ");
73+
Serial.println(ADCVALUE_TO_FLOAT_VOLTAGE(tle9012.devices[0].cell_voltages[n]));
74+
}
75+
76+
Serial.println("-------------------------------------------------");
77+
78+
delay(1000);
79+
80+
}

0 commit comments

Comments
 (0)