HVAC_DEV_V2_DIG_POT/dig_pot.c

184 lines
7.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by cfif on 29.05.2026.
//
#include "dig_pot.h"
// Потенциометр 100 кОм
static const uint32_t pot_100k[256] = {
// 0-9
142, 518, 897, 1275, 1653, 1974, 2312, 2650, 2988, 3326,
// 10-19
3918, 4296, 4674, 5052, 5430, 5808, 6186, 6564, 6942, 7320,
// 20-29
7640, 8020, 8400, 8780, 9160, 9540, 9920, 10300, 10680, 11060,
// 30-39
11400, 11775, 12150, 12525, 12900, 13275, 13650, 14025, 14400, 14775,
// 40-49
15150, 15527, 15904, 16281, 16658, 17035, 17412, 17789, 18166, 18543,
// 50-59
18920, 19297, 19674, 20051, 20428, 20805, 21182, 21559, 21936, 22313,
// 60-69
22660, 23044, 23428, 23812, 24196, 24580, 24964, 25348, 25732, 26116,
// 70-79
26400, 26775, 27150, 27525, 27900, 28275, 28650, 29025, 29400, 29775,
// 80-89
30120, 30500, 30880, 31260, 31640, 32020, 32400, 32780, 33160, 33540,
// 90-99
33880, 34254, 34628, 35002, 35376, 35750, 36124, 36498, 36872, 37246,
// 100-109
37550, 37925, 38300, 38675, 39050, 39425, 39800, 40175, 40550, 40925,
// 110-119
41300, 41680, 42060, 42440, 42820, 43200, 43580, 43960, 44340, 44720,
// 120-129
44980, 45340, 45700, 46060, 46420, 46780, 47140, 47500, 47860, 48220,
// 130-139
48560, 48931, 49302, 49673, 50044, 50415, 50786, 51157, 51528, 51899,
// 140-149
52270, 52634, 52998, 53362, 53726, 54090, 54454, 54818, 55182, 55546,
// 150-159
56000, 56411, 56822, 57233, 57644, 58055, 58466, 58877, 59288, 59699,
// 160-169
59520, 59934, 60348, 60762, 61176, 61590, 62004, 62418, 62832, 63246,
// 170-179
63000, 63450, 63900, 64350, 64800, 65250, 65700, 66150, 66600, 67050,
// 180-189
66000, 66667, 67333, 68000, 68667, 69333, 70000, 70667, 71333, 72000,
// 190-199
70000, 70700, 71400, 72100, 72800, 73500, 74200, 74900, 75600, 76300,
// 200-209
73300, 74167, 75033, 75900, 76767, 77633, 78500, 79367, 80233, 81100,
// 210-219
77600, 78200, 78800, 79400, 80000, 80600, 81200, 81800, 82400, 83000,
// 220-229
80600, 81200, 81800, 82400, 83000, 83600, 84200, 84800, 85400, 86000,
// 230-239
84000, 84600, 85200, 85800, 86400, 87000, 87600, 88200, 88800, 89400,
// 240-249
87000, 87500, 88000, 88500, 89000, 89500, 90000, 90500, 91000, 91500,
// 250-255
90500, 90900, 91300, 91700, 92100, 93400
};
// Потенциометр 10 кОм
static const uint32_t pot_10k[256] = {
// 0-9
142, 183, 222, 264, 305, 346, 387, 428, 469, 510,
// 10-19
548, 589, 629, 670, 711, 752, 793, 834, 875, 916,
// 20-29
940, 979, 1018, 1057, 1096, 1135, 1174, 1213, 1252, 1291,
// 30-39
1347, 1386, 1425, 1464, 1503, 1542, 1581, 1620, 1659, 1698,
// 40-49
1736, 1775, 1814, 1853, 1892, 1931, 1970, 2009, 2048, 2087,
// 50-59
2126, 2165, 2204, 2243, 2282, 2321, 2360, 2399, 2438, 2477,
// 60-69
2533, 2572, 2611, 2650, 2689, 2728, 2767, 2806, 2845, 2884,
// 70-79
2924, 2963, 3002, 3041, 3080, 3119, 3158, 3197, 3236, 3275,
// 80-89
3313, 3352, 3391, 3430, 3469, 3508, 3547, 3586, 3625, 3664,
// 90-99
3720, 3759, 3798, 3837, 3876, 3915, 3954, 3993, 4032, 4071,
// 100-109
4112, 4152, 4192, 4232, 4272, 4312, 4352, 4392, 4432, 4472,
// 110-119
4518, 4558, 4598, 4638, 4678, 4718, 4758, 4798, 4838, 4878,
// 120-129
4906, 4946, 4986, 5026, 5066, 5106, 5146, 5186, 5226, 5266,
// 130-139
5296, 5336, 5376, 5416, 5456, 5496, 5536, 5576, 5616, 5656,
// 140-149
5704, 5744, 5784, 5824, 5864, 5904, 5944, 5984, 6024, 6064,
// 150-159
6097, 6137, 6177, 6217, 6257, 6297, 6337, 6377, 6417, 6457,
// 160-169
6460, 6500, 6540, 6580, 6620, 6660, 6700, 6740, 6780, 6820,
// 170-179
6860, 6900, 6940, 6980, 7020, 7060, 7100, 7140, 7180, 7220,
// 180-189
7260, 7300, 7340, 7380, 7420, 7460, 7500, 7540, 7580, 7620,
// 190-199
7660, 7700, 7740, 7780, 7820, 7860, 7900, 7940, 7980, 8020,
// 200-209
8050, 8090, 8130, 8170, 8210, 8250, 8290, 8330, 8370, 8410,
// 210-219
8440, 8480, 8520, 8560, 8600, 8640, 8680, 8720, 8760, 8800,
// 220-229
8840, 8880, 8920, 8960, 9000, 9040, 9080, 9120, 9160, 9200,
// 230-239
9220, 9260, 9300, 9340, 9380, 9420, 9460, 9500, 9540, 9580,
// 240-249
9620, 9660, 9700, 9740, 9780, 9820, 9860, 9900, 9940, 9980,
// 250-255
10020, 10040, 10060, 10080, 10180, 10200
};
#define R_CORRECTION 120 // вычитаемая поправка (Ом)
uint32_t find_steps_for_resistance(uint32_t target_ohm, uint8_t *step_100k, uint8_t *step_10k) {
uint32_t best_r = 0;
uint32_t best_diff = 0xFFFFFFFF;
uint8_t best_n100 = 0, best_n10 = 0;
for (uint16_t n100 = 0; n100 < 256; n100++) {
uint32_t r100 = pot_100k[n100];
for (uint16_t n10 = 0; n10 < 256; n10++) {
uint32_t r10 = pot_10k[n10];
uint32_t r_total = r100 + r10;
r_total -= R_CORRECTION;
uint32_t diff = (r_total > target_ohm) ? (r_total - target_ohm) : (target_ohm - r_total);
if (diff < best_diff) {
best_diff = diff;
best_r = r_total;
best_n100 = n100;
best_n10 = n10;
if (diff == 0) {
*step_100k = best_n100;
*step_10k = best_n10;
return best_r;
}
}
}
}
*step_100k = best_n100;
*step_10k = best_n10;
return best_r;
}
// Функция получения сопротивления по температуре с линейной интерполяцией
uint32_t get_resistance_by_temperature_linear(const TablePoint *table, int table_size, int16_t temperature) {
// Проверка границ
if (temperature <= table[table_size - 1].temp) {
return (uint32_t)table[table_size - 1].resistance;
}
if (temperature >= table[0].temp) {
return (uint32_t)table[0].resistance;
}
// Поиск интервала по температуре (таблица отсортирована по убыванию температуры)
int left = 0;
int right = table_size - 1;
for (int i = 0; i < table_size - 1; i++) {
if (temperature <= table[i].temp && temperature >= table[i + 1].temp) {
left = i;
right = i + 1;
break;
}
}
// Линейная интерполяция сопротивления
float t = ((float)temperature - (float)table[left].temp) /
((float)table[right].temp - (float)table[left].temp);
return (uint32_t)(table[left].resistance + t * (table[right].resistance - table[left].resistance));
}