11#include < Wire.h>
2- // register pointers for various device functions
2+
33#define DEV_ADDR 0x66
44#define RESOLUTION 0.0625
55
6+ #define retryAttempts 3
7+
8+ // register pointers for various device functions
69enum MCP9600_Register {
710 HOT_JUNC_TEMP = 0x00 ,
811 DELTA_JUNCT_TEMP = 0x01 ,
@@ -14,74 +17,73 @@ enum MCP9600_Register {
1417};
1518
1619uint8_t readSingleRegister (MCP9600_Register reg){
17- Wire.beginTransmission (DEV_ADDR);
18- Wire.write (reg);
19- Wire.endTransmission ();
20- Wire.requestFrom (DEV_ADDR, 1 );
21-
22- if (Wire.available ()){
23- uint8_t data = Wire.read ();
20+ for (byte attempts; attempts <= retryAttempts; attempts++){
21+ Wire.beginTransmission (DEV_ADDR);
22+ Wire.write (reg);
2423 Wire.endTransmission ();
25- return data;
26- }
27-
28- else {
29- return 0 ;
24+ if (Wire.requestFrom (DEV_ADDR, 1 ) != 0 ){
25+ return Wire.read ();
26+ }
3027 }
28+ return ;
3129}
3230
3331uint16_t readDoubleRegister (MCP9600_Register reg){
34- Wire.beginTransmission (DEV_ADDR);
35- Wire.write (reg);
36- Wire.endTransmission ();
37- Wire.requestFrom (DEV_ADDR, 2 );
38-
39- while (!(Wire.available () < 2 )){
40- uint16_t data = Wire.read () << 8 ;
41- data |= Wire.read ();
32+ for (byte attempts; attempts <= retryAttempts; attempts++){
33+ Wire.beginTransmission (DEV_ADDR);
34+ Wire.write (reg);
4235 Wire.endTransmission ();
43- return data;
36+
37+ if (Wire.requestFrom (DEV_ADDR, 2 ) != 0 ){
38+ uint16_t data = Wire.read () << 8 ;
39+ data |= Wire.read ();
40+ return data;
41+ }
4442 }
43+ return ;
4544}
4645
4746uint16_t getDeviceID (){
4847 return readDoubleRegister (DEVICE_ID);
4948}
5049
5150int16_t getDelta (){
52- Wire.beginTransmission (DEV_ADDR);
53- Wire.write (DELTA_JUNCT_TEMP);
54- Wire.endTransmission ();
55- Wire.requestFrom (DEV_ADDR, 2 );
51+ int16_t little = readDoubleRegister (DELTA_JUNCT_TEMP);
52+ int16_t big = lowByte (little);
53+ big |= highByte (little) << 8 ;
54+ return big;
55+ }
5656
57- while (!(Wire.available () < 2 )){
58- int16_t data = Wire.read () << 8 ;
59- data |= Wire.read ();
60- Wire.endTransmission ();
61- return data;
57+ bool isConnected (){
58+ Wire.beginTransmission (DEV_ADDR);
59+ if (Wire.endTransmission () != 0 ){
60+ return false ;
61+ }
62+ else {
63+ return true ;
6264 }
6365}
6466
6567void setup () {
6668 Wire.begin ();
69+ Wire.setClock (10000 );
6770 Serial.begin (115200 );
68- Wire.beginTransmission (DEV_ADDR);
6971
7072 // make sure the device will ack
71- if (Wire.endTransmission () != 0 ){
72- Serial.println (" Device Setup Failed" );
73- while (1 );
73+ if (isConnected ()){
74+ Serial.println (" Device Setup Okay" );
7475 }
7576
7677 else {
77- Serial.print (" Device setup okay" );
78+ Serial.println (" Device Setup Failed" );
79+ while (1 ); // hang forever
7880 }
7981}
8082
8183void loop () {
82- uint16_t hot = readDoubleRegister (HOT_JUNC_TEMP);
83- uint16_t cold = readDoubleRegister (COLD_JUNC_TEMP);
84- uint16_t delta = getDelta ();
84+ int16_t hot = readDoubleRegister (HOT_JUNC_TEMP);
85+ int16_t cold = readDoubleRegister (COLD_JUNC_TEMP);
86+ int16_t delta = getDelta ();
8587 Serial.print (" Device ID: " );
8688 Serial.print (getDeviceID (), BIN);
8789 Serial.print (" Hot Junction: " );
@@ -98,5 +100,4 @@ void loop() {
98100 Serial.print ((float )delta*RESOLUTION);
99101 Serial.print (" °C)" );
100102 Serial.println ();
101- delay (10 );
102103}
0 commit comments