LibDriver MCP9600
Loading...
Searching...
No Matches
driver_mcp9600.c
Go to the documentation of this file.
1
36
37#include "driver_mcp9600.h"
38
42#define CHIP_NAME "Microchip MCP9600"
43#define MANUFACTURER_NAME "Microchip"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 2.5f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define MCP9600_REG_THERMOCOUPLE_HOT_JUNCTION 0x00
55#define MCP9600_REG_JUNCTIONS_TEMPERATURE_DELTA 0x01
56#define MCP9600_REG_COLD_JUNCTION_TEMPERATURE 0x02
57#define MCP9600_REG_RAW_ADC_DATA 0x03
58#define MCP9600_REG_STATUS 0x04
59#define MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION 0x05
60#define MCP9600_REG_DEVICE_CONFIGURATION 0x06
61#define MCP9600_REG_ALERT1_CONFIGURATION 0x08
62#define MCP9600_REG_ALERT2_CONFIGURATION 0x09
63#define MCP9600_REG_ALERT3_CONFIGURATION 0x0A
64#define MCP9600_REG_ALERT4_CONFIGURATION 0x0B
65#define MCP9600_REG_ALERT1_HYSTERESIS 0x0C
66#define MCP9600_REG_ALERT2_HYSTERESIS 0x0D
67#define MCP9600_REG_ALERT3_HYSTERESIS 0x0E
68#define MCP9600_REG_ALERT4_HYSTERESIS 0x0F
69#define MCP9600_REG_TEMPERATURE_ALERT1_LIMIT 0x10
70#define MCP9600_REG_TEMPERATURE_ALERT2_LIMIT 0x11
71#define MCP9600_REG_TEMPERATURE_ALERT3_LIMIT 0x12
72#define MCP9600_REG_TEMPERATURE_ALERT4_LIMIT 0x13
73#define MCP9600_REG_DEVICE_ID_REVISON 0x20
74
86static uint8_t a_mcp9600_iic_read(mcp9600_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
87{
88 uint8_t buf[1];
89
90 buf[0] = reg; /* set reg */
91 if (handle->iic_write_cmd(handle->iic_addr, (uint8_t *)buf, 1) != 0) /* write command */
92 {
93 return 1; /* return error */
94 }
95 if (handle->iic_read_cmd(handle->iic_addr, data, len) != 0) /* read data */
96 {
97 return 1; /* return error */
98 }
99
100 return 0; /* success return 0 */
101}
102
114static uint8_t a_mcp9600_iic_write(mcp9600_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
115{
116 uint8_t buf[16];
117 uint16_t i;
118
119 if ((len + 1) > 16) /* check length */
120 {
121 return 1; /* return error */
122 }
123 buf[0] = reg; /* set MSB of reg */
124 for (i = 0; i < len; i++)
125 {
126 buf[1 + i] = data[i]; /* copy write data */
127 }
128 if (handle->iic_write_cmd(handle->iic_addr, (uint8_t *)buf, len + 1) != 0) /* write iic command */
129 {
130 return 1; /* return error */
131 }
132
133 return 0; /* success return 0 */
134}
135
146{
147 if (handle == NULL) /* check handle */
148 {
149 return 2; /* return error */
150 }
151
152 handle->iic_addr = (uint8_t)addr_pin; /* set pin */
153
154 return 0; /* success return 0 */
155}
156
167{
168 if (handle == NULL) /* check handle */
169 {
170 return 2; /* return error */
171 }
172
173 *addr_pin = (mcp9600_address_t)(handle->iic_addr); /* get pin */
174
175 return 0; /* success return 0 */
176}
177
190{
191 uint8_t res;
192 uint8_t buf[2];
193
194 if (handle == NULL) /* check handle */
195 {
196 return 2; /* return error */
197 }
198 if (handle->debug_print == NULL) /* check debug_print */
199 {
200 return 3; /* return error */
201 }
202 if (handle->iic_init == NULL) /* check iic_init */
203 {
204 handle->debug_print("mcp9600: iic_init is null.\n"); /* iic_init is null */
205
206 return 3; /* return error */
207 }
208 if (handle->iic_deinit == NULL) /* check iic_deinit */
209 {
210 handle->debug_print("mcp9600: iic_deinit is null.\n"); /* iic_deinit is null */
211
212 return 3; /* return error */
213 }
214 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
215 {
216 handle->debug_print("mcp9600: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
217
218 return 3; /* return error */
219 }
220 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
221 {
222 handle->debug_print("mcp9600: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
223
224 return 3; /* return error */
225 }
226 if (handle->delay_ms == NULL) /* check delay_ms */
227 {
228 handle->debug_print("mcp9600: delay_ms is null.\n"); /* delay_ms is null */
229
230 return 3; /* return error */
231 }
232
233 if (handle->iic_init() != 0) /* iic init */
234 {
235 handle->debug_print("mcp9600: iic init failed.\n"); /* iic init failed */
236
237 return 1; /* return error */
238 }
239 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
240 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_ID_REVISON,
241 (uint8_t *)buf, 2); /* read device id */
242 if (res != 0) /* check result */
243 {
244 handle->debug_print("mcp9600: read device id failed.\n"); /* read device id failed */
245 (void)handle->iic_deinit(); /* iic deinit */
246
247 return 1; /* return error */
248 }
249 if (buf[0] != 0x40) /* check id */
250 {
251 handle->debug_print("mcp9600: id is invalid.\n"); /* id is invalid */
252 (void)handle->iic_deinit(); /* iic deinit */
253
254 return 4; /* return error */
255 }
256 handle->inited = 1; /* set inited */
257
258 return 0; /* success return 0 */
259}
260
273{
274 uint8_t res;
275 uint8_t reg;
276
277 if (handle == NULL) /* check handle */
278 {
279 return 2; /* return error */
280 }
281 if (handle->inited != 1) /* check handle initialization */
282 {
283 return 3; /* return error */
284 }
285
286 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
287 if (res != 0) /* check result */
288 {
289 handle->debug_print("mcp9600: power down failed.\n"); /* power down failed */
290
291 return 4; /* return error */
292 }
293
294 reg &= ~(3 << 0); /* clear configure */
295 reg |= 0x1 << 0; /* set configure */
296 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
297 if (res != 0) /* check result */
298 {
299 handle->debug_print("mcp9600: power down failed.\n"); /* power down failed */
300
301 return 4; /* return error */
302 }
303 res = handle->iic_deinit(); /* iic deinit */
304 if (res != 0) /* check result */
305 {
306 handle->debug_print("mcp9600: iic deinit failed.\n"); /* iic deinit failed */
307
308 return 1; /* return error */
309 }
310 handle->inited = 0; /* clear flag */
311
312 return 0; /* success return 0 */
313}
314
326{
327 uint8_t res;
328 uint8_t reg;
329
330 if (handle == NULL) /* check handle */
331 {
332 return 2; /* return error */
333 }
334 if (handle->inited != 1) /* check handle initialization */
335 {
336 return 3; /* return error */
337 }
338
339 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
340 if (res != 0) /* check result */
341 {
342 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
343
344 return 1; /* return error */
345 }
346
347 reg &= ~(3 << 0); /* clear configure */
348 reg |= 0x0 << 0; /* set configure */
349 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
350 if (res != 0) /* check result */
351 {
352 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
353
354 return 1; /* return error */
355 }
356
357 return 0; /* success return 0 */
358}
359
371{
372 uint8_t res;
373 uint8_t reg;
374
375 if (handle == NULL) /* check handle */
376 {
377 return 2; /* return error */
378 }
379 if (handle->inited != 1) /* check handle initialization */
380 {
381 return 3; /* return error */
382 }
383
384 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
385 if (res != 0) /* check result */
386 {
387 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
388
389 return 1; /* return error */
390 }
391
392 reg &= ~(3 << 0); /* clear configure */
393 reg |= 0x02 << 0; /* set configure */
394 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
395 if (res != 0) /* check result */
396 {
397 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
398
399 return 1; /* return error */
400 }
401
402 return 0; /* success return 0 */
403}
404
422uint8_t mcp9600_continuous_read(mcp9600_handle_t *handle, int16_t *hot_raw, float *hot_s,
423 int16_t *delta_raw, float *delta_s, int16_t *cold_raw, float *cold_s)
424{
425 uint8_t res;
426 uint8_t reg;
427 uint8_t buf[2];
428
429 if (handle == NULL) /* check handle */
430 {
431 return 2; /* return error */
432 }
433 if (handle->inited != 1) /* check handle initialization */
434 {
435 return 3; /* return error */
436 }
437
438 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_HOT_JUNCTION, (uint8_t *)buf, 2); /* read config */
439 if (res != 0) /* check result */
440 {
441 handle->debug_print("mcp9600: read hot junction temperature failed.\n"); /* read hot junction temperature failed */
442
443 return 1; /* return error */
444 }
445 *hot_raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
446 *hot_s = (float)(*hot_raw) / 16.0f; /* convert the data */
447
448 res = a_mcp9600_iic_read(handle, MCP9600_REG_JUNCTIONS_TEMPERATURE_DELTA, (uint8_t *)buf, 2); /* read config */
449 if (res != 0) /* check result */
450 {
451 handle->debug_print("mcp9600: read junction thermocouple delta failed.\n"); /* junction thermocouple delta failed */
452
453 return 1; /* return error */
454 }
455 *delta_raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
456 *delta_s = (float)(*delta_raw) / 16.0f; /* convert the data */
457
458 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
459 if (res != 0) /* check result */
460 {
461 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
462
463 return 1; /* return error */
464 }
465 res = a_mcp9600_iic_read(handle, MCP9600_REG_COLD_JUNCTION_TEMPERATURE, (uint8_t *)buf, 2); /* read config */
466 if (res != 0) /* check result */
467 {
468 handle->debug_print("mcp9600: read cold junction temperature failed.\n"); /* read cold junction temperature failed */
469
470 return 1; /* return error */
471 }
472 *cold_raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
473 if (((reg >> 7) & 0x01) != 0) /* check the config */
474 {
475 *cold_s = (float)(*cold_raw) / 16.0f; /* convert the data */
476 }
477 else
478 {
479 *cold_s = (float)(*cold_raw) / 4.0f; /* convert the data */
480 }
481
482 return 0; /* success return 0 */
483}
484
502uint8_t mcp9600_single_read(mcp9600_handle_t *handle, int16_t *hot_raw, float *hot_s,
503 int16_t *delta_raw, float *delta_s, int16_t *cold_raw, float *cold_s)
504{
505 uint8_t res;
506 uint8_t reg;
507 uint16_t timeout;
508 uint8_t buf[2];
509
510 if (handle == NULL) /* check handle */
511 {
512 return 2; /* return error */
513 }
514 if (handle->inited != 1) /* check handle initialization */
515 {
516 return 3; /* return error */
517 }
518
519 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
520 if (res != 0) /* check result */
521 {
522 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
523
524 return 1; /* return error */
525 }
526 reg &= ~(1 << 7); /* clear flag */
527 reg &= ~(1 << 6); /* clear flag */
528 res = a_mcp9600_iic_write(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* write config */
529 if (res != 0) /* check result */
530 {
531 handle->debug_print("mcp9600: write status failed.\n"); /* write status failed */
532
533 return 1; /* return error */
534 }
535
536 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
537 if (res != 0) /* check result */
538 {
539 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
540
541 return 1; /* return error */
542 }
543 reg &= ~(3 << 0); /* clear configure */
544 reg |= 0x02 << 0; /* set configure */
545 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
546 if (res != 0) /* check result */
547 {
548 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
549
550 return 1; /* return error */
551 }
552 timeout = 10000; /* set timeout 10000 ms */
553 while (timeout != 0) /* wait timeout */
554 {
555 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
556 if (res != 0) /* check result */
557 {
558 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
559
560 return 1; /* return error */
561 }
562 if ((reg & 0xC0) == 0xC0) /* check flag */
563 {
564 break; /* break */
565 }
566 handle->delay_ms(1); /* delay 1 ms */
567 timeout--; /* timeout-- */
568 }
569 if (timeout == 0) /* check timeout */
570 {
571 handle->debug_print("mcp9600: read timeout.\n"); /* read timeout failed */
572
573 return 4; /* return error */
574 }
575
576 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_HOT_JUNCTION, (uint8_t *)buf, 2); /* read config */
577 if (res != 0) /* check result */
578 {
579 handle->debug_print("mcp9600: read hot junction temperature failed.\n"); /* read hot junction temperature failed */
580
581 return 1; /* return error */
582 }
583 *hot_raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
584 *hot_s = (float)(*hot_raw) / 16.0f; /* convert the data */
585
586 res = a_mcp9600_iic_read(handle, MCP9600_REG_JUNCTIONS_TEMPERATURE_DELTA, (uint8_t *)buf, 2); /* read config */
587 if (res != 0) /* check result */
588 {
589 handle->debug_print("mcp9600: read junction thermocouple delta failed.\n"); /* junction thermocouple delta failed */
590
591 return 1; /* return error */
592 }
593 *delta_raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
594 *delta_s = (float)(*delta_raw) / 16.0f; /* convert the data */
595
596 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
597 if (res != 0) /* check result */
598 {
599 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
600
601 return 1; /* return error */
602 }
603 res = a_mcp9600_iic_read(handle, MCP9600_REG_COLD_JUNCTION_TEMPERATURE, (uint8_t *)buf, 2); /* read config */
604 if (res != 0) /* check result */
605 {
606 handle->debug_print("mcp9600: read cold junction temperature failed.\n"); /* read cold junction temperature failed */
607
608 return 1; /* return error */
609 }
610 *cold_raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
611 if (((reg >> 7) & 0x01) != 0) /* check the config */
612 {
613 *cold_s = (float)(*cold_raw) / 16.0f; /* convert the data */
614 }
615 else
616 {
617 *cold_s = (float)(*cold_raw) / 4.0f; /* convert the data */
618 }
619
620 return 0; /* success return 0 */
621}
622
635{
636 uint8_t res;
637 uint8_t reg;
638
639 if (handle == NULL) /* check handle */
640 {
641 return 2; /* return error */
642 }
643 if (handle->inited != 1) /* check handle initialization */
644 {
645 return 3; /* return error */
646 }
647
648 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
649 if (res != 0) /* check result */
650 {
651 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
652
653 return 1; /* return error */
654 }
655
656 *status = (mcp9600_bool_t)((reg >> 7) & 0x01); /* get flag */
657
658 return 0; /* success return 0 */
659}
660
672{
673 uint8_t res;
674 uint8_t reg;
675
676 if (handle == NULL) /* check handle */
677 {
678 return 2; /* return error */
679 }
680 if (handle->inited != 1) /* check handle initialization */
681 {
682 return 3; /* return error */
683 }
684
685 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
686 if (res != 0) /* check result */
687 {
688 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
689
690 return 1; /* return error */
691 }
692 reg &= ~(1 << 7); /* clear flag */
693 res = a_mcp9600_iic_write(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* write config */
694 if (res != 0) /* check result */
695 {
696 handle->debug_print("mcp9600: write status failed.\n"); /* write status failed */
697
698 return 1; /* return error */
699 }
700
701 return 0; /* success return 0 */
702}
703
716{
717 uint8_t res;
718 uint8_t reg;
719
720 if (handle == NULL) /* check handle */
721 {
722 return 2; /* return error */
723 }
724 if (handle->inited != 1) /* check handle initialization */
725 {
726 return 3; /* return error */
727 }
728
729 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
730 if (res != 0) /* check result */
731 {
732 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
733
734 return 1; /* return error */
735 }
736
737 *status = (mcp9600_bool_t)((reg >> 6) & 0x01); /* get flag */
738
739 return 0; /* success return 0 */
740}
741
753{
754 uint8_t res;
755 uint8_t reg;
756
757 if (handle == NULL) /* check handle */
758 {
759 return 2; /* return error */
760 }
761 if (handle->inited != 1) /* check handle initialization */
762 {
763 return 3; /* return error */
764 }
765
766 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
767 if (res != 0) /* check result */
768 {
769 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
770
771 return 1; /* return error */
772 }
773 reg &= ~(1 << 6); /* clear flag */
774 res = a_mcp9600_iic_write(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* write config */
775 if (res != 0) /* check result */
776 {
777 handle->debug_print("mcp9600: write status failed.\n"); /* write status failed */
778
779 return 1; /* return error */
780 }
781
782 return 0; /* success return 0 */
783}
784
797{
798 uint8_t res;
799 uint8_t reg;
800
801 if (handle == NULL) /* check handle */
802 {
803 return 2; /* return error */
804 }
805 if (handle->inited != 1) /* check handle initialization */
806 {
807 return 3; /* return error */
808 }
809
810 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
811 if (res != 0) /* check result */
812 {
813 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
814
815 return 1; /* return error */
816 }
817
818 *range = (mcp9600_input_range_t)((reg >> 4) & 0x01); /* get flag */
819
820 return 0; /* success return 0 */
821}
822
836{
837 uint8_t res;
838 uint8_t reg;
839
840 if (handle == NULL) /* check handle */
841 {
842 return 2; /* return error */
843 }
844 if (handle->inited != 1) /* check handle initialization */
845 {
846 return 3; /* return error */
847 }
848
849 res = a_mcp9600_iic_read(handle, MCP9600_REG_STATUS, (uint8_t *)&reg, 1); /* read config */
850 if (res != 0) /* check result */
851 {
852 handle->debug_print("mcp9600: read status failed.\n"); /* read status failed */
853
854 return 1; /* return error */
855 }
856
857 *status = (mcp9600_alert_status_t)((reg >> alert) & 0x01); /* get alert status */
858
859 return 0; /* success return 0 */
860}
861
874uint8_t mcp9600_get_hot_junction_temperature(mcp9600_handle_t *handle, int16_t *raw, float *s)
875{
876 uint8_t res;
877 uint8_t buf[2];
878
879 if (handle == NULL) /* check handle */
880 {
881 return 2; /* return error */
882 }
883 if (handle->inited != 1) /* check handle initialization */
884 {
885 return 3; /* return error */
886 }
887
888 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_HOT_JUNCTION, (uint8_t *)buf, 2); /* read config */
889 if (res != 0) /* check result */
890 {
891 handle->debug_print("mcp9600: read hot junction temperature failed.\n"); /* read hot junction temperature failed */
892
893 return 1; /* return error */
894 }
895
896 *raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
897 *s = (float)(*raw) / 16.0f; /* convert the data */
898
899 return 0; /* success return 0 */
900}
901
914uint8_t mcp9600_get_junction_thermocouple_delta(mcp9600_handle_t *handle, int16_t *raw, float *s)
915{
916 uint8_t res;
917 uint8_t buf[2];
918
919 if (handle == NULL) /* check handle */
920 {
921 return 2; /* return error */
922 }
923 if (handle->inited != 1) /* check handle initialization */
924 {
925 return 3; /* return error */
926 }
927
928 res = a_mcp9600_iic_read(handle, MCP9600_REG_JUNCTIONS_TEMPERATURE_DELTA, (uint8_t *)buf, 2); /* read config */
929 if (res != 0) /* check result */
930 {
931 handle->debug_print("mcp9600: read junction thermocouple delta failed.\n"); /* junction thermocouple delta failed */
932
933 return 1; /* return error */
934 }
935
936 *raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
937 *s = (float)(*raw) / 16.0f; /* convert the data */
938
939 return 0; /* success return 0 */
940}
941
954uint8_t mcp9600_get_cold_junction_temperature(mcp9600_handle_t *handle, int16_t *raw, float *s)
955{
956 uint8_t res;
957 uint8_t reg;
958 uint8_t buf[2];
959
960 if (handle == NULL) /* check handle */
961 {
962 return 2; /* return error */
963 }
964 if (handle->inited != 1) /* check handle initialization */
965 {
966 return 3; /* return error */
967 }
968
969 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
970 if (res != 0) /* check result */
971 {
972 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
973
974 return 1; /* return error */
975 }
976
977 res = a_mcp9600_iic_read(handle, MCP9600_REG_COLD_JUNCTION_TEMPERATURE, (uint8_t *)buf, 2); /* read config */
978 if (res != 0) /* check result */
979 {
980 handle->debug_print("mcp9600: read cold junction temperature failed.\n"); /* read cold junction temperature failed */
981
982 return 1; /* return error */
983 }
984
985 *raw = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get raw data */
986 if (((reg >> 7) & 0x01) != 0) /* check the config */
987 {
988 *s = (float)(*raw) / 16.0f; /* convert the data */
989 }
990 else
991 {
992 *s = (float)(*raw) / 4.0f; /* convert the data */
993 }
994
995 return 0; /* success return 0 */
996}
997
1010uint8_t mcp9600_get_raw_adc(mcp9600_handle_t *handle, int32_t *raw, double *uv)
1011{
1012 uint8_t res;
1013 uint8_t reg;
1014 uint8_t shift;
1015 uint16_t mask;
1016 uint8_t buf[3];
1017 double resolution;
1018
1019 if (handle == NULL) /* check handle */
1020 {
1021 return 2; /* return error */
1022 }
1023 if (handle->inited != 1) /* check handle initialization */
1024 {
1025 return 3; /* return error */
1026 }
1027
1028 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1029 if (res != 0) /* check result */
1030 {
1031 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1032
1033 return 1; /* return error */
1034 }
1035
1036 reg = (reg >> 5) & 0x3; /* get type */
1037 if (reg == 0x0) /* get 18 bit */
1038 {
1039 resolution = 2.0; /* set 2.0 */
1040 shift = 0; /* set shift 0 */
1041 mask = 0x0; /* set mask 0x0 */
1042 }
1043 else if (reg == 0x1) /* get 16 bit */
1044 {
1045 resolution = 8.0; /* set 8.0 */
1046 shift = 2; /* set shift 2 */
1047 mask = 0x03; /* set mask 0x03 */
1048 }
1049 else if (reg == 0x2) /* get 14 bit */
1050 {
1051 resolution = 32.0; /* set 32.0 */
1052 shift = 4; /* set shift 4 */
1053 mask = 0x0F; /* set mask 0x0F */
1054 }
1055 else /* get 12 bit */
1056 {
1057 resolution = 128.0; /* set 128.0 */
1058 shift = 6; /* set shift 6 */
1059 mask = 0x3F; /* set mask 0x3F */
1060 }
1061
1062 res = a_mcp9600_iic_read(handle, MCP9600_REG_RAW_ADC_DATA, (uint8_t *)buf, 3); /* read config */
1063 if (res != 0) /* check result */
1064 {
1065 handle->debug_print("mcp9600: read raw adc data failed.\n"); /* read raw adc data failed */
1066
1067 return 1; /* return error */
1068 }
1069
1070 *raw = (int32_t)(((uint32_t)buf[0] << 16) | ((uint32_t)buf[1]) << 8) | buf[2]; /* get raw data */
1071 if ((buf[0] & 0x80) != 0) /* check sign */
1072 {
1073 *raw = (int32_t)((uint32_t)(*raw) | ((uint32_t)(0xFF) << 24)); /* set sign bits */
1074 }
1075 if (((*raw) & 0x80000000U) != 0) /* sign*/
1076 {
1077 *raw = ((*raw) >> shift) | (uint32_t)(mask) << (32 - shift); /* set raw */
1078 }
1079 else
1080 {
1081 *raw = ((*raw) >> shift); /* set raw */
1082 }
1083
1084 *uv = (double)(*raw) * resolution; /* convert the data */
1085
1086 return 0; /* success return 0 */
1087}
1088
1101{
1102 uint8_t res;
1103 uint8_t reg;
1104
1105 if (handle == NULL) /* check handle */
1106 {
1107 return 2; /* return error */
1108 }
1109 if (handle->inited != 1) /* check handle initialization */
1110 {
1111 return 3; /* return error */
1112 }
1113
1114 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1115 if (res != 0) /* check result */
1116 {
1117 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1118
1119 return 1; /* return error */
1120 }
1121
1122 reg &= ~(1 << 7); /* clear configure */
1123 reg |= resolution << 7; /* set configure */
1124 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
1125 if (res != 0) /* check result */
1126 {
1127 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
1128
1129 return 1; /* return error */
1130 }
1131
1132 return 0; /* success return 0 */
1133}
1134
1147{
1148 uint8_t res;
1149 uint8_t reg;
1150
1151 if (handle == NULL) /* check handle */
1152 {
1153 return 2; /* return error */
1154 }
1155 if (handle->inited != 1) /* check handle initialization */
1156 {
1157 return 3; /* return error */
1158 }
1159
1160 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1161 if (res != 0) /* check result */
1162 {
1163 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1164
1165 return 1; /* return error */
1166 }
1167
1168 *resolution = (mcp9600_cold_junction_resolution_t)((reg >> 7) & 0x01); /* set cold junction resolution */
1169
1170 return 0; /* success return 0 */
1171}
1172
1185{
1186 uint8_t res;
1187 uint8_t reg;
1188
1189 if (handle == NULL) /* check handle */
1190 {
1191 return 2; /* return error */
1192 }
1193 if (handle->inited != 1) /* check handle initialization */
1194 {
1195 return 3; /* return error */
1196 }
1197
1198 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1199 if (res != 0) /* check result */
1200 {
1201 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1202
1203 return 1; /* return error */
1204 }
1205
1206 reg &= ~(3 << 5); /* clear configure */
1207 reg |= resolution << 5; /* set configure */
1208 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
1209 if (res != 0) /* check result */
1210 {
1211 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
1212
1213 return 1; /* return error */
1214 }
1215
1216 return 0; /* success return 0 */
1217}
1218
1231{
1232 uint8_t res;
1233 uint8_t reg;
1234
1235 if (handle == NULL) /* check handle */
1236 {
1237 return 2; /* return error */
1238 }
1239 if (handle->inited != 1) /* check handle initialization */
1240 {
1241 return 3; /* return error */
1242 }
1243
1244 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1245 if (res != 0) /* check result */
1246 {
1247 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1248
1249 return 1; /* return error */
1250 }
1251
1252 *resolution = (mcp9600_adc_resolution_t)((reg >> 5) & 0x03); /* get configuration */
1253
1254 return 0; /* success return 0 */
1255}
1256
1269{
1270 uint8_t res;
1271 uint8_t reg;
1272
1273 if (handle == NULL) /* check handle */
1274 {
1275 return 2; /* return error */
1276 }
1277 if (handle->inited != 1) /* check handle initialization */
1278 {
1279 return 3; /* return error */
1280 }
1281
1282 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1283 if (res != 0) /* check result */
1284 {
1285 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1286
1287 return 1; /* return error */
1288 }
1289
1290 reg &= ~(7 << 2); /* clear configure */
1291 reg |= sample << 2; /* set configure */
1292 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
1293 if (res != 0) /* check result */
1294 {
1295 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
1296
1297 return 1; /* return error */
1298 }
1299
1300 return 0; /* success return 0 */
1301}
1302
1315{
1316 uint8_t res;
1317 uint8_t reg;
1318
1319 if (handle == NULL) /* check handle */
1320 {
1321 return 2; /* return error */
1322 }
1323 if (handle->inited != 1) /* check handle initialization */
1324 {
1325 return 3; /* return error */
1326 }
1327
1328 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1329 if (res != 0) /* check result */
1330 {
1331 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1332
1333 return 1; /* return error */
1334 }
1335
1336 *sample = (mcp9600_burst_mode_sample_t)((reg >> 2) & 0x7); /* set sample */
1337
1338 return 0; /* success return 0 */
1339}
1340
1353{
1354 uint8_t res;
1355 uint8_t reg;
1356
1357 if (handle == NULL) /* check handle */
1358 {
1359 return 2; /* return error */
1360 }
1361 if (handle->inited != 1) /* check handle initialization */
1362 {
1363 return 3; /* return error */
1364 }
1365
1366 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1367 if (res != 0) /* check result */
1368 {
1369 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1370
1371 return 1; /* return error */
1372 }
1373
1374 reg &= ~(3 << 0); /* clear configure */
1375 reg |= mode << 0; /* set configure */
1376 res = a_mcp9600_iic_write(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
1377 if (res != 0) /* check result */
1378 {
1379 handle->debug_print("mcp9600: write device configuration failed.\n"); /* write device configuration failed */
1380
1381 return 1; /* return error */
1382 }
1383
1384 return 0; /* success return 0 */
1385}
1386
1399{
1400 uint8_t res;
1401 uint8_t reg;
1402
1403 if (handle == NULL) /* check handle */
1404 {
1405 return 2; /* return error */
1406 }
1407 if (handle->inited != 1) /* check handle initialization */
1408 {
1409 return 3; /* return error */
1410 }
1411
1412 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1413 if (res != 0) /* check result */
1414 {
1415 handle->debug_print("mcp9600: read device configuration failed.\n"); /* read device configuration failed */
1416
1417 return 1; /* return error */
1418 }
1419
1420 *mode = (mcp9600_mode_t)((reg >> 0) & 0x3); /* get mode */
1421
1422 return 0; /* success return 0 */
1423}
1424
1437{
1438 uint8_t res;
1439 uint8_t reg;
1440
1441 if (handle == NULL) /* check handle */
1442 {
1443 return 2; /* return error */
1444 }
1445 if (handle->inited != 1) /* check handle initialization */
1446 {
1447 return 3; /* return error */
1448 }
1449
1450 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1451 if (res != 0) /* check result */
1452 {
1453 handle->debug_print("mcp9600: read thermocouple sensor configuration failed.\n"); /* read thermocouple sensor configuration failed */
1454
1455 return 1; /* return error */
1456 }
1457
1458 reg &= ~(0x7 << 4); /* clear configure */
1459 reg |= type << 4; /* set configure */
1460 res = a_mcp9600_iic_write(handle, MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
1461 if (res != 0) /* check result */
1462 {
1463 handle->debug_print("mcp9600: write thermocouple sensor configuration failed.\n"); /* write thermocouple sensor configuration failed */
1464
1465 return 1; /* return error */
1466 }
1467
1468 return 0; /* success return 0 */
1469}
1470
1483{
1484 uint8_t res;
1485 uint8_t reg;
1486
1487 if (handle == NULL) /* check handle */
1488 {
1489 return 2; /* return error */
1490 }
1491 if (handle->inited != 1) /* check handle initialization */
1492 {
1493 return 3; /* return error */
1494 }
1495
1496 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1497 if (res != 0) /* check result */
1498 {
1499 handle->debug_print("mcp9600: read thermocouple sensor configuration failed.\n"); /* read thermocouple sensor configuration failed */
1500
1501 return 1; /* return error */
1502 }
1503 *type = (mcp9600_thermocouple_type_t)((reg >> 4) & 0x07); /* get type */
1504
1505 return 0; /* success return 0 */
1506}
1507
1520{
1521 uint8_t res;
1522 uint8_t reg;
1523
1524 if (handle == NULL) /* check handle */
1525 {
1526 return 2; /* return error */
1527 }
1528 if (handle->inited != 1) /* check handle initialization */
1529 {
1530 return 3; /* return error */
1531 }
1532
1533 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1534 if (res != 0) /* check result */
1535 {
1536 handle->debug_print("mcp9600: read thermocouple sensor configuration failed.\n"); /* read thermocouple sensor configuration failed */
1537
1538 return 1; /* return error */
1539 }
1540
1541 reg &= ~(0x7 << 0); /* clear configure */
1542 reg |= coefficient << 0; /* set configure */
1543 res = a_mcp9600_iic_write(handle, MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION, (uint8_t *)&reg, 1); /* write config */
1544 if (res != 0) /* check result */
1545 {
1546 handle->debug_print("mcp9600: write thermocouple sensor configuration failed.\n"); /* write thermocouple sensor configuration failed */
1547
1548 return 1; /* return error */
1549 }
1550
1551 return 0; /* success return 0 */
1552}
1553
1566{
1567 uint8_t res;
1568 uint8_t reg;
1569
1570 if (handle == NULL) /* check handle */
1571 {
1572 return 2; /* return error */
1573 }
1574 if (handle->inited != 1) /* check handle initialization */
1575 {
1576 return 3; /* return error */
1577 }
1578
1579 res = a_mcp9600_iic_read(handle, MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION, (uint8_t *)&reg, 1); /* read config */
1580 if (res != 0) /* check result */
1581 {
1582 handle->debug_print("mcp9600: read thermocouple sensor configuration failed.\n"); /* read thermocouple sensor configuration failed */
1583
1584 return 1; /* return error */
1585 }
1586 *coefficient = (mcp9600_filter_coefficient_t)((reg >> 0) & (0x7)); /* get coefficient */
1587
1588 return 0; /* success return 0 */
1589}
1590
1602uint8_t mcp9600_alert_limit_convert_to_register(mcp9600_handle_t *handle, float c, int16_t *reg)
1603{
1604 if (handle == NULL) /* check handle */
1605 {
1606 return 2; /* return error */
1607 }
1608 if (handle->inited != 1) /* check handle initialization */
1609 {
1610 return 3; /* return error */
1611 }
1612
1613 *reg = (int16_t)(c * 16.0f); /* convert */
1614
1615 return 0; /* success return 0 */
1616}
1617
1629uint8_t mcp9600_alert_limit_convert_to_data(mcp9600_handle_t *handle, int16_t reg, float *c)
1630{
1631 if (handle == NULL) /* check handle */
1632 {
1633 return 2; /* return error */
1634 }
1635 if (handle->inited != 1) /* check handle initialization */
1636 {
1637 return 3; /* return error */
1638 }
1639
1640 *c = (float)reg / 16.0f; /* convert */
1641
1642 return 0; /* success return 0 */
1643}
1644
1659{
1660 uint8_t res;
1661 uint8_t reg_addr;
1662 uint8_t buf[2];
1663
1664 if (handle == NULL) /* check handle */
1665 {
1666 return 2; /* return error */
1667 }
1668 if (handle->inited != 1) /* check handle initialization */
1669 {
1670 return 3; /* return error */
1671 }
1672
1673 switch (alert) /* alert type */
1674 {
1675 case MCP9600_ALERT_1 : /* alert 1 */
1676 {
1677 res = 0; /* set ok */
1678 reg_addr = MCP9600_REG_TEMPERATURE_ALERT1_LIMIT; /* set alert 1 */
1679
1680 break; /* break */
1681 }
1682 case MCP9600_ALERT_2 : /* alert 2 */
1683 {
1684 res = 0; /* set ok */
1685 reg_addr = MCP9600_REG_TEMPERATURE_ALERT2_LIMIT; /* set alert 2 */
1686
1687 break; /* break */
1688 }
1689 case MCP9600_ALERT_3 : /* alert 3 */
1690 {
1691 res = 0; /* set ok */
1692 reg_addr = MCP9600_REG_TEMPERATURE_ALERT3_LIMIT; /* set alert 3 */
1693
1694 break; /* break */
1695 }
1696 case MCP9600_ALERT_4 : /* alert 4 */
1697 {
1698 res = 0; /* set ok */
1699 reg_addr = MCP9600_REG_TEMPERATURE_ALERT4_LIMIT; /* set alert 4 */
1700
1701 break; /* break */
1702 }
1703 default :
1704 {
1705 res = 1; /* set failed */
1706
1707 break; /* break */
1708 }
1709 }
1710 if (res != 0) /* check the result */
1711 {
1712 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
1713
1714 return 4; /* return error */
1715 }
1716 buf[0] = (reg >> 8) & 0xFF; /* set MSB */
1717 buf[1] = (reg >> 0) & 0xFF; /* set LSB */
1718 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 2); /* set alert limit */
1719 if (res != 0) /* check result */
1720 {
1721 handle->debug_print("mcp9600: set alert limit failed.\n"); /* set alert limit failed */
1722
1723 return 1; /* return error */
1724 }
1725
1726 return 0; /* success return 0 */
1727}
1728
1742uint8_t mcp9600_get_alert_limit(mcp9600_handle_t *handle, mcp9600_alert_t alert, int16_t *reg)
1743{
1744 uint8_t res;
1745 uint8_t reg_addr;
1746 uint8_t buf[2];
1747
1748 if (handle == NULL) /* check handle */
1749 {
1750 return 2; /* return error */
1751 }
1752 if (handle->inited != 1) /* check handle initialization */
1753 {
1754 return 3; /* return error */
1755 }
1756
1757 switch (alert) /* alert type */
1758 {
1759 case MCP9600_ALERT_1 : /* alert 1 */
1760 {
1761 res = 0; /* set ok */
1762 reg_addr = MCP9600_REG_TEMPERATURE_ALERT1_LIMIT; /* set alert 1 */
1763
1764 break; /* break */
1765 }
1766 case MCP9600_ALERT_2 : /* alert 2 */
1767 {
1768 res = 0; /* set ok */
1769 reg_addr = MCP9600_REG_TEMPERATURE_ALERT2_LIMIT; /* set alert 2 */
1770
1771 break; /* break */
1772 }
1773 case MCP9600_ALERT_3 : /* alert 3 */
1774 {
1775 res = 0; /* set ok */
1776 reg_addr = MCP9600_REG_TEMPERATURE_ALERT3_LIMIT; /* set alert 3 */
1777
1778 break; /* break */
1779 }
1780 case MCP9600_ALERT_4 : /* alert 4 */
1781 {
1782 res = 0; /* set ok */
1783 reg_addr = MCP9600_REG_TEMPERATURE_ALERT4_LIMIT; /* set alert 4 */
1784
1785 break; /* break */
1786 }
1787 default :
1788 {
1789 res = 1; /* set failed */
1790
1791 break; /* break */
1792 }
1793 }
1794 if (res != 0) /* check the result */
1795 {
1796 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
1797
1798 return 4; /* return error */
1799 }
1800 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 2); /* get alert limit */
1801 if (res != 0) /* check result */
1802 {
1803 handle->debug_print("mcp9600: get alert limit failed.\n"); /* get alert limit failed */
1804
1805 return 1; /* return error */
1806 }
1807 *reg = (int16_t)(((uint16_t)(buf[0]) << 8) | buf[1]); /* set register */
1808
1809 return 0; /* success return 0 */
1810}
1811
1824{
1825 if (handle == NULL) /* check handle */
1826 {
1827 return 2; /* return error */
1828 }
1829 if (handle->inited != 1) /* check handle initialization */
1830 {
1831 return 3; /* return error */
1832 }
1833
1834 *reg = (uint8_t)(c); /* convert */
1835
1836 return 0; /* success return 0 */
1837}
1838
1850uint8_t mcp9600_alert_hysteresis_convert_to_data(mcp9600_handle_t *handle, uint8_t reg, float *c)
1851{
1852 if (handle == NULL) /* check handle */
1853 {
1854 return 2; /* return error */
1855 }
1856 if (handle->inited != 1) /* check handle initialization */
1857 {
1858 return 3; /* return error */
1859 }
1860
1861 *c = (float)(reg); /* convert */
1862
1863 return 0; /* success return 0 */
1864}
1865
1880{
1881 uint8_t res;
1882 uint8_t reg_addr;
1883 uint8_t buf[1];
1884
1885 if (handle == NULL) /* check handle */
1886 {
1887 return 2; /* return error */
1888 }
1889 if (handle->inited != 1) /* check handle initialization */
1890 {
1891 return 3; /* return error */
1892 }
1893
1894 switch (alert) /* alert type */
1895 {
1896 case MCP9600_ALERT_1 : /* alert 1 */
1897 {
1898 res = 0; /* set ok */
1899 reg_addr = MCP9600_REG_ALERT1_HYSTERESIS; /* set alert 1 */
1900
1901 break; /* break */
1902 }
1903 case MCP9600_ALERT_2 : /* alert 2 */
1904 {
1905 res = 0; /* set ok */
1906 reg_addr = MCP9600_REG_ALERT2_HYSTERESIS; /* set alert 2 */
1907
1908 break; /* break */
1909 }
1910 case MCP9600_ALERT_3 : /* alert 3 */
1911 {
1912 res = 0; /* set ok */
1913 reg_addr = MCP9600_REG_ALERT3_HYSTERESIS; /* set alert 3 */
1914
1915 break; /* break */
1916 }
1917 case MCP9600_ALERT_4 : /* alert 4 */
1918 {
1919 res = 0; /* set ok */
1920 reg_addr = MCP9600_REG_ALERT4_HYSTERESIS; /* set alert 4 */
1921
1922 break; /* break */
1923 }
1924 default :
1925 {
1926 res = 1; /* set failed */
1927
1928 break; /* break */
1929 }
1930 }
1931 if (res != 0) /* check the result */
1932 {
1933 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
1934
1935 return 4; /* return error */
1936 }
1937 buf[0] = reg; /* set register */
1938 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert hysteresis */
1939 if (res != 0) /* check result */
1940 {
1941 handle->debug_print("mcp9600: set alert hysteresis failed.\n"); /* set alert hysteresis failed */
1942
1943 return 1; /* return error */
1944 }
1945
1946 return 0; /* success return 0 */
1947}
1948
1963{
1964 uint8_t res;
1965 uint8_t reg_addr;
1966 uint8_t buf[1];
1967
1968 if (handle == NULL) /* check handle */
1969 {
1970 return 2; /* return error */
1971 }
1972 if (handle->inited != 1) /* check handle initialization */
1973 {
1974 return 3; /* return error */
1975 }
1976
1977 switch (alert) /* alert type */
1978 {
1979 case MCP9600_ALERT_1 : /* alert 1 */
1980 {
1981 res = 0; /* set ok */
1982 reg_addr = MCP9600_REG_ALERT1_HYSTERESIS; /* set alert 1 */
1983
1984 break; /* break */
1985 }
1986 case MCP9600_ALERT_2 : /* alert 2 */
1987 {
1988 res = 0; /* set ok */
1989 reg_addr = MCP9600_REG_ALERT2_HYSTERESIS; /* set alert 2 */
1990
1991 break; /* break */
1992 }
1993 case MCP9600_ALERT_3 : /* alert 3 */
1994 {
1995 res = 0; /* set ok */
1996 reg_addr = MCP9600_REG_ALERT3_HYSTERESIS; /* set alert 3 */
1997
1998 break; /* break */
1999 }
2000 case MCP9600_ALERT_4 : /* alert 4 */
2001 {
2002 res = 0; /* set ok */
2003 reg_addr = MCP9600_REG_ALERT4_HYSTERESIS; /* set alert 4 */
2004
2005 break; /* break */
2006 }
2007 default :
2008 {
2009 res = 1; /* set failed */
2010
2011 break; /* break */
2012 }
2013 }
2014 if (res != 0) /* check the result */
2015 {
2016 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2017
2018 return 4; /* return error */
2019 }
2020 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert hysteresis */
2021 if (res != 0) /* check result */
2022 {
2023 handle->debug_print("mcp9600: get alert hysteresis failed.\n"); /* get alert hysteresis failed */
2024
2025 return 1; /* return error */
2026 }
2027 *reg = buf[0]; /* set register */
2028
2029 return 0; /* success return 0 */
2030}
2031
2045{
2046 uint8_t res;
2047 uint8_t reg_addr;
2048 uint8_t buf[1];
2049
2050 if (handle == NULL) /* check handle */
2051 {
2052 return 2; /* return error */
2053 }
2054 if (handle->inited != 1) /* check handle initialization */
2055 {
2056 return 3; /* return error */
2057 }
2058
2059 switch (alert) /* alert type */
2060 {
2061 case MCP9600_ALERT_1 : /* alert 1 */
2062 {
2063 res = 0; /* set ok */
2064 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2065
2066 break; /* break */
2067 }
2068 case MCP9600_ALERT_2 : /* alert 2 */
2069 {
2070 res = 0; /* set ok */
2071 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2072
2073 break; /* break */
2074 }
2075 case MCP9600_ALERT_3 : /* alert 3 */
2076 {
2077 res = 0; /* set ok */
2078 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2079
2080 break; /* break */
2081 }
2082 case MCP9600_ALERT_4 : /* alert 4 */
2083 {
2084 res = 0; /* set ok */
2085 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2086
2087 break; /* break */
2088 }
2089 default :
2090 {
2091 res = 1; /* set failed */
2092
2093 break; /* break */
2094 }
2095 }
2096 if (res != 0) /* check the result */
2097 {
2098 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2099
2100 return 4; /* return error */
2101 }
2102 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2103 if (res != 0) /* check result */
2104 {
2105 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2106
2107 return 1; /* return error */
2108 }
2109 buf[0] &= ~(1 << 7); /* clear reg */
2110 buf[0] |= 1 << 7; /* set clear */
2111 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert config */
2112 if (res != 0) /* check result */
2113 {
2114 handle->debug_print("mcp9600: set alert config failed.\n"); /* set alert config failed */
2115
2116 return 1; /* return error */
2117 }
2118
2119 return 0; /* success return 0 */
2120}
2121
2135uint8_t mcp9600_get_interrupt(mcp9600_handle_t *handle, mcp9600_alert_t alert, uint8_t *status)
2136{
2137 uint8_t res;
2138 uint8_t reg_addr;
2139 uint8_t buf[1];
2140
2141 if (handle == NULL) /* check handle */
2142 {
2143 return 2; /* return error */
2144 }
2145 if (handle->inited != 1) /* check handle initialization */
2146 {
2147 return 3; /* return error */
2148 }
2149
2150 switch (alert) /* alert type */
2151 {
2152 case MCP9600_ALERT_1 : /* alert 1 */
2153 {
2154 res = 0; /* set ok */
2155 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2156
2157 break; /* break */
2158 }
2159 case MCP9600_ALERT_2 : /* alert 2 */
2160 {
2161 res = 0; /* set ok */
2162 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2163
2164 break; /* break */
2165 }
2166 case MCP9600_ALERT_3 : /* alert 3 */
2167 {
2168 res = 0; /* set ok */
2169 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2170
2171 break; /* break */
2172 }
2173 case MCP9600_ALERT_4 : /* alert 4 */
2174 {
2175 res = 0; /* set ok */
2176 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2177
2178 break; /* break */
2179 }
2180 default :
2181 {
2182 res = 1; /* set failed */
2183
2184 break; /* break */
2185 }
2186 }
2187 if (res != 0) /* check the result */
2188 {
2189 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2190
2191 return 4; /* return error */
2192 }
2193 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2194 if (res != 0) /* check result */
2195 {
2196 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2197
2198 return 1; /* return error */
2199 }
2200 *status = (buf[0] >> 7) & 0x01; /* set status */
2201
2202 return 0; /* success return 0 */
2203}
2204
2220{
2221 uint8_t res;
2222 uint8_t reg_addr;
2223 uint8_t buf[1];
2224
2225 if (handle == NULL) /* check handle */
2226 {
2227 return 2; /* return error */
2228 }
2229 if (handle->inited != 1) /* check handle initialization */
2230 {
2231 return 3; /* return error */
2232 }
2233
2234 switch (alert) /* alert type */
2235 {
2236 case MCP9600_ALERT_1 : /* alert 1 */
2237 {
2238 res = 0; /* set ok */
2239 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2240
2241 break; /* break */
2242 }
2243 case MCP9600_ALERT_2 : /* alert 2 */
2244 {
2245 res = 0; /* set ok */
2246 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2247
2248 break; /* break */
2249 }
2250 case MCP9600_ALERT_3 : /* alert 3 */
2251 {
2252 res = 0; /* set ok */
2253 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2254
2255 break; /* break */
2256 }
2257 case MCP9600_ALERT_4 : /* alert 4 */
2258 {
2259 res = 0; /* set ok */
2260 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2261
2262 break; /* break */
2263 }
2264 default :
2265 {
2266 res = 1; /* set failed */
2267
2268 break; /* break */
2269 }
2270 }
2271 if (res != 0) /* check the result */
2272 {
2273 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2274
2275 return 4; /* return error */
2276 }
2277 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2278 if (res != 0) /* check result */
2279 {
2280 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2281
2282 return 1; /* return error */
2283 }
2284 buf[0] &= ~(1 << 4); /* clear reg */
2285 buf[0] |= maintain_detect << 4; /* set maintain detect */
2286 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert config */
2287 if (res != 0) /* check result */
2288 {
2289 handle->debug_print("mcp9600: set alert config failed.\n"); /* set alert config failed */
2290
2291 return 1; /* return error */
2292 }
2293
2294 return 0; /* success return 0 */
2295}
2296
2312{
2313 uint8_t res;
2314 uint8_t reg_addr;
2315 uint8_t buf[1];
2316
2317 if (handle == NULL) /* check handle */
2318 {
2319 return 2; /* return error */
2320 }
2321 if (handle->inited != 1) /* check handle initialization */
2322 {
2323 return 3; /* return error */
2324 }
2325
2326 switch (alert) /* alert type */
2327 {
2328 case MCP9600_ALERT_1 : /* alert 1 */
2329 {
2330 res = 0; /* set ok */
2331 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2332
2333 break; /* break */
2334 }
2335 case MCP9600_ALERT_2 : /* alert 2 */
2336 {
2337 res = 0; /* set ok */
2338 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2339
2340 break; /* break */
2341 }
2342 case MCP9600_ALERT_3 : /* alert 3 */
2343 {
2344 res = 0; /* set ok */
2345 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2346
2347 break; /* break */
2348 }
2349 case MCP9600_ALERT_4 : /* alert 4 */
2350 {
2351 res = 0; /* set ok */
2352 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2353
2354 break; /* break */
2355 }
2356 default :
2357 {
2358 res = 1; /* set failed */
2359
2360 break; /* break */
2361 }
2362 }
2363 if (res != 0) /* check the result */
2364 {
2365 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2366
2367 return 4; /* return error */
2368 }
2369 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2370 if (res != 0) /* check result */
2371 {
2372 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2373
2374 return 1; /* return error */
2375 }
2376 *maintain_detect = (mcp9600_temperature_maintain_detect_t)((buf[0] >> 4) & 0x01); /* get maintain detect */
2377
2378 return 0; /* success return 0 */
2379}
2380
2395{
2396 uint8_t res;
2397 uint8_t reg_addr;
2398 uint8_t buf[1];
2399
2400 if (handle == NULL) /* check handle */
2401 {
2402 return 2; /* return error */
2403 }
2404 if (handle->inited != 1) /* check handle initialization */
2405 {
2406 return 3; /* return error */
2407 }
2408
2409 switch (alert) /* alert type */
2410 {
2411 case MCP9600_ALERT_1 : /* alert 1 */
2412 {
2413 res = 0; /* set ok */
2414 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2415
2416 break; /* break */
2417 }
2418 case MCP9600_ALERT_2 : /* alert 2 */
2419 {
2420 res = 0; /* set ok */
2421 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2422
2423 break; /* break */
2424 }
2425 case MCP9600_ALERT_3 : /* alert 3 */
2426 {
2427 res = 0; /* set ok */
2428 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2429
2430 break; /* break */
2431 }
2432 case MCP9600_ALERT_4 : /* alert 4 */
2433 {
2434 res = 0; /* set ok */
2435 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2436
2437 break; /* break */
2438 }
2439 default :
2440 {
2441 res = 1; /* set failed */
2442
2443 break; /* break */
2444 }
2445 }
2446 if (res != 0) /* check the result */
2447 {
2448 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2449
2450 return 4; /* return error */
2451 }
2452 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2453 if (res != 0) /* check result */
2454 {
2455 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2456
2457 return 1; /* return error */
2458 }
2459 buf[0] &= ~(1 << 3); /* clear reg */
2460 buf[0] |= edge << 3; /* set detect edge */
2461 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert config */
2462 if (res != 0) /* check result */
2463 {
2464 handle->debug_print("mcp9600: set alert config failed.\n"); /* set alert config failed */
2465
2466 return 1; /* return error */
2467 }
2468
2469 return 0; /* success return 0 */
2470}
2471
2486{
2487 uint8_t res;
2488 uint8_t reg_addr;
2489 uint8_t buf[1];
2490
2491 if (handle == NULL) /* check handle */
2492 {
2493 return 2; /* return error */
2494 }
2495 if (handle->inited != 1) /* check handle initialization */
2496 {
2497 return 3; /* return error */
2498 }
2499
2500 switch (alert) /* alert type */
2501 {
2502 case MCP9600_ALERT_1 : /* alert 1 */
2503 {
2504 res = 0; /* set ok */
2505 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2506
2507 break; /* break */
2508 }
2509 case MCP9600_ALERT_2 : /* alert 2 */
2510 {
2511 res = 0; /* set ok */
2512 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2513
2514 break; /* break */
2515 }
2516 case MCP9600_ALERT_3 : /* alert 3 */
2517 {
2518 res = 0; /* set ok */
2519 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2520
2521 break; /* break */
2522 }
2523 case MCP9600_ALERT_4 : /* alert 4 */
2524 {
2525 res = 0; /* set ok */
2526 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2527
2528 break; /* break */
2529 }
2530 default :
2531 {
2532 res = 1; /* set failed */
2533
2534 break; /* break */
2535 }
2536 }
2537 if (res != 0) /* check the result */
2538 {
2539 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2540
2541 return 4; /* return error */
2542 }
2543 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2544 if (res != 0) /* check result */
2545 {
2546 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2547
2548 return 1; /* return error */
2549 }
2550 *edge = (mcp9600_detect_edge_t)((buf[0] >> 3) & 0x01); /* get detect edge */
2551
2552 return 0; /* success return 0 */
2553}
2554
2569{
2570 uint8_t res;
2571 uint8_t reg_addr;
2572 uint8_t buf[1];
2573
2574 if (handle == NULL) /* check handle */
2575 {
2576 return 2; /* return error */
2577 }
2578 if (handle->inited != 1) /* check handle initialization */
2579 {
2580 return 3; /* return error */
2581 }
2582
2583 switch (alert) /* alert type */
2584 {
2585 case MCP9600_ALERT_1 : /* alert 1 */
2586 {
2587 res = 0; /* set ok */
2588 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2589
2590 break; /* break */
2591 }
2592 case MCP9600_ALERT_2 : /* alert 2 */
2593 {
2594 res = 0; /* set ok */
2595 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2596
2597 break; /* break */
2598 }
2599 case MCP9600_ALERT_3 : /* alert 3 */
2600 {
2601 res = 0; /* set ok */
2602 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2603
2604 break; /* break */
2605 }
2606 case MCP9600_ALERT_4 : /* alert 4 */
2607 {
2608 res = 0; /* set ok */
2609 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2610
2611 break; /* break */
2612 }
2613 default :
2614 {
2615 res = 1; /* set failed */
2616
2617 break; /* break */
2618 }
2619 }
2620 if (res != 0) /* check the result */
2621 {
2622 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2623
2624 return 4; /* return error */
2625 }
2626 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2627 if (res != 0) /* check result */
2628 {
2629 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2630
2631 return 1; /* return error */
2632 }
2633 buf[0] &= ~(1 << 2); /* clear reg */
2634 buf[0] |= level << 2; /* set active level */
2635 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert config */
2636 if (res != 0) /* check result */
2637 {
2638 handle->debug_print("mcp9600: set alert config failed.\n"); /* set alert config failed */
2639
2640 return 1; /* return error */
2641 }
2642
2643 return 0; /* success return 0 */
2644}
2645
2660{
2661 uint8_t res;
2662 uint8_t reg_addr;
2663 uint8_t buf[1];
2664
2665 if (handle == NULL) /* check handle */
2666 {
2667 return 2; /* return error */
2668 }
2669 if (handle->inited != 1) /* check handle initialization */
2670 {
2671 return 3; /* return error */
2672 }
2673
2674 switch (alert) /* alert type */
2675 {
2676 case MCP9600_ALERT_1 : /* alert 1 */
2677 {
2678 res = 0; /* set ok */
2679 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2680
2681 break; /* break */
2682 }
2683 case MCP9600_ALERT_2 : /* alert 2 */
2684 {
2685 res = 0; /* set ok */
2686 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2687
2688 break; /* break */
2689 }
2690 case MCP9600_ALERT_3 : /* alert 3 */
2691 {
2692 res = 0; /* set ok */
2693 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2694
2695 break; /* break */
2696 }
2697 case MCP9600_ALERT_4 : /* alert 4 */
2698 {
2699 res = 0; /* set ok */
2700 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2701
2702 break; /* break */
2703 }
2704 default :
2705 {
2706 res = 1; /* set failed */
2707
2708 break; /* break */
2709 }
2710 }
2711 if (res != 0) /* check the result */
2712 {
2713 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2714
2715 return 4; /* return error */
2716 }
2717 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2718 if (res != 0) /* check result */
2719 {
2720 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2721
2722 return 1; /* return error */
2723 }
2724 *level = (mcp9600_active_level_t)((buf[0] >> 2) & 0x01); /* get level */
2725
2726 return 0; /* success return 0 */
2727}
2728
2743{
2744 uint8_t res;
2745 uint8_t reg_addr;
2746 uint8_t buf[1];
2747
2748 if (handle == NULL) /* check handle */
2749 {
2750 return 2; /* return error */
2751 }
2752 if (handle->inited != 1) /* check handle initialization */
2753 {
2754 return 3; /* return error */
2755 }
2756
2757 switch (alert) /* alert type */
2758 {
2759 case MCP9600_ALERT_1 : /* alert 1 */
2760 {
2761 res = 0; /* set ok */
2762 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2763
2764 break; /* break */
2765 }
2766 case MCP9600_ALERT_2 : /* alert 2 */
2767 {
2768 res = 0; /* set ok */
2769 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2770
2771 break; /* break */
2772 }
2773 case MCP9600_ALERT_3 : /* alert 3 */
2774 {
2775 res = 0; /* set ok */
2776 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2777
2778 break; /* break */
2779 }
2780 case MCP9600_ALERT_4 : /* alert 4 */
2781 {
2782 res = 0; /* set ok */
2783 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2784
2785 break; /* break */
2786 }
2787 default :
2788 {
2789 res = 1; /* set failed */
2790
2791 break; /* break */
2792 }
2793 }
2794 if (res != 0) /* check the result */
2795 {
2796 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2797
2798 return 4; /* return error */
2799 }
2800 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2801 if (res != 0) /* check result */
2802 {
2803 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2804
2805 return 1; /* return error */
2806 }
2807 buf[0] &= ~(1 << 1); /* clear reg */
2808 buf[0] |= mode << 1; /* set interrupt mode */
2809 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert config */
2810 if (res != 0) /* check result */
2811 {
2812 handle->debug_print("mcp9600: set alert config failed.\n"); /* set alert config failed */
2813
2814 return 1; /* return error */
2815 }
2816
2817 return 0; /* success return 0 */
2818}
2819
2834{
2835 uint8_t res;
2836 uint8_t reg_addr;
2837 uint8_t buf[1];
2838
2839 if (handle == NULL) /* check handle */
2840 {
2841 return 2; /* return error */
2842 }
2843 if (handle->inited != 1) /* check handle initialization */
2844 {
2845 return 3; /* return error */
2846 }
2847
2848 switch (alert) /* alert type */
2849 {
2850 case MCP9600_ALERT_1 : /* alert 1 */
2851 {
2852 res = 0; /* set ok */
2853 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2854
2855 break; /* break */
2856 }
2857 case MCP9600_ALERT_2 : /* alert 2 */
2858 {
2859 res = 0; /* set ok */
2860 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2861
2862 break; /* break */
2863 }
2864 case MCP9600_ALERT_3 : /* alert 3 */
2865 {
2866 res = 0; /* set ok */
2867 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2868
2869 break; /* break */
2870 }
2871 case MCP9600_ALERT_4 : /* alert 4 */
2872 {
2873 res = 0; /* set ok */
2874 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2875
2876 break; /* break */
2877 }
2878 default :
2879 {
2880 res = 1; /* set failed */
2881
2882 break; /* break */
2883 }
2884 }
2885 if (res != 0) /* check the result */
2886 {
2887 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2888
2889 return 4; /* return error */
2890 }
2891 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2892 if (res != 0) /* check result */
2893 {
2894 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2895
2896 return 1; /* return error */
2897 }
2898 *mode = (mcp9600_interrupt_mode_t)((buf[0] >> 1) & 0x01); /* get mode */
2899
2900 return 0; /* success return 0 */
2901}
2902
2917{
2918 uint8_t res;
2919 uint8_t reg_addr;
2920 uint8_t buf[1];
2921
2922 if (handle == NULL) /* check handle */
2923 {
2924 return 2; /* return error */
2925 }
2926 if (handle->inited != 1) /* check handle initialization */
2927 {
2928 return 3; /* return error */
2929 }
2930
2931 switch (alert) /* alert type */
2932 {
2933 case MCP9600_ALERT_1 : /* alert 1 */
2934 {
2935 res = 0; /* set ok */
2936 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
2937
2938 break; /* break */
2939 }
2940 case MCP9600_ALERT_2 : /* alert 2 */
2941 {
2942 res = 0; /* set ok */
2943 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
2944
2945 break; /* break */
2946 }
2947 case MCP9600_ALERT_3 : /* alert 3 */
2948 {
2949 res = 0; /* set ok */
2950 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
2951
2952 break; /* break */
2953 }
2954 case MCP9600_ALERT_4 : /* alert 4 */
2955 {
2956 res = 0; /* set ok */
2957 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
2958
2959 break; /* break */
2960 }
2961 default :
2962 {
2963 res = 1; /* set failed */
2964
2965 break; /* break */
2966 }
2967 }
2968 if (res != 0) /* check the result */
2969 {
2970 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
2971
2972 return 4; /* return error */
2973 }
2974 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
2975 if (res != 0) /* check result */
2976 {
2977 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
2978
2979 return 1; /* return error */
2980 }
2981 buf[0] &= ~(1 << 0); /* clear reg */
2982 buf[0] |= enable << 0; /* set alert output */
2983 res = a_mcp9600_iic_write(handle, reg_addr, (uint8_t *)buf, 1); /* set alert config */
2984 if (res != 0) /* check result */
2985 {
2986 handle->debug_print("mcp9600: set alert config failed.\n"); /* set alert config failed */
2987
2988 return 1; /* return error */
2989 }
2990
2991 return 0; /* success return 0 */
2992}
2993
3008{
3009 uint8_t res;
3010 uint8_t reg_addr;
3011 uint8_t buf[1];
3012
3013 if (handle == NULL) /* check handle */
3014 {
3015 return 2; /* return error */
3016 }
3017 if (handle->inited != 1) /* check handle initialization */
3018 {
3019 return 3; /* return error */
3020 }
3021
3022 switch (alert) /* alert type */
3023 {
3024 case MCP9600_ALERT_1 : /* alert 1 */
3025 {
3026 res = 0; /* set ok */
3027 reg_addr = MCP9600_REG_ALERT1_CONFIGURATION; /* set alert 1 */
3028
3029 break; /* break */
3030 }
3031 case MCP9600_ALERT_2 : /* alert 2 */
3032 {
3033 res = 0; /* set ok */
3034 reg_addr = MCP9600_REG_ALERT2_CONFIGURATION; /* set alert 2 */
3035
3036 break; /* break */
3037 }
3038 case MCP9600_ALERT_3 : /* alert 3 */
3039 {
3040 res = 0; /* set ok */
3041 reg_addr = MCP9600_REG_ALERT3_CONFIGURATION; /* set alert 3 */
3042
3043 break; /* break */
3044 }
3045 case MCP9600_ALERT_4 : /* alert 4 */
3046 {
3047 res = 0; /* set ok */
3048 reg_addr = MCP9600_REG_ALERT4_CONFIGURATION; /* set alert 4 */
3049
3050 break; /* break */
3051 }
3052 default :
3053 {
3054 res = 1; /* set failed */
3055
3056 break; /* break */
3057 }
3058 }
3059 if (res != 0) /* check the result */
3060 {
3061 handle->debug_print("mcp9600: alert is invalid.\n"); /* alert is invalid */
3062
3063 return 4; /* return error */
3064 }
3065 res = a_mcp9600_iic_read(handle, reg_addr, (uint8_t *)buf, 1); /* get alert config */
3066 if (res != 0) /* check result */
3067 {
3068 handle->debug_print("mcp9600: get alert config failed.\n"); /* get alert config failed */
3069
3070 return 1; /* return error */
3071 }
3072 *enable = (mcp9600_bool_t)((buf[0] >> 0) & 0x01); /* get enable */
3073
3074 return 0; /* success return 0 */
3075}
3076
3089uint8_t mcp9600_get_device_id_revision(mcp9600_handle_t *handle, uint8_t *id, uint8_t *revision)
3090{
3091 uint8_t res;
3092 uint8_t buf[2];
3093
3094 if (handle == NULL) /* check handle */
3095 {
3096 return 2; /* return error */
3097 }
3098 if (handle->inited != 1) /* check handle initialization */
3099 {
3100 return 3; /* return error */
3101 }
3102
3103 res = a_mcp9600_iic_read(handle, MCP9600_REG_DEVICE_ID_REVISON, (uint8_t *)buf, 2); /* read device id */
3104 if (res != 0) /* check result */
3105 {
3106 handle->debug_print("mcp9600: read device id failed.\n"); /* read device id failed */
3107
3108 return 1; /* return error */
3109 }
3110 *id = buf[0]; /* get id */
3111 *revision = buf[1]; /* get revision */
3112
3113 return 0; /* success return 0 */
3114}
3115
3129uint8_t mcp9600_set_reg(mcp9600_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
3130{
3131 if (handle == NULL) /* check handle */
3132 {
3133 return 2; /* return error */
3134 }
3135 if (handle->inited != 1) /* check handle initialization */
3136 {
3137 return 3; /* return error */
3138 }
3139
3140 return a_mcp9600_iic_write(handle, reg, buf, len); /* write data */
3141}
3142
3156uint8_t mcp9600_get_reg(mcp9600_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
3157{
3158 if (handle == NULL) /* check handle */
3159 {
3160 return 2; /* return error */
3161 }
3162 if (handle->inited != 1) /* check handle initialization */
3163 {
3164 return 3; /* return error */
3165 }
3166
3167 return a_mcp9600_iic_read(handle, reg, buf, len); /* read data */
3168}
3169
3179{
3180 if (info == NULL) /* check handle */
3181 {
3182 return 2; /* return error */
3183 }
3184
3185 memset(info, 0, sizeof(mcp9600_info_t)); /* initialize mcp9600 info structure */
3186 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
3187 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
3188 strncpy(info->interface, "IIC", 8); /* copy interface name */
3189 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
3190 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
3191 info->max_current_ma = MAX_CURRENT; /* set maximum current */
3192 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
3193 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
3194 info->driver_version = DRIVER_VERSION; /* set driver version */
3195
3196 return 0; /* success return 0 */
3197}
#define MCP9600_REG_ALERT4_HYSTERESIS
#define MCP9600_REG_DEVICE_ID_REVISON
#define MCP9600_REG_TEMPERATURE_ALERT2_LIMIT
#define MAX_CURRENT
#define MCP9600_REG_TEMPERATURE_ALERT3_LIMIT
#define MCP9600_REG_DEVICE_CONFIGURATION
#define SUPPLY_VOLTAGE_MAX
#define MCP9600_REG_THERMOCOUPLE_HOT_JUNCTION
chip register definition
#define TEMPERATURE_MAX
#define MCP9600_REG_ALERT3_HYSTERESIS
#define MCP9600_REG_COLD_JUNCTION_TEMPERATURE
#define MCP9600_REG_ALERT1_HYSTERESIS
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MCP9600_REG_ALERT4_CONFIGURATION
#define MCP9600_REG_ALERT3_CONFIGURATION
#define MCP9600_REG_ALERT2_HYSTERESIS
#define MCP9600_REG_ALERT2_CONFIGURATION
#define MCP9600_REG_THERMOCOUPLE_SENSOR_CONFIGURATION
#define MCP9600_REG_JUNCTIONS_TEMPERATURE_DELTA
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define MCP9600_REG_ALERT1_CONFIGURATION
#define MCP9600_REG_STATUS
#define MCP9600_REG_TEMPERATURE_ALERT4_LIMIT
#define MCP9600_REG_RAW_ADC_DATA
#define MCP9600_REG_TEMPERATURE_ALERT1_LIMIT
driver mcp9600 header file
mcp9600_active_level_t
mcp9600 active level enumeration definition
uint8_t mcp9600_get_cold_junction_resolution(mcp9600_handle_t *handle, mcp9600_cold_junction_resolution_t *resolution)
get the cold junction resolution
mcp9600_mode_t
mcp9600 mode enumeration definition
uint8_t mcp9600_set_alert_hysteresis(mcp9600_handle_t *handle, mcp9600_alert_t alert, uint8_t reg)
set the alert hysteresis
uint8_t mcp9600_get_interrupt(mcp9600_handle_t *handle, mcp9600_alert_t alert, uint8_t *status)
get the interrupt flag
mcp9600_input_range_t
mcp9600 input range enumeration definition
mcp9600_cold_junction_resolution_t
mcp9600 cold junction resolution enumeration definition
mcp9600_address_t
mcp9600 address enumeration definition
struct mcp9600_info_s mcp9600_info_t
mcp9600 information structure definition
mcp9600_thermocouple_type_t
mcp9600 thermocouple type enumeration definition
uint8_t mcp9600_get_alert_hysteresis(mcp9600_handle_t *handle, mcp9600_alert_t alert, uint8_t *reg)
get the alert hysteresis
uint8_t mcp9600_set_active_level(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_active_level_t level)
set the active level
uint8_t mcp9600_set_detect_edge(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_detect_edge_t edge)
set the detect edge
uint8_t mcp9600_clear_status_burst_complete_flag(mcp9600_handle_t *handle)
clear the burst complete status flag
uint8_t mcp9600_get_alert_output(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_bool_t *enable)
get the alert output
uint8_t mcp9600_get_cold_junction_temperature(mcp9600_handle_t *handle, int16_t *raw, float *s)
get the cold junction temperature
uint8_t mcp9600_set_cold_junction_resolution(mcp9600_handle_t *handle, mcp9600_cold_junction_resolution_t resolution)
set the cold junction resolution
mcp9600_filter_coefficient_t
mcp9600 filter coefficient enumeration definition
uint8_t mcp9600_get_active_level(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_active_level_t *level)
get the active level
uint8_t mcp9600_get_junction_thermocouple_delta(mcp9600_handle_t *handle, int16_t *raw, float *s)
get the junction thermocouple delta
uint8_t mcp9600_single_read(mcp9600_handle_t *handle, int16_t *hot_raw, float *hot_s, int16_t *delta_raw, float *delta_s, int16_t *cold_raw, float *cold_s)
read data once
mcp9600_adc_resolution_t
mcp9600 adc resolution enumeration definition
uint8_t mcp9600_get_status_temperature_update_flag(mcp9600_handle_t *handle, mcp9600_bool_t *status)
get the temperature update status flag
uint8_t mcp9600_clear_interrupt(mcp9600_handle_t *handle, mcp9600_alert_t alert)
clear the interrupt flag
uint8_t mcp9600_set_alert_limit(mcp9600_handle_t *handle, mcp9600_alert_t alert, int16_t reg)
set the alert limit
uint8_t mcp9600_get_addr_pin(mcp9600_handle_t *handle, mcp9600_address_t *addr_pin)
get the iic address pin
uint8_t mcp9600_set_thermocouple_type(mcp9600_handle_t *handle, mcp9600_thermocouple_type_t type)
set the thermocouple type
uint8_t mcp9600_stop_continuous_read(mcp9600_handle_t *handle)
stop reading data
uint8_t mcp9600_set_mode(mcp9600_handle_t *handle, mcp9600_mode_t mode)
set the mode
uint8_t mcp9600_start_continuous_read(mcp9600_handle_t *handle)
start reading data
uint8_t mcp9600_get_adc_resolution(mcp9600_handle_t *handle, mcp9600_adc_resolution_t *resolution)
get the adc resolution
struct mcp9600_handle_s mcp9600_handle_t
mcp9600 handle structure definition
uint8_t mcp9600_alert_hysteresis_convert_to_data(mcp9600_handle_t *handle, uint8_t reg, float *c)
convert the register raw data to the alert hysteresis
uint8_t mcp9600_get_raw_adc(mcp9600_handle_t *handle, int32_t *raw, double *uv)
get the raw adc
mcp9600_burst_mode_sample_t
mcp9600 burst mode sample enumeration definition
uint8_t mcp9600_get_temperature_maintain_detect(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_temperature_maintain_detect_t *maintain_detect)
get the temperature maintain detect
uint8_t mcp9600_get_detect_edge(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_detect_edge_t *edge)
get the detect edge
uint8_t mcp9600_alert_limit_convert_to_register(mcp9600_handle_t *handle, float c, int16_t *reg)
convert the alert limit to the register raw data
uint8_t mcp9600_get_mode(mcp9600_handle_t *handle, mcp9600_mode_t *mode)
get the mode
uint8_t mcp9600_alert_limit_convert_to_data(mcp9600_handle_t *handle, int16_t reg, float *c)
convert the register raw data to the alert limit
uint8_t mcp9600_get_thermocouple_type(mcp9600_handle_t *handle, mcp9600_thermocouple_type_t *type)
get the thermocouple type
uint8_t mcp9600_alert_hysteresis_convert_to_register(mcp9600_handle_t *handle, float c, uint8_t *reg)
convert the alert hysteresis to the register raw data
mcp9600_alert_status_t
mcp9600 alert status enumeration definition
uint8_t mcp9600_get_status_burst_complete_flag(mcp9600_handle_t *handle, mcp9600_bool_t *status)
get the burst complete status flag
mcp9600_interrupt_mode_t
mcp9600 interrupt mode enumeration definition
uint8_t mcp9600_get_alert_limit(mcp9600_handle_t *handle, mcp9600_alert_t alert, int16_t *reg)
get the alert limit
mcp9600_alert_t
mcp9600 alert enumeration definition
mcp9600_bool_t
mcp9600 bool enumeration definition
uint8_t mcp9600_get_burst_mode_sample(mcp9600_handle_t *handle, mcp9600_burst_mode_sample_t *sample)
get the burst mode sample
uint8_t mcp9600_set_alert_output(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_bool_t enable)
set the alert output
uint8_t mcp9600_set_addr_pin(mcp9600_handle_t *handle, mcp9600_address_t addr_pin)
set the iic address pin
uint8_t mcp9600_set_filter_coefficient(mcp9600_handle_t *handle, mcp9600_filter_coefficient_t coefficient)
set the filter coefficient
uint8_t mcp9600_set_temperature_maintain_detect(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_temperature_maintain_detect_t maintain_detect)
set the temperature maintain detect
uint8_t mcp9600_set_burst_mode_sample(mcp9600_handle_t *handle, mcp9600_burst_mode_sample_t sample)
set the burst mode sample
uint8_t mcp9600_get_filter_coefficient(mcp9600_handle_t *handle, mcp9600_filter_coefficient_t *coefficient)
get the filter coefficient
mcp9600_temperature_maintain_detect_t
mcp9600 temperature maintain detect enumeration definition
uint8_t mcp9600_get_status_input_range(mcp9600_handle_t *handle, mcp9600_input_range_t *range)
get the input range status
uint8_t mcp9600_info(mcp9600_info_t *info)
get chip information
uint8_t mcp9600_get_device_id_revision(mcp9600_handle_t *handle, uint8_t *id, uint8_t *revision)
get the device id and revision
uint8_t mcp9600_get_hot_junction_temperature(mcp9600_handle_t *handle, int16_t *raw, float *s)
get the hot junction temperature
uint8_t mcp9600_set_interrupt_mode(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_interrupt_mode_t mode)
set the interrupt mode
uint8_t mcp9600_get_interrupt_mode(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_interrupt_mode_t *mode)
get the interrupt mode
uint8_t mcp9600_clear_status_temperature_update_flag(mcp9600_handle_t *handle)
clear the temperature update status flag
uint8_t mcp9600_deinit(mcp9600_handle_t *handle)
close the chip
uint8_t mcp9600_set_adc_resolution(mcp9600_handle_t *handle, mcp9600_adc_resolution_t resolution)
set the adc resolution
mcp9600_detect_edge_t
mcp9600 detect edge enumeration definition
uint8_t mcp9600_continuous_read(mcp9600_handle_t *handle, int16_t *hot_raw, float *hot_s, int16_t *delta_raw, float *delta_s, int16_t *cold_raw, float *cold_s)
read data continuously
uint8_t mcp9600_get_alert_status(mcp9600_handle_t *handle, mcp9600_alert_t alert, mcp9600_alert_status_t *status)
get the alert status
uint8_t mcp9600_init(mcp9600_handle_t *handle)
initialize the chip
@ MCP9600_ALERT_2
@ MCP9600_ALERT_3
@ MCP9600_ALERT_1
@ MCP9600_ALERT_4
uint8_t mcp9600_get_reg(mcp9600_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t mcp9600_set_reg(mcp9600_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint32_t driver_version
char manufacturer_name[32]