LibDriver NTC
Loading...
Searching...
No Matches
driver_ntc.c
Go to the documentation of this file.
1
36
37#include "driver_ntc.h"
38#include <math.h>
39
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
51
59static float a_ntc_beta_formula(ntc_handle_t *handle, float ohm)
60{
61 float b;
62 float r0;
63 float t0;
64 float temp_k;
65
66 b = handle->beta; /* set b */
67 r0 = handle->r25_ohm; /* set r0 */
68 t0 = 298.15f; /* set t0 */
69 temp_k = 1.0f / ((1.0f / t0) + logf(ohm / r0) / b); /* calculate k */
70
71 return temp_k - 273.15f; /* get the final value */
72}
73
81static float a_ntc_steinhart_hart(ntc_handle_t *handle, float ohm)
82{
83 double a;
84 double b;
85 double c;
86 double log_r;
87 double log_r3;
88 double temp_k;
89
90 a = handle->a; /* set a */
91 b = handle->b; /* set b */
92 c = handle->c; /* set c */
93 log_r = log((double)ohm); /* calculate r */
94 log_r3 = log_r * log_r * log_r; /* calculate r3 */
95 temp_k = 1.0 / (a + b * log_r + c * log_r3); /* calculate k */
96
97 return (float)(temp_k - 273.15); /* get the final value */
98}
99
107static float a_ntc_table(ntc_handle_t *handle, float ohm)
108{
109 int32_t low;
110 int32_t high;
111 int32_t mid;
112 float temp;
113
114 low = 0; /* set start */
115 high = (int32_t)(handle->table_len - 1); /* set end */
116 while (low <= high) /* loop */
117 {
118 mid = (low + high) / 2; /* set middle value */
119 if (ohm > handle->table[mid].ohm) /* check range */
120 {
121 high = mid - 1; /* middle - 1 */
122 }
123 else if (ohm < handle->table[mid].ohm) /* check range */
124 {
125 low = mid + 1; /* middle + 1 */
126 }
127 else
128 {
129 return handle->table[mid].degrees_celsius; /* return the temp */
130 }
131 }
132
133 temp = handle->table[high].degrees_celsius +
134 (handle->table[low].degrees_celsius - handle->table[high].degrees_celsius) *
135 (ohm - handle->table[high].ohm) / (handle->table[low].ohm - handle->table[high].ohm); /* get the final value */
136
137 return temp; /* return the temp */
138}
139
151static uint8_t a_ntc_convert(ntc_handle_t *handle, float ohm, float *degrees_celsius)
152{
153 if (handle->algorithm == (uint8_t)NTC_ALGORITHM_BETA_FORMULA) /* beta formula */
154 {
155 *degrees_celsius = a_ntc_beta_formula(handle, ohm); /* convert */
156
157 return 0; /* success return 0 */
158 }
159 else if (handle->algorithm == (uint8_t)NTC_ALGORITHM_STEINHART_HART) /* steinhart hart */
160 {
161 *degrees_celsius = a_ntc_steinhart_hart(handle, ohm); /* convert */
162
163 return 0; /* success return 0 */
164 }
165 else /* lookup table */
166 {
167 if (handle->table_len > 0) /* check table length */
168 {
169 if (ohm > handle->table[0].ohm) /* check range */
170 {
171 handle->debug_print("ntc: out of table.\n"); /* out of table */
172
173 return 1; /* return error */
174 }
175
176 if (ohm < handle->table[handle->table_len - 1].ohm) /* check range */
177 {
178 handle->debug_print("ntc: out of table.\n"); /* out of table */
179
180 return 1; /* return error */
181 }
182
183 *degrees_celsius = a_ntc_table(handle, ohm); /* convert */
184
185 return 0; /* success return 0 */
186 }
187 else
188 {
189 handle->debug_print("ntc: table length is 0.\n"); /* table length is 0 */
190
191 return 2; /* return error */
192 }
193 }
194}
195
206static uint8_t a_ntc_filter(ntc_handle_t *handle, uint16_t input_len, float *output)
207{
208 if (handle->filter == (uint8_t)NTC_FILTER_NONE) /* no filter */
209 {
210 if (input_len != 1) /* check the length */
211 {
212 handle->debug_print("ntc: no filter length must be 1.\n"); /* no filter length must be 1 */
213
214 return 1; /* return error */
215 }
216 *output = handle->buf_flt[0]; /* set output */
217
218 return 0; /* success return 0 */
219 }
220 else if (handle->filter == (uint8_t)NTC_FILTER_FIRST_ORDER_LAG) /* first order lag filter */
221 {
222 int32_t i;
223 float sum;
224 float sum2;
225
226 if (input_len != 1) /* check the length */
227 {
228 handle->debug_print("ntc: first order lag filter length must be 1.\n"); /* first order lag filter length must be 1 */
229
230 return 1; /* return error */
231 }
232
233 if (handle->filter_pointer < handle->filter_len) /* not full */
234 {
235 handle->cache_flt[handle->filter_pointer] = handle->buf_flt[0]; /* push one */
236 handle->filter_pointer++; /* filter pointer++ */
237 }
238 else
239 {
240 for (i = 0; i < handle->filter_pointer - 1; i++) /* length - 1 */
241 {
242 handle->cache_flt[i] = handle->cache_flt[i + 1]; /* exchange */
243 }
244 handle->cache_flt[handle->filter_pointer - 1] = handle->buf_flt[0]; /* push to the last */
245 }
246
247 sum = 0.0f; /* init 0.0f */
248 sum2 = 0.0f; /* init 0.0f */
249 for (i = 0; i < handle->filter_pointer; i++) /* sum all */
250 {
251 sum += handle->cache_flt[i] * handle->param_flt[i]; /* sum all */
252 sum2 += handle->param_flt[i]; /* sum all */
253 }
254 *output = sum / sum2; /* set the output */
255 handle->cache_flt[handle->filter_pointer - 1] = *output; /* save */
256
257 return 0; /* success return 0 */
258 }
259 else if (handle->filter == (uint8_t)NTC_FILTER_MEDIAN) /* median filter */
260 {
261 int32_t i;
262 int32_t j;
263 float temp;
264
265 if ((input_len % 2) == 0) /* check the length */
266 {
267 handle->debug_print("ntc: median filter length must be odd number.\n"); /* median filter length must be odd number */
268
269 return 1; /* return error */
270 }
271 if (input_len < 3) /* check the length */
272 {
273 handle->debug_print("ntc: median filter length < 3.\n"); /* median filter length < 3 */
274
275 return 1; /* return error */
276 }
277
278 for (j = 0; j < input_len - 1; j++) /* 1 layer loop */
279 {
280 for (i = 0; i < input_len - 1 - j; i++) /* 2 layer loop */
281 {
282 if (handle->buf_flt[i] > handle->buf_flt[i + 1]) /* choose the bigger */
283 {
284 temp = handle->buf_flt[i]; /* exchange */
285 handle->buf_flt[i] = handle->buf_flt[i + 1]; /* exchange */
286 handle->buf_flt[i + 1] = temp; /* exchange */
287 }
288 }
289 }
290
291 *output = handle->buf_flt[(input_len - 1) / 2]; /* set middle value */
292
293 return 0; /* success return 0 */
294 }
295 else if (handle->filter == (uint8_t)NTC_FILTER_ANTI_SPIKE_AVERAGE) /* anti spike average filter */
296 {
297 int32_t i;
298 int32_t j;
299 float temp;
300 float sum;
301
302 if (input_len < 3) /* check the length */
303 {
304 handle->debug_print("ntc: anti spike average filter < 3.\n"); /* anti spike average filter < 3 */
305
306 return 1; /* return error */
307 }
308
309 for (j = 0; j < input_len - 1; j++) /* 1 layer loop */
310 {
311 for (i = 0; i < input_len - 1 - j; i++) /* 2 layer loop */
312 {
313 if (handle->buf_flt[i] > handle->buf_flt[i + 1]) /* choose the bigger */
314 {
315 temp = handle->buf_flt[i]; /* exchange */
316 handle->buf_flt[i] = handle->buf_flt[i + 1]; /* exchange */
317 handle->buf_flt[i + 1] = temp; /* exchange */
318 }
319 }
320 }
321
322 sum = 0.0f; /* init 0.0f */
323 for (i = 1; i < input_len - 1; i++) /* remove the first and the last */
324 {
325 sum += handle->buf_flt[i]; /* sum all */
326 }
327 *output = sum / (float)(input_len - 2); /* set the output */
328
329 return 0; /* success return 0 */
330 }
331 else if (handle->filter == (uint8_t)NTC_FILTER_MOVING_AVERAGE) /* moving average filter */
332 {
333 int32_t i;
334 float sum;
335
336 if (input_len != 1) /* check the length */
337 {
338 handle->debug_print("ntc: moving average filter length must be 1.\n"); /* moving average filter length must be 1 */
339
340 return 1; /* return error */
341 }
342
343 if (handle->filter_pointer < handle->filter_len) /* not full */
344 {
345 handle->cache_flt[handle->filter_pointer] = handle->buf_flt[0]; /* push one */
346 handle->filter_pointer++; /* filter pointer++ */
347 }
348 else
349 {
350 for (i = 0; i < handle->filter_pointer - 1; i++) /* length - 1 */
351 {
352 handle->cache_flt[i] = handle->cache_flt[i + 1]; /* exchange */
353 }
354 handle->cache_flt[handle->filter_pointer - 1] = handle->buf_flt[0]; /* push to the last */
355 }
356
357 sum = 0.0f; /* init 0.0f */
358 for (i = 0; i < handle->filter_pointer; i++) /* sum all */
359 {
360 sum += handle->cache_flt[i]; /* sum all */
361 }
362 *output = sum / (float)(handle->filter_pointer); /* set the output */
363
364 return 0; /* success return 0 */
365 }
366 else if (handle->filter == (uint8_t)NTC_FILTER_WEIGHTED_MOVING_AVERAGE) /* weighted moving average filter */
367 {
368 int32_t i;
369 float sum;
370 float sum2;
371
372 if (input_len != 1) /* check the length */
373 {
374 handle->debug_print("ntc: moving average filter length must be 1.\n"); /* moving average filter length must be 1 */
375
376 return 1; /* return error */
377 }
378
379 if (handle->filter_pointer < handle->filter_len) /* not full */
380 {
381 handle->cache_flt[handle->filter_pointer] = handle->buf_flt[0]; /* push one */
382 handle->filter_pointer++; /* filter pointer++ */
383 }
384 else
385 {
386 for (i = 0; i < handle->filter_pointer - 1; i++) /* length - 1 */
387 {
388 handle->cache_flt[i] = handle->cache_flt[i + 1]; /* exchange */
389 }
390 handle->cache_flt[handle->filter_pointer - 1] = handle->buf_flt[0]; /* push to the last */
391 }
392
393 sum = 0.0f; /* init 0.0f */
394 sum2 = 0.0f; /* init 0.0f */
395 for (i = 0; i < handle->filter_pointer; i++) /* sum all */
396 {
397 sum += handle->cache_flt[i] * handle->param_flt[i]; /* sum all */
398 sum2 += handle->param_flt[i]; /* sum all */
399 }
400 *output = sum / sum2; /* set the output */
401
402 return 0; /* success return 0 */
403 }
404 else if (handle->filter == (uint8_t)NTC_FILTER_LIMITING) /* limiting filter */
405 {
406 int32_t i;
407 float deg1;
408 float deg2;
409
410 if (input_len != 1) /* check the length */
411 {
412 handle->debug_print("ntc: filter param invalid.\n"); /* filter param invalid */
413
414 return 1; /* return error */
415 }
416
417 if (handle->filter_pointer < handle->filter_len) /* not full */
418 {
419 handle->cache_flt[handle->filter_pointer] = handle->buf_flt[0]; /* push one */
420 handle->filter_pointer++; /* filter pointer++ */
421 }
422 else
423 {
424 for (i = 0; i < handle->filter_pointer - 1; i++) /* length - 1 */
425 {
426 handle->cache_flt[i] = handle->cache_flt[i + 1]; /* exchange */
427 }
428 handle->cache_flt[handle->filter_pointer - 1] = handle->buf_flt[0]; /* push to the last */
429 }
430
431 if (a_ntc_convert(handle, handle->cache_flt[0], &deg1) != 0) /* convert degrees */
432 {
433 return 1; /* return error */
434 }
435 if (a_ntc_convert(handle, handle->buf_flt[0], &deg2) != 0) /* convert degrees */
436 {
437 return 1; /* return error */
438 }
439 if (fabsf(deg1 - deg2) > handle->param_flt[0]) /* set limit */
440 {
441 *output = handle->cache_flt[0]; /* set the last one */
442 handle->cache_flt[1] = handle->cache_flt[0]; /* save the data */
443 }
444 else
445 {
446 *output = handle->buf_flt[0]; /* set output */
447 }
448
449 return 0; /* success return 0 */
450 }
451 else /* kalman filter */
452 {
453 if (input_len != 1) /* check the length */
454 {
455 handle->debug_print("ntc: filter param invalid.\n"); /* filter param invalid */
456
457 return 1; /* return error */
458 }
459
460 handle->cache_flt[1] = handle->cache_flt[1] + handle->param_flt[0]; /* predict */
461 handle->cache_flt[2] = handle->cache_flt[1] /
462 (handle->cache_flt[1] + handle->param_flt[1]); /* update */
463 handle->cache_flt[0] = handle->cache_flt[0] +
464 handle->cache_flt[2] *
465 (handle->buf_flt[0] - handle->cache_flt[0]); /* correct */
466 handle->cache_flt[1] = (1.0f - handle->cache_flt[2]) * handle->cache_flt[1]; /* correct */
467
468 *output = handle->cache_flt[0]; /* set output */
469
470 return 0; /* success return 0 */
471 }
472}
473
485static uint8_t a_ntc_adc(ntc_handle_t *handle, uint16_t len, uint8_t *open_short_circuit)
486{
487 uint8_t res;
488 uint16_t i;
489
490 res = handle->adc_read(handle->buf, len); /* adc read */
491 if (res != 0) /* check the result */
492 {
493 handle->debug_print("ntc: adc read failed.\n"); /* adc read failed */
494 *open_short_circuit = 0; /* init 0 */
495
496 return 1; /* return error */
497 }
498
499 if (handle->circuit == (uint8_t)NTC_CIRCUIT_VCC_NTC_R_GND) /* vcc -> ntc -> r_fixed -> gnd */
500 {
501 for (i = 0; i < len; i++) /* convert all */
502 {
503 if (handle->buf[i] == 0) /* check open circuit */
504 {
505 *open_short_circuit = 1; /* set open circuit */
506 handle->debug_print("ntc: open circuit.\n"); /* open circuit */
507
508 return 2; /* return error */
509 }
510 else if (handle->buf[i] >= handle->vcc_counter) /* check short circuit */
511 {
512 *open_short_circuit = 2; /* set short circuit */
513 handle->debug_print("ntc: short circuit.\n"); /* short circuit */
514
515 return 2; /* return error */
516 }
517 else
518 {
519 handle->buf_flt[i] = handle->r_fixed_ohm *
520 (((float)handle->vcc_counter /
521 (float)handle->buf[i]) - 1.0f); /* convert */
522 }
523 }
524 *open_short_circuit = 0; /* init 0 */
525
526 return 0; /* success return 0 */
527 }
528 else if (handle->circuit == (uint8_t)NTC_CIRCUIT_VCC_R_NTC_GND) /* vcc -> r_fixed -> ntc -> gnd */
529 {
530 for (i = 0; i < len; i++) /* convert all */
531 {
532 if (handle->buf[i] == 0) /* check short circuit */
533 {
534 *open_short_circuit = 2; /* set short circuit */
535 handle->debug_print("ntc: short circuit.\n"); /* short circuit */
536
537 return 2; /* return error */
538 }
539 else if (handle->buf[i] >= handle->vcc_counter) /* check open circuit */
540 {
541 *open_short_circuit = 1; /* set open circuit */
542 handle->debug_print("ntc: open circuit.\n"); /* open circuit */
543
544 return 2; /* return error */
545 }
546 else
547 {
548 handle->buf_flt[i] = handle->r_fixed_ohm /
549 (((float)handle->vcc_counter /
550 (float)handle->buf[i])- 1.0f); /* convert */
551 }
552 }
553 *open_short_circuit = 0; /* init 0 */
554
555 return 0; /* success return 0 */
556 }
557 else
558 {
559 handle->debug_print("ntc: invalid circuit type.\n"); /* adc invalid circuit type */
560 *open_short_circuit = 0; /* init 0 */
561
562 return 3; /* return error */
563 }
564}
565
577{
578 if (handle == NULL) /* check handle */
579 {
580 return 2; /* return error */
581 }
582 if (handle->debug_print == NULL) /* check debug_print */
583 {
584 return 3; /* return error */
585 }
586
587 handle->circuit = (uint8_t)(circuit); /* set circuit */
588
589 return 0; /* success return 0 */
590}
591
603{
604 if (handle == NULL) /* check handle */
605 {
606 return 2; /* return error */
607 }
608 if (handle->debug_print == NULL) /* check debug_print */
609 {
610 return 3; /* return error */
611 }
612
613 *circuit = (ntc_circuit_t)handle->circuit; /* set circuit */
614
615 return 0; /* success return 0 */
616}
617
628uint8_t ntc_set_circuit_fixed_resistor(ntc_handle_t *handle, float r_fixed_ohm)
629{
630 if (handle == NULL) /* check handle */
631 {
632 return 2; /* return error */
633 }
634 if (handle->debug_print == NULL) /* check debug_print */
635 {
636 return 3; /* return error */
637 }
638
639 handle->r_fixed_ohm = r_fixed_ohm; /* set resistor */
640
641 return 0; /* success return 0 */
642}
643
654uint8_t ntc_get_circuit_fixed_resistor(ntc_handle_t *handle, float *r_fixed_ohm)
655{
656 if (handle == NULL) /* check handle */
657 {
658 return 2; /* return error */
659 }
660 if (handle->debug_print == NULL) /* check debug_print */
661 {
662 return 3; /* return error */
663 }
664
665 *r_fixed_ohm = handle->r_fixed_ohm; /* set resistor */
666
667 return 0; /* success return 0 */
668}
669
674{
675 if (handle == NULL) /* check handle */
676 {
677 return 2; /* return error */
678 }
679 if (handle->debug_print == NULL) /* check debug_print */
680 {
681 return 3; /* return error */
682 }
683
684 handle->algorithm = (uint8_t)(algorithm); /* set algorithm */
685
686 return 0; /* success return 0 */
687}
688
700{
701 if (handle == NULL) /* check handle */
702 {
703 return 2; /* return error */
704 }
705 if (handle->debug_print == NULL) /* check debug_print */
706 {
707 return 3; /* return error */
708 }
709
710 *algorithm = (ntc_algorithm_t)handle->algorithm; /* set algorithm */
711
712 return 0; /* success return 0 */
713}
714
726{
727 if (handle == NULL) /* check handle */
728 {
729 return 2; /* return error */
730 }
731 if (handle->debug_print == NULL) /* check debug_print */
732 {
733 return 3; /* return error */
734 }
735
736 handle->beta = beta; /* set beta formula beta value */
737
738 return 0; /* success return 0 */
739}
740
752{
753 if (handle == NULL) /* check handle */
754 {
755 return 2; /* return error */
756 }
757 if (handle->debug_print == NULL) /* check debug_print */
758 {
759 return 3; /* return error */
760 }
761
762 *beta = handle->beta; /* set beta formula beta value */
763
764 return 0; /* success return 0 */
765}
766
778{
779 if (handle == NULL) /* check handle */
780 {
781 return 2; /* return error */
782 }
783 if (handle->debug_print == NULL) /* check debug_print */
784 {
785 return 3; /* return error */
786 }
787
788 handle->r25_ohm = r25_ohm; /* set r25 ohm */
789
790 return 0; /* success return 0 */
791}
792
804{
805 if (handle == NULL) /* check handle */
806 {
807 return 2; /* return error */
808 }
809 if (handle->debug_print == NULL) /* check debug_print */
810 {
811 return 3; /* return error */
812 }
813
814 *r25_ohm = handle->r25_ohm; /* set r25 ohm */
815
816 return 0; /* success return 0 */
817}
818
831uint8_t ntc_set_algorithm_steinhart_hart(ntc_handle_t *handle, double a, double b, double c)
832{
833 if (handle == NULL) /* check handle */
834 {
835 return 2; /* return error */
836 }
837 if (handle->debug_print == NULL) /* check debug_print */
838 {
839 return 3; /* return error */
840 }
841
842 handle->a = a; /* set a */
843 handle->b = b; /* set b */
844 handle->c = c; /* set c */
845
846 return 0; /* success return 0 */
847}
848
861uint8_t ntc_get_algorithm_steinhart_hart(ntc_handle_t *handle, double *a, double *b, double *c)
862{
863 if (handle == NULL) /* check handle */
864 {
865 return 2; /* return error */
866 }
867 if (handle->debug_print == NULL) /* check debug_print */
868 {
869 return 3; /* return error */
870 }
871
872 *a = handle->a; /* set a */
873 *b = handle->b; /* set b */
874 *c = handle->c; /* set c */
875
876 return 0; /* success return 0 */
877}
878
893uint8_t ntc_load_algorithm_lookup_table(ntc_handle_t *handle, const ntc_table_t *table, uint16_t table_len)
894{
895 uint16_t i;
896 float degrees_celsius;
897 float ohm;
898
899 if (handle == NULL) /* check handle */
900 {
901 return 2; /* return error */
902 }
903 if (handle->debug_print == NULL) /* check debug_print */
904 {
905 return 3; /* return error */
906 }
907 if (table_len == 0) /* check the table length */
908 {
909 handle->debug_print("ntc: table_len is 0.\n"); /* table_len is 0 */
910
911 return 4; /* return error */
912 }
913
914 ohm = table[0].ohm; /* set ohm */
915 degrees_celsius = table[0].degrees_celsius; /* set degrees celsius */
916 for (i = 1; i < table_len; i++) /* check table */
917 {
918 if (table[i].ohm > ohm) /* check table ohm */
919 {
920 handle->debug_print("ntc: table[%d] ohm is invalid.\n", i); /* table ohm is invalid */
921
922 return 5; /* return error */
923 }
924 if (table[i].degrees_celsius < degrees_celsius) /* check degrees celsius */
925 {
926 handle->debug_print("ntc: table[%d] degrees celsius is invalid.\n", i); /* table degrees celsius is invalid */
927
928 return 6; /* return error */
929 }
930
931 ohm = table[i].ohm; /* set ohm */
932 degrees_celsius = table[i].degrees_celsius; /* set degrees celsius */
933 }
934 handle->table = table; /* set table pointer */
935 handle->table_len = table_len; /* set table length */
936
937 return 0; /* success return 0 */
938}
939
951{
952 if (handle == NULL) /* check handle */
953 {
954 return 2; /* return error */
955 }
956 if (handle->debug_print == NULL) /* check debug_print */
957 {
958 return 3; /* return error */
959 }
960
961 handle->filter = (uint8_t)(filter); /* set filter */
962
963 return 0; /* success return 0 */
964}
965
976uint8_t ntc_get_filter(ntc_handle_t *handle, ntc_filter_t *filter)
977{
978 if (handle == NULL) /* check handle */
979 {
980 return 2; /* return error */
981 }
982 if (handle->debug_print == NULL) /* check debug_print */
983 {
984 return 3; /* return error */
985 }
986
987 *filter = (ntc_filter_t)handle->filter; /* set filter */
988
989 return 0; /* success return 0 */
990}
991
995uint8_t ntc_set_filter_first_order_lag(ntc_handle_t *handle, float alpha)
996{
997 if (handle == NULL) /* check handle */
998 {
999 return 2; /* return error */
1000 }
1001 if (handle->debug_print == NULL) /* check debug_print */
1002 {
1003 return 3; /* return error */
1004 }
1005 if (NTC_FILTER_BUFFER_SIZE < 2) /* check buffer length */
1006 {
1007 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < 2.\n"); /* NTC_FILTER_BUFFER_SIZE < 2 */
1008
1009 return 4; /* return error */
1010 }
1011 if (alpha < 0.0f) /* check alpha */
1012 {
1013 handle->debug_print("ntc: alpha < 0.0f.\n"); /* alpha < 0.0f */
1014
1015 return 5; /* return error */
1016 }
1017 if (alpha > 1.0f) /* check alpha */
1018 {
1019 handle->debug_print("ntc: alpha > 1.0f.\n"); /* alpha > 1.0f */
1020
1021 return 6; /* return error */
1022 }
1023
1024 handle->filter_len = 2; /* set filter length 2 */
1025 handle->filter_pointer = 0; /* init 0 */
1026 handle->param_flt[0] = 1.0f - alpha; /* set param 0 */
1027 handle->param_flt[1] = alpha; /* set param 1 */
1028
1029 return 0; /* success return 0 */
1030}
1031
1035uint8_t ntc_set_filter_median_length(ntc_handle_t *handle, uint16_t length)
1036{
1037 if (handle == NULL) /* check handle */
1038 {
1039 return 2; /* return error */
1040 }
1041 if (handle->debug_print == NULL) /* check debug_print */
1042 {
1043 return 3; /* return error */
1044 }
1045 if (NTC_FILTER_BUFFER_SIZE < length) /* check buffer length */
1046 {
1047 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length); /* NTC_FILTER_BUFFER_SIZE < length */
1048
1049 return 4; /* return error */
1050 }
1051 if ((length % 2) == 0) /* check the length */
1052 {
1053 handle->debug_print("ntc: median filter length must be odd number.\n"); /* median filter length must be odd number */
1054
1055 return 5; /* return error */
1056 }
1057 if (length < 3) /* check the length */
1058 {
1059 handle->debug_print("ntc: length < 3.\n"); /* length < 3 */
1060
1061 return 6; /* return error */
1062 }
1063
1064 handle->filter_len = length; /* set filter length */
1065 handle->filter_pointer = 0; /* init 0 */
1066
1067 return 0; /* success return 0 */
1068}
1069
1074{
1075 if (handle == NULL) /* check handle */
1076 {
1077 return 2; /* return error */
1078 }
1079 if (handle->debug_print == NULL) /* check debug_print */
1080 {
1081 return 3; /* return error */
1082 }
1083 if (NTC_FILTER_BUFFER_SIZE < length) /* check buffer length */
1084 {
1085 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length); /* NTC_FILTER_BUFFER_SIZE < length */
1086
1087 return 4; /* return error */
1088 }
1089 if (length < 3) /* check the length */
1090 {
1091 handle->debug_print("ntc: length < 3.\n"); /* length < 3 */
1092
1093 return 5; /* return error */
1094 }
1095
1096 handle->filter_len = length; /* set filter length */
1097 handle->filter_pointer = 0; /* init 0 */
1098
1099 return 0; /* success return 0 */
1100}
1101
1106{
1107 if (handle == NULL) /* check handle */
1108 {
1109 return 2; /* return error */
1110 }
1111 if (handle->debug_print == NULL) /* check debug_print */
1112 {
1113 return 3; /* return error */
1114 }
1115 if (NTC_FILTER_BUFFER_SIZE < length) /* check buffer length */
1116 {
1117 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length); /* NTC_FILTER_BUFFER_SIZE < length */
1118
1119 return 4; /* return error */
1120 }
1121 if (length == 0) /* check the length */
1122 {
1123 handle->debug_print("ntc: length is 0.\n"); /* length is 0 */
1124
1125 return 5; /* return error */
1126 }
1127
1128 handle->filter_len = length; /* set filter length */
1129 handle->filter_pointer = 0; /* init 0 */
1130
1131 return 0; /* success return 0 */
1132}
1133
1137uint8_t ntc_set_filter_weighted_moving_average_length(ntc_handle_t *handle, float *weight, uint16_t length)
1138{
1139 uint16_t i;
1140
1141 if (handle == NULL) /* check handle */
1142 {
1143 return 2; /* return error */
1144 }
1145 if (handle->debug_print == NULL) /* check debug_print */
1146 {
1147 return 3; /* return error */
1148 }
1149 if (NTC_FILTER_BUFFER_SIZE < length) /* check buffer length */
1150 {
1151 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < %d.\n", length); /* NTC_FILTER_BUFFER_SIZE < length */
1152
1153 return 4; /* return error */
1154 }
1155 if (length == 0) /* check the length */
1156 {
1157 handle->debug_print("ntc: length is 0.\n"); /* length is 0 */
1158
1159 return 5; /* return error */
1160 }
1161
1162 for (i = 0; i < length; i++) /* save all */
1163 {
1164 handle->param_flt[i] = weight[i]; /* set weight */
1165 }
1166 handle->filter_len = length; /* set filter length */
1167 handle->filter_pointer = 0; /* init 0 */
1168
1169 return 0; /* success return 0 */
1170}
1171
1175uint8_t ntc_set_filter_limiting(ntc_handle_t *handle, float degrees_celsius)
1176{
1177 if (handle == NULL) /* check handle */
1178 {
1179 return 2; /* return error */
1180 }
1181 if (handle->debug_print == NULL) /* check debug_print */
1182 {
1183 return 3; /* return error */
1184 }
1185 if (NTC_FILTER_BUFFER_SIZE < 2) /* check buffer length */
1186 {
1187 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < 2.\n"); /* NTC_FILTER_BUFFER_SIZE < 2 */
1188
1189 return 4; /* return error */
1190 }
1191
1192 handle->filter_len = 2; /* set filter length 2 */
1193 handle->filter_pointer = 0; /* init 0 */
1194 handle->param_flt[0] = degrees_celsius; /* set degrees celsius */
1195
1196 return 0; /* success return 0 */
1197}
1198
1203 float q_process_noise_covariance,
1204 float r_measurement_noise_covariance,
1205 float p_estimation_error_covariance,
1206 float x_estimated_value)
1207{
1208 uint8_t res;
1209 uint8_t open_short_circuit;
1210
1211 if (handle == NULL) /* check handle */
1212 {
1213 return 2; /* return error */
1214 }
1215 if (handle->debug_print == NULL) /* check debug_print */
1216 {
1217 return 3; /* return error */
1218 }
1219 if (NTC_FILTER_BUFFER_SIZE < 3) /* check buffer length */
1220 {
1221 handle->debug_print("ntc: NTC_FILTER_BUFFER_SIZE < 3.\n"); /* NTC_FILTER_BUFFER_SIZE < 3 */
1222
1223 return 4; /* return error */
1224 }
1225 if (isnan(x_estimated_value) != 0) /* check isnan */
1226 {
1227 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1228 if (res != 0) /* check the result */
1229 {
1230 return 5; /* return error */
1231 }
1232 x_estimated_value = handle->buf_flt[0]; /* set x estimated value */
1233 }
1234
1235 handle->filter_len = 1; /* set filter length 2 */
1236 handle->filter_pointer = 0; /* init 0 */
1237 handle->param_flt[0] = q_process_noise_covariance; /* q, process noise covariance */
1238 handle->param_flt[1] = r_measurement_noise_covariance; /* r, measurement noise covariance */
1239 handle->cache_flt[0] = x_estimated_value; /* x, estimated value */
1240 handle->cache_flt[1] = p_estimation_error_covariance; /* p, measurement noise covariance */
1241 handle->cache_flt[2] = 0.0f; /* k, kalman gain */
1242
1243 return 0; /* success return 0 */
1244}
1245
1256{
1257 if (handle == NULL) /* check handle */
1258 {
1259 return 2; /* return error */
1260 }
1261 if (handle->debug_print == NULL) /* check debug_print */
1262 {
1263 return 3; /* return error */
1264 }
1265
1266 handle->filter_pointer = 0; /* init 0 */
1267
1268 return 0; /* success return 0 */
1269}
1270
1282uint8_t ntc_init(ntc_handle_t *handle)
1283{
1284 uint8_t res;
1285 uint16_t i;
1286
1287 if (handle == NULL) /* check handle */
1288 {
1289 return 2; /* return error */
1290 }
1291 if (handle->debug_print == NULL) /* check debug_print */
1292 {
1293 return 3; /* return error */
1294 }
1295 if (handle->adc_init == NULL) /* check adc_init */
1296 {
1297 handle->debug_print("ntc: adc_init is null.\n"); /* adc_init is null */
1298
1299 return 3; /* return error */
1300 }
1301 if (handle->adc_deinit == NULL) /* check adc_deinit */
1302 {
1303 handle->debug_print("ntc: adc_deinit is null.\n"); /* adc_deinit is null */
1304
1305 return 3; /* return error */
1306 }
1307 if (handle->adc_read == NULL) /* check adc_read */
1308 {
1309 handle->debug_print("ntc: adc_read is null.\n"); /* adc_read is null */
1310
1311 return 3; /* return error */
1312 }
1313 if (handle->delay_ms == NULL) /* check delay_ms */
1314 {
1315 handle->debug_print("ntc: delay_ms is null.\n"); /* delay_ms is null */
1316
1317 return 3; /* return error */
1318 }
1319
1320 res = handle->adc_init(&handle->vcc_counter); /* adc init */
1321 if (res != 0) /* check the result */
1322 {
1323 handle->debug_print("ntc: adc init failed.\n"); /* adc init failed */
1324
1325 return 1; /* return error */
1326 }
1327 if (handle->vcc_counter == 0) /* check vcc counter */
1328 {
1329 handle->debug_print("ntc: vcc counter is invalid.\n"); /* vcc counter is invalid */
1330 (void)handle->adc_deinit(); /* adc deinit */
1331
1332 return 4; /* return error */
1333 }
1334 handle->circuit = (uint8_t)NTC_CIRCUIT_VCC_R_NTC_GND; /* init vcc -> r_fixed -> ntc -> gnd */
1335 handle->r_fixed_ohm = 1.0f; /* init 1.0f */
1336 handle->algorithm = (uint8_t)NTC_ALGORITHM_BETA_FORMULA; /* init beta formula */
1337 handle->beta = 1.0f; /* init 1.0f */
1338 handle->r25_ohm = 1.0f; /* init 1.0f */
1339 handle->a = 1.0; /* init 1.0 */
1340 handle->b = 1.0; /* init 1.0 */
1341 handle->c = 1.0; /* init 1.0 */
1342 handle->table = NULL; /* init null */
1343 handle->table_len = 0; /* init 0 */
1344 handle->filter = (uint8_t)NTC_FILTER_NONE; /* init 0 */
1345 for(i = 0; i < NTC_FILTER_BUFFER_SIZE; i++) /* init all */
1346 {
1347 handle->buf[i] = 0; /* init 0 */
1348 handle->buf_flt[i] = 0.0f; /* init 0 */
1349 handle->cache_flt[i] = 1.0f; /* init 1.0f */
1350 handle->param_flt[i] = 1.0f; /* init 1.0f */
1351 }
1352 handle->filter_len = 0; /* init 0 */
1353 handle->filter_pointer = 0; /* init 0 */
1354 handle->inited = 1; /* flag inited */
1355
1356 return 0; /* success return 0 */
1357}
1358
1369uint8_t ntc_deinit(ntc_handle_t *handle)
1370{
1371 uint8_t res;
1372
1373 if (handle == NULL) /* check handle */
1374 {
1375 return 2; /* return error */
1376 }
1377 if (handle->inited != 1) /* check handle initialization */
1378 {
1379 return 3; /* return error */
1380 }
1381
1382 res = handle->adc_deinit(); /* adc deinit */
1383 if (res != 0) /* check the result */
1384 {
1385 handle->debug_print("ntc: adc deinit failed.\n"); /* adc deinit failed */
1386
1387 return 1; /* return error */
1388 }
1389 handle->inited = 0; /* flag close */
1390
1391 return 0; /* success return 0 */
1392}
1393
1410uint8_t ntc_read_temperature(ntc_handle_t *handle, float *ohm, float *degrees_celsius)
1411{
1412 uint8_t res;
1413 uint8_t open_short_circuit;
1414
1415 if (handle == NULL) /* check handle */
1416 {
1417 return 2; /* return error */
1418 }
1419 if (handle->inited != 1) /* check handle initialization */
1420 {
1421 return 3; /* return error */
1422 }
1423
1424 if (handle->filter == (uint8_t)NTC_FILTER_NONE) /* no filter */
1425 {
1426 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1427 if (res != 0) /* check the result */
1428 {
1429 if (open_short_circuit != 0) /* check open short circuit */
1430 {
1431 if (open_short_circuit == 1) /* check type */
1432 {
1433 return 4; /* return error */
1434 }
1435 else
1436 {
1437 return 5; /* return error */
1438 }
1439 }
1440 else
1441 {
1442 return 1; /* return error */
1443 }
1444 }
1445
1446 res = a_ntc_filter(handle, 1, ohm); /* run the filter */
1447 if (res != 0) /* check the result */
1448 {
1449 return 6; /* return error */
1450 }
1451
1452 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1453 if (res != 0) /* check the result */
1454 {
1455 return 7; /* return error */
1456 }
1457
1458 return 0; /* success return 0 */
1459 }
1460 else if (handle->filter == (uint8_t)NTC_FILTER_FIRST_ORDER_LAG) /* first order lag filter */
1461 {
1462 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1463 if (res != 0) /* check the result */
1464 {
1465 if (open_short_circuit != 0) /* check open short circuit */
1466 {
1467 if (open_short_circuit == 1) /* check type */
1468 {
1469 return 4; /* return error */
1470 }
1471 else
1472 {
1473 return 5; /* return error */
1474 }
1475 }
1476 else
1477 {
1478 return 1; /* return error */
1479 }
1480 }
1481
1482 res = a_ntc_filter(handle, 1, ohm); /* run the filter */
1483 if (res != 0) /* check the result */
1484 {
1485 return 6; /* return error */
1486 }
1487
1488 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1489 if (res != 0) /* check the result */
1490 {
1491 return 7; /* return error */
1492 }
1493
1494 return 0; /* success return 0 */
1495 }
1496 else if (handle->filter == (uint8_t)NTC_FILTER_MEDIAN) /* median filter */
1497 {
1498 res = a_ntc_adc(handle, handle->filter_len, &open_short_circuit); /* read adc */
1499 if (res != 0) /* check the result */
1500 {
1501 if (open_short_circuit != 0) /* check open short circuit */
1502 {
1503 if (open_short_circuit == 1) /* check type */
1504 {
1505 return 4; /* return error */
1506 }
1507 else
1508 {
1509 return 5; /* return error */
1510 }
1511 }
1512 else
1513 {
1514 return 1; /* return error */
1515 }
1516 }
1517
1518 res = a_ntc_filter(handle, handle->filter_len, ohm); /* run the filter */
1519 if (res != 0) /* check the result */
1520 {
1521 return 6; /* return error */
1522 }
1523
1524 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1525 if (res != 0) /* check the result */
1526 {
1527 return 7; /* return error */
1528 }
1529
1530 return 0; /* success return 0 */
1531 }
1532 else if (handle->filter == (uint8_t)NTC_FILTER_ANTI_SPIKE_AVERAGE) /* anti spike average filter */
1533 {
1534 res = a_ntc_adc(handle, handle->filter_len, &open_short_circuit); /* read adc */
1535 if (res != 0) /* check the result */
1536 {
1537 if (open_short_circuit != 0) /* check open short circuit */
1538 {
1539 if (open_short_circuit == 1) /* check type */
1540 {
1541 return 4; /* return error */
1542 }
1543 else
1544 {
1545 return 5; /* return error */
1546 }
1547 }
1548 else
1549 {
1550 return 1; /* return error */
1551 }
1552 }
1553
1554 res = a_ntc_filter(handle, handle->filter_len, ohm); /* run the filter */
1555 if (res != 0) /* check the result */
1556 {
1557 return 6; /* return error */
1558 }
1559
1560 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1561 if (res != 0) /* check the result */
1562 {
1563 return 7; /* return error */
1564 }
1565
1566 return 0; /* success return 0 */
1567 }
1568 else if (handle->filter == (uint8_t)NTC_FILTER_MOVING_AVERAGE) /* moving average filter */
1569 {
1570 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1571 if (res != 0) /* check the result */
1572 {
1573 if (open_short_circuit != 0) /* check open short circuit */
1574 {
1575 if (open_short_circuit == 1) /* check type */
1576 {
1577 return 4; /* return error */
1578 }
1579 else
1580 {
1581 return 5; /* return error */
1582 }
1583 }
1584 else
1585 {
1586 return 1; /* return error */
1587 }
1588 }
1589
1590 res = a_ntc_filter(handle, 1, ohm); /* run the filter */
1591 if (res != 0) /* check the result */
1592 {
1593 return 6; /* return error */
1594 }
1595
1596 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1597 if (res != 0) /* check the result */
1598 {
1599 return 7; /* return error */
1600 }
1601
1602 return 0; /* success return 0 */
1603 }
1604 else if (handle->filter == (uint8_t)NTC_FILTER_WEIGHTED_MOVING_AVERAGE) /* weighted moving average filter */
1605 {
1606 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1607 if (res != 0) /* check the result */
1608 {
1609 if (open_short_circuit != 0) /* check open short circuit */
1610 {
1611 if (open_short_circuit == 1) /* check type */
1612 {
1613 return 4; /* return error */
1614 }
1615 else
1616 {
1617 return 5; /* return error */
1618 }
1619 }
1620 else
1621 {
1622 return 1; /* return error */
1623 }
1624 }
1625
1626 res = a_ntc_filter(handle, 1, ohm); /* run the filter */
1627 if (res != 0) /* check the result */
1628 {
1629 return 6; /* return error */
1630 }
1631
1632 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1633 if (res != 0) /* check the result */
1634 {
1635 return 7; /* return error */
1636 }
1637
1638 return 0; /* success return 0 */
1639 }
1640 else if (handle->filter == (uint8_t)NTC_FILTER_LIMITING) /* limiting filter */
1641 {
1642 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1643 if (res != 0) /* check the result */
1644 {
1645 if (open_short_circuit != 0) /* check open short circuit */
1646 {
1647 if (open_short_circuit == 1) /* check type */
1648 {
1649 return 4; /* return error */
1650 }
1651 else
1652 {
1653 return 5; /* return error */
1654 }
1655 }
1656 else
1657 {
1658 return 1; /* return error */
1659 }
1660 }
1661
1662 res = a_ntc_filter(handle, 1, ohm); /* run the filter */
1663 if (res != 0) /* check the result */
1664 {
1665 return 6; /* return error */
1666 }
1667
1668 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1669 if (res != 0) /* check the result */
1670 {
1671 return 7; /* return error */
1672 }
1673
1674 return 0; /* success return 0 */
1675 }
1676 else /* kalman filter */
1677 {
1678 res = a_ntc_adc(handle, 1, &open_short_circuit); /* read adc */
1679 if (res != 0) /* check the result */
1680 {
1681 if (open_short_circuit != 0) /* check open short circuit */
1682 {
1683 if (open_short_circuit == 1) /* check type */
1684 {
1685 return 4; /* return error */
1686 }
1687 else
1688 {
1689 return 5; /* return error */
1690 }
1691 }
1692 else
1693 {
1694 return 1; /* return error */
1695 }
1696 }
1697
1698 res = a_ntc_filter(handle, 1, ohm); /* run the filter */
1699 if (res != 0) /* check the result */
1700 {
1701 return 6; /* return error */
1702 }
1703
1704 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1705 if (res != 0) /* check the result */
1706 {
1707 return 7; /* return error */
1708 }
1709
1710 return 0; /* success return 0 */
1711 }
1712}
1713
1728uint8_t ntc_calculate_temperature(ntc_handle_t *handle, float ohm, float *degrees_celsius)
1729{
1730 uint8_t res;
1731
1732 if (handle == NULL) /* check handle */
1733 {
1734 return 2; /* return error */
1735 }
1736 if (handle->inited != 1) /* check handle initialization */
1737 {
1738 return 3; /* return error */
1739 }
1740
1741 res = a_ntc_convert(handle, ohm, degrees_celsius); /* ntc convert */
1742 if (res != 0) /* check the result */
1743 {
1744 return 1; /* return error */
1745 }
1746
1747 return 0; /* success return 0 */
1748}
1749
1765uint8_t ntc_calculate_temperature_with_filter(ntc_handle_t *handle, float *ohm, uint16_t len, float *degrees_celsius)
1766{
1767 uint8_t res;
1768 uint16_t i;
1769
1770 if (handle == NULL) /* check handle */
1771 {
1772 return 2; /* return error */
1773 }
1774 if (handle->inited != 1) /* check handle initialization */
1775 {
1776 return 3; /* return error */
1777 }
1778
1779 for (i = 0; i < len; i++) /* copy data */
1780 {
1781 handle->buf_flt[i] = ohm[i]; /* copy data */
1782 }
1783
1784 res = a_ntc_filter(handle, len, ohm); /* run the filter */
1785 if (res != 0) /* check the result */
1786 {
1787 return 1; /* return error */
1788 }
1789
1790 res = a_ntc_convert(handle, *ohm, degrees_celsius); /* ntc convert */
1791 if (res != 0) /* check the result */
1792 {
1793 return 4; /* return error */
1794 }
1795
1796 return 0; /* success return 0 */
1797}
1798
1807uint8_t ntc_info(ntc_info_t *info)
1808{
1809 if (info == NULL) /* check handle */
1810 {
1811 return 2; /* return error */
1812 }
1813
1814 memset(info, 0, sizeof(ntc_info_t)); /* initialize ntc info structure */
1815 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1816 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1817 strncpy(info->interface, "ADC", 8); /* copy interface name */
1818 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1819 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1820 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1821 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1822 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1823 info->driver_version = DRIVER_VERSION; /* set driver version */
1824
1825 return 0; /* success return 0 */
1826}
#define MAX_CURRENT
Definition driver_ntc.c:47
#define SUPPLY_VOLTAGE_MAX
Definition driver_ntc.c:46
#define TEMPERATURE_MAX
Definition driver_ntc.c:49
#define MANUFACTURER_NAME
Definition driver_ntc.c:44
#define TEMPERATURE_MIN
Definition driver_ntc.c:48
#define SUPPLY_VOLTAGE_MIN
Definition driver_ntc.c:45
#define CHIP_NAME
chip information definition
Definition driver_ntc.c:43
#define DRIVER_VERSION
Definition driver_ntc.c:50
driver ntc header file
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
Definition driver_ntc.c:831
uint8_t ntc_set_circuit(ntc_handle_t *handle, ntc_circuit_t circuit)
set circuit
Definition driver_ntc.c:576
uint8_t ntc_load_algorithm_lookup_table(ntc_handle_t *handle, const ntc_table_t *table, uint16_t table_len)
load algorithm lookup table
Definition driver_ntc.c:893
uint8_t ntc_set_algorithm(ntc_handle_t *handle, ntc_algorithm_t algorithm)
set algorithm
Definition driver_ntc.c:673
#define NTC_FILTER_BUFFER_SIZE
ntc filter buffer size definition
Definition driver_ntc.h:64
ntc_filter_t
ntc filter enumeration definition
Definition driver_ntc.h:90
uint8_t ntc_get_circuit_fixed_resistor(ntc_handle_t *handle, float *r_fixed_ohm)
get circuit fixed resistor
Definition driver_ntc.c:654
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
Definition driver_ntc.c:777
ntc_algorithm_t
ntc algorithm enumeration definition
Definition driver_ntc.h:80
uint8_t ntc_init(ntc_handle_t *handle)
initialize the chip
ntc_circuit_t
ntc circuit enumeration definition
Definition driver_ntc.h:71
uint8_t ntc_get_circuit(ntc_handle_t *handle, ntc_circuit_t *circuit)
get circuit
Definition driver_ntc.c:602
uint8_t ntc_get_algorithm_beta_formula_beta_value(ntc_handle_t *handle, float *beta)
get algorithm beta formula beta value
Definition driver_ntc.c:751
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
Definition driver_ntc.c:699
uint8_t ntc_set_algorithm_beta_formula_beta_value(ntc_handle_t *handle, float beta)
set algorithm beta formula beta value
Definition driver_ntc.c:725
uint8_t ntc_get_algorithm_beta_formula_r25_ohm(ntc_handle_t *handle, float *r25_ohm)
get algorithm beta formula r25 ohm
Definition driver_ntc.c:803
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
Definition driver_ntc.c:861
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
Definition driver_ntc.c:628
@ NTC_FILTER_MEDIAN
Definition driver_ntc.h:93
@ NTC_FILTER_ANTI_SPIKE_AVERAGE
Definition driver_ntc.h:94
@ NTC_FILTER_FIRST_ORDER_LAG
Definition driver_ntc.h:92
@ NTC_FILTER_NONE
Definition driver_ntc.h:91
@ NTC_FILTER_MOVING_AVERAGE
Definition driver_ntc.h:95
@ NTC_FILTER_WEIGHTED_MOVING_AVERAGE
Definition driver_ntc.h:96
@ NTC_FILTER_LIMITING
Definition driver_ntc.h:97
@ NTC_ALGORITHM_BETA_FORMULA
Definition driver_ntc.h:81
@ NTC_ALGORITHM_STEINHART_HART
Definition driver_ntc.h:82
@ NTC_CIRCUIT_VCC_R_NTC_GND
Definition driver_ntc.h:73
@ NTC_CIRCUIT_VCC_NTC_R_GND
Definition driver_ntc.h:72
uint8_t ntc_set_filter_first_order_lag(ntc_handle_t *handle, float alpha)
set filter first order lag
Definition driver_ntc.c:995
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
Definition driver_ntc.c:976
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
Definition driver_ntc.c:950
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]
Definition driver_ntc.h:134
uint16_t filter_pointer
Definition driver_ntc.h:138
uint32_t buf[NTC_FILTER_BUFFER_SIZE]
Definition driver_ntc.h:133
uint8_t inited
Definition driver_ntc.h:120
const ntc_table_t * table
Definition driver_ntc.h:130
uint16_t table_len
Definition driver_ntc.h:131
float r_fixed_ohm
Definition driver_ntc.h:123
void(* delay_ms)(uint32_t ms)
Definition driver_ntc.h:118
uint8_t(* adc_read)(uint32_t *counter, uint16_t len)
Definition driver_ntc.h:117
float cache_flt[NTC_FILTER_BUFFER_SIZE]
Definition driver_ntc.h:135
uint8_t circuit
Definition driver_ntc.h:121
uint8_t(* adc_init)(uint32_t *vcc_counter)
Definition driver_ntc.h:115
float param_flt[NTC_FILTER_BUFFER_SIZE]
Definition driver_ntc.h:136
uint8_t algorithm
Definition driver_ntc.h:124
uint32_t vcc_counter
Definition driver_ntc.h:122
void(* debug_print)(const char *const fmt,...)
Definition driver_ntc.h:119
uint8_t(* adc_deinit)(void)
Definition driver_ntc.h:116
uint8_t filter
Definition driver_ntc.h:132
uint16_t filter_len
Definition driver_ntc.h:137
float temperature_max
Definition driver_ntc.h:153
float supply_voltage_max_v
Definition driver_ntc.h:150
uint32_t driver_version
Definition driver_ntc.h:154
float temperature_min
Definition driver_ntc.h:152
float max_current_ma
Definition driver_ntc.h:151
char manufacturer_name[32]
Definition driver_ntc.h:147
float supply_voltage_min_v
Definition driver_ntc.h:149
char interface[8]
Definition driver_ntc.h:148
char chip_name[32]
Definition driver_ntc.h:146
float degrees_celsius
Definition driver_ntc.h:106