43#define CHIP_NAME "General NTC"
44#define MANUFACTURER_NAME "General"
45#define SUPPLY_VOLTAGE_MIN 1.8f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 1.0f
48#define TEMPERATURE_MIN -50.0f
49#define TEMPERATURE_MAX 250.0f
50#define DRIVER_VERSION 1000
59static float a_ntc_beta_formula(
ntc_handle_t *handle,
float ohm)
69 temp_k = 1.0f / ((1.0f / t0) + logf(ohm / r0) / b);
71 return temp_k - 273.15f;
81static float a_ntc_steinhart_hart(
ntc_handle_t *handle,
float ohm)
93 log_r = log((
double)ohm);
94 log_r3 = log_r * log_r * log_r;
95 temp_k = 1.0 / (a + b * log_r + c * log_r3);
97 return (
float)(temp_k - 273.15);
107static float a_ntc_table(
ntc_handle_t *handle,
float ohm)
118 mid = (low + high) / 2;
123 else if (ohm < handle->table[mid].ohm)
151static uint8_t a_ntc_convert(
ntc_handle_t *handle,
float ohm,
float *degrees_celsius)
155 *degrees_celsius = a_ntc_beta_formula(handle, ohm);
161 *degrees_celsius = a_ntc_steinhart_hart(handle, ohm);
176 if (ohm < handle->table[handle->
table_len - 1].ohm)
183 *degrees_celsius = a_ntc_table(handle, ohm);
206static uint8_t a_ntc_filter(
ntc_handle_t *handle, uint16_t input_len,
float *output)
212 handle->
debug_print(
"ntc: no filter length must be 1.\n");
228 handle->
debug_print(
"ntc: first order lag filter length must be 1.\n");
254 *output = sum / sum2;
265 if ((input_len % 2) == 0)
267 handle->
debug_print(
"ntc: median filter length must be odd number.\n");
273 handle->
debug_print(
"ntc: median filter length < 3.\n");
278 for (j = 0; j < input_len - 1; j++)
280 for (i = 0; i < input_len - 1 - j; i++)
291 *output = handle->
buf_flt[(input_len - 1) / 2];
304 handle->
debug_print(
"ntc: anti spike average filter < 3.\n");
309 for (j = 0; j < input_len - 1; j++)
311 for (i = 0; i < input_len - 1 - j; i++)
323 for (i = 1; i < input_len - 1; i++)
327 *output = sum / (float)(input_len - 2);
338 handle->
debug_print(
"ntc: moving average filter length must be 1.\n");
374 handle->
debug_print(
"ntc: moving average filter length must be 1.\n");
400 *output = sum / sum2;
412 handle->
debug_print(
"ntc: filter param invalid.\n");
431 if (a_ntc_convert(handle, handle->
cache_flt[0], °1) != 0)
435 if (a_ntc_convert(handle, handle->
buf_flt[0], °2) != 0)
439 if (fabsf(deg1 - deg2) > handle->
param_flt[0])
455 handle->
debug_print(
"ntc: filter param invalid.\n");
485static uint8_t a_ntc_adc(
ntc_handle_t *handle, uint16_t len, uint8_t *open_short_circuit)
494 *open_short_circuit = 0;
501 for (i = 0; i < len; i++)
503 if (handle->
buf[i] == 0)
505 *open_short_circuit = 1;
512 *open_short_circuit = 2;
521 (float)handle->
buf[i]) - 1.0f);
524 *open_short_circuit = 0;
530 for (i = 0; i < len; i++)
532 if (handle->
buf[i] == 0)
534 *open_short_circuit = 2;
541 *open_short_circuit = 1;
550 (float)handle->
buf[i])- 1.0f);
553 *open_short_circuit = 0;
559 handle->
debug_print(
"ntc: invalid circuit type.\n");
560 *open_short_circuit = 0;
587 handle->
circuit = (uint8_t)(circuit);
684 handle->
algorithm = (uint8_t)(algorithm);
762 *beta = handle->
beta;
896 float degrees_celsius;
916 for (i = 1; i < table_len; i++)
918 if (table[i].ohm > ohm)
920 handle->
debug_print(
"ntc: table[%d] ohm is invalid.\n", i);
924 if (table[i].degrees_celsius < degrees_celsius)
926 handle->
debug_print(
"ntc: table[%d] degrees celsius is invalid.\n", i);
934 handle->
table = table;
961 handle->
filter = (uint8_t)(filter);
1007 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < 2.\n");
1047 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length);
1051 if ((length % 2) == 0)
1053 handle->
debug_print(
"ntc: median filter length must be odd number.\n");
1085 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length);
1117 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length);
1151 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length);
1162 for (i = 0; i < length; i++)
1187 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < 2.\n");
1203 float q_process_noise_covariance,
1204 float r_measurement_noise_covariance,
1205 float p_estimation_error_covariance,
1206 float x_estimated_value)
1209 uint8_t open_short_circuit;
1221 handle->
debug_print(
"ntc: NTC_FILTER_BUFFER_SIZE < 3.\n");
1225 if (isnan(x_estimated_value) != 0)
1227 res = a_ntc_adc(handle, 1, &open_short_circuit);
1232 x_estimated_value = handle->
buf_flt[0];
1237 handle->
param_flt[0] = q_process_noise_covariance;
1238 handle->
param_flt[1] = r_measurement_noise_covariance;
1239 handle->
cache_flt[0] = x_estimated_value;
1240 handle->
cache_flt[1] = p_estimation_error_covariance;
1303 handle->
debug_print(
"ntc: adc_deinit is null.\n");
1329 handle->
debug_print(
"ntc: vcc counter is invalid.\n");
1337 handle->
beta = 1.0f;
1342 handle->
table = NULL;
1413 uint8_t open_short_circuit;
1426 res = a_ntc_adc(handle, 1, &open_short_circuit);
1429 if (open_short_circuit != 0)
1431 if (open_short_circuit == 1)
1446 res = a_ntc_filter(handle, 1, ohm);
1452 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1462 res = a_ntc_adc(handle, 1, &open_short_circuit);
1465 if (open_short_circuit != 0)
1467 if (open_short_circuit == 1)
1482 res = a_ntc_filter(handle, 1, ohm);
1488 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1498 res = a_ntc_adc(handle, handle->
filter_len, &open_short_circuit);
1501 if (open_short_circuit != 0)
1503 if (open_short_circuit == 1)
1518 res = a_ntc_filter(handle, handle->
filter_len, ohm);
1524 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1534 res = a_ntc_adc(handle, handle->
filter_len, &open_short_circuit);
1537 if (open_short_circuit != 0)
1539 if (open_short_circuit == 1)
1554 res = a_ntc_filter(handle, handle->
filter_len, ohm);
1560 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1570 res = a_ntc_adc(handle, 1, &open_short_circuit);
1573 if (open_short_circuit != 0)
1575 if (open_short_circuit == 1)
1590 res = a_ntc_filter(handle, 1, ohm);
1596 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1606 res = a_ntc_adc(handle, 1, &open_short_circuit);
1609 if (open_short_circuit != 0)
1611 if (open_short_circuit == 1)
1626 res = a_ntc_filter(handle, 1, ohm);
1632 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1642 res = a_ntc_adc(handle, 1, &open_short_circuit);
1645 if (open_short_circuit != 0)
1647 if (open_short_circuit == 1)
1662 res = a_ntc_filter(handle, 1, ohm);
1668 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1678 res = a_ntc_adc(handle, 1, &open_short_circuit);
1681 if (open_short_circuit != 0)
1683 if (open_short_circuit == 1)
1698 res = a_ntc_filter(handle, 1, ohm);
1704 res = a_ntc_convert(handle, *ohm, degrees_celsius);
1741 res = a_ntc_convert(handle, ohm, degrees_celsius);
1779 for (i = 0; i < len; i++)
1784 res = a_ntc_filter(handle, len, ohm);
1790 res = a_ntc_convert(handle, *ohm, degrees_celsius);
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
uint8_t ntc_read_temperature(ntc_handle_t *handle, float *ohm, float *degrees_celsius)
read temperature
uint8_t ntc_set_algorithm_steinhart_hart(ntc_handle_t *handle, double a, double b, double c)
set algorithm steinhart hart
uint8_t ntc_set_circuit(ntc_handle_t *handle, ntc_circuit_t circuit)
set circuit
uint8_t ntc_load_algorithm_lookup_table(ntc_handle_t *handle, const ntc_table_t *table, uint16_t table_len)
load algorithm lookup table
uint8_t ntc_set_algorithm(ntc_handle_t *handle, ntc_algorithm_t algorithm)
set algorithm
#define NTC_FILTER_BUFFER_SIZE
ntc filter buffer size definition
ntc_filter_t
ntc filter enumeration definition
uint8_t ntc_get_circuit_fixed_resistor(ntc_handle_t *handle, float *r_fixed_ohm)
get circuit fixed resistor
uint8_t ntc_info(ntc_info_t *info)
get chip's information
uint8_t ntc_set_algorithm_beta_formula_r25_ohm(ntc_handle_t *handle, float r25_ohm)
set algorithm beta formula r25 ohm
ntc_algorithm_t
ntc algorithm enumeration definition
uint8_t ntc_init(ntc_handle_t *handle)
initialize the chip
ntc_circuit_t
ntc circuit enumeration definition
uint8_t ntc_get_circuit(ntc_handle_t *handle, ntc_circuit_t *circuit)
get circuit
uint8_t ntc_get_algorithm_beta_formula_beta_value(ntc_handle_t *handle, float *beta)
get algorithm beta formula beta value
uint8_t ntc_calculate_temperature_with_filter(ntc_handle_t *handle, float *ohm, uint16_t len, float *degrees_celsius)
calculate temperature with filter
struct ntc_info_s ntc_info_t
ntc information structure definition
uint8_t ntc_get_algorithm(ntc_handle_t *handle, ntc_algorithm_t *algorithm)
get algorithm
uint8_t ntc_set_algorithm_beta_formula_beta_value(ntc_handle_t *handle, float beta)
set algorithm beta formula beta value
uint8_t ntc_get_algorithm_beta_formula_r25_ohm(ntc_handle_t *handle, float *r25_ohm)
get algorithm beta formula r25 ohm
struct ntc_handle_s ntc_handle_t
ntc handle structure definition
uint8_t ntc_get_algorithm_steinhart_hart(ntc_handle_t *handle, double *a, double *b, double *c)
get algorithm steinhart hart
uint8_t ntc_deinit(ntc_handle_t *handle)
close the chip
struct ntc_table_s ntc_table_t
ntc table structure definition
uint8_t ntc_calculate_temperature(ntc_handle_t *handle, float ohm, float *degrees_celsius)
calculate temperature
uint8_t ntc_set_circuit_fixed_resistor(ntc_handle_t *handle, float r_fixed_ohm)
set circuit fixed resistor
@ NTC_FILTER_ANTI_SPIKE_AVERAGE
@ NTC_FILTER_FIRST_ORDER_LAG
@ NTC_FILTER_MOVING_AVERAGE
@ NTC_FILTER_WEIGHTED_MOVING_AVERAGE
@ NTC_ALGORITHM_BETA_FORMULA
@ NTC_ALGORITHM_STEINHART_HART
@ NTC_CIRCUIT_VCC_R_NTC_GND
@ NTC_CIRCUIT_VCC_NTC_R_GND
uint8_t ntc_set_filter_first_order_lag(ntc_handle_t *handle, float alpha)
set filter first order lag
uint8_t ntc_set_filter_moving_average_length(ntc_handle_t *handle, uint16_t length)
set filter moving average length
uint8_t ntc_get_filter(ntc_handle_t *handle, ntc_filter_t *filter)
get filter
uint8_t ntc_set_filter_limiting(ntc_handle_t *handle, float degrees_celsius)
set filter limiting
uint8_t ntc_set_filter_median_length(ntc_handle_t *handle, uint16_t length)
set filter median length
uint8_t ntc_reset_filter(ntc_handle_t *handle)
reset the filter
uint8_t ntc_set_filter_anti_spike_average_length(ntc_handle_t *handle, uint16_t length)
set filter anti spike average length
uint8_t ntc_set_filter(ntc_handle_t *handle, ntc_filter_t filter)
set filter
uint8_t ntc_set_filter_kalman(ntc_handle_t *handle, float q_process_noise_covariance, float r_measurement_noise_covariance, float p_estimation_error_covariance, float x_estimated_value)
set filter kalman
uint8_t ntc_set_filter_weighted_moving_average_length(ntc_handle_t *handle, float *weight, uint16_t length)
set filter weighted moving average length
float buf_flt[NTC_FILTER_BUFFER_SIZE]
uint32_t buf[NTC_FILTER_BUFFER_SIZE]
const ntc_table_t * table
void(* delay_ms)(uint32_t ms)
uint8_t(* adc_read)(uint32_t *counter, uint16_t len)
float cache_flt[NTC_FILTER_BUFFER_SIZE]
uint8_t(* adc_init)(uint32_t *vcc_counter)
float param_flt[NTC_FILTER_BUFFER_SIZE]
void(* debug_print)(const char *const fmt,...)
uint8_t(* adc_deinit)(void)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v