@@ -27,63 +27,67 @@ Distributed as-is; no warranty is given.
2727#endif
2828
2929MCP9600::MCP9600 (uint8_t address, TwoWire &wirePort){
30- _deviceAddress = address; // grab the address that the sensor is on
31- _i2cPort = &wirePort; // grab the port that the user wants to use
32- _i2cPort->begin ();
33- _i2cPort->setClock (10000 );
30+ _deviceAddress = address; // grab the address that the sensor is on
31+ _i2cPort = &wirePort; // grab the port that the user wants to use
32+ _i2cPort->begin ();
33+ _i2cPort->setClock (10000 );
3434}
3535
3636bool MCP9600::isConnected (){
37- _i2cPort->beginTransmission (_deviceAddress);
38- return (_i2cPort->endTransmission () == 0 );
37+ _i2cPort->beginTransmission (_deviceAddress);
38+ return (_i2cPort->endTransmission () == 0 );
3939}
4040
4141uint16_t MCP9600::deviceID (){
42- return readDoubleRegister (DEVICE_ID);
42+ return readDoubleRegister (DEVICE_ID);
4343}
4444
4545bool MCP9600::checkDeviceID (){
46- return (highByte (deviceID ()) == DEV_ID_UPPER);
46+ return (highByte (deviceID ()) == DEV_ID_UPPER);
4747}
4848
4949float MCP9600::thermocoupleTemp (){
50- int16_t raw = readDoubleRegister (HOT_JUNC_TEMP);
51- return ((float )raw * DEV_RESOLUTION);
50+ int16_t raw = readDoubleRegister (HOT_JUNC_TEMP);
51+ return ((float )raw * DEV_RESOLUTION);
5252}
5353
5454float MCP9600::ambientTemp (){
55- int16_t raw = readDoubleRegister (COLD_JUNC_TEMP);
56- return ((float )raw * DEV_RESOLUTION);
55+ int16_t raw = readDoubleRegister (COLD_JUNC_TEMP);
56+ return ((float )raw * DEV_RESOLUTION);
5757}
5858
5959float MCP9600::tempDelta (){
60- int16_t raw = readDoubleRegister (DELTA_JUNC_TEMP);
61- return ((float )raw * DEV_RESOLUTION);
60+ int16_t raw = readDoubleRegister (DELTA_JUNC_TEMP);
61+ return ((float )raw * DEV_RESOLUTION);
6262}
6363
6464uint8_t MCP9600::setThermocoupleType (thermocoupleType type){
65- uint8_t config = readSingleRegister (THERMO_SENSOR_CONFIG);
66- bitClear (config, 4 ); // clear the necessary bits so that they can be set
67- bitClear (config, 5 );
68- bitClear (config, 6 );
69- config |= (type << 4 ); // set the necessary bits in the config register
70- if (writeSingleRegister (THERMO_SENSOR_CONFIG, config) != 0 ) return 1 ; // if write fails, return 1
71- if (readSingleRegister (THERMO_SENSOR_CONFIG) != config) return 2 ; // if the register didn't take the new value, return 2
72-
73- return 0 ; // otherwise return 0
65+ uint8_t config = readSingleRegister (THERMO_SENSOR_CONFIG);
66+ bitClear (config, 4 ); // clear the necessary bits so that they can be set
67+ bitClear (config, 5 );
68+ bitClear (config, 6 );
69+ config |= (type << 4 ); // set the necessary bits in the config register
70+ if (writeSingleRegister (THERMO_SENSOR_CONFIG, config) != 0 ) return 1 ; // if write fails, return 1
71+ if (readSingleRegister (THERMO_SENSOR_CONFIG) != config) return 2 ; // if the register didn't take the new value, return 2
72+
73+ return 0 ; // otherwise return 0
7474}
7575
7676uint8_t MCP9600::setFilterCoeffecients (uint8_t coeffecient){
77- if (coeffecient > 7 ) return 3 ; // return immediately if the value is too big
78-
79- uint8_t config = readSingleRegister (THERMO_SENSOR_CONFIG);
80- bitClear (config, 0 ); // clear the necessary bits so that they can be set
81- bitClear (config, 1 );
82- bitClear (config, 2 );
83- config |= coeffecient; // set the necessary bits in the config register
84- if (writeSingleRegister (THERMO_SENSOR_CONFIG, config) != 0 ) return 1 ; // if the write fails, return 1
85- if (readSingleRegister (THERMO_SENSOR_CONFIG) != config) return 2 ; // if the register didn't take the new value, return 2
86-
77+ if (coeffecient > 7 ) return 3 ; // return immediately if the value is too big
78+
79+ uint8_t config = readSingleRegister (THERMO_SENSOR_CONFIG);
80+ config = config >> 3 ;
81+ config = config << 3 ;
82+ config |= coeffecient; // set the necessary bits in the config register
83+
84+ writeSingleRegister (THERMO_SENSOR_CONFIG, config);
85+ for (uint8_t attempts = 0 ; attempts <= retryAttempts; attempts++){
86+ uint8_t readBack = readSingleRegister (THERMO_SENSOR_CONFIG);
87+ Serial.println (readBack, BIN);
88+ if (readBack == config) return 0 ;
89+ }
90+ return 1 ;
8791}
8892
8993uint8_t MCP9600::readSingleRegister (MCP9600_Register reg){
@@ -114,8 +118,8 @@ uint16_t MCP9600::readDoubleRegister(MCP9600_Register reg){
114118}
115119
116120uint8_t MCP9600::writeSingleRegister (MCP9600_Register reg, uint8_t data){
117- _i2cPort->beginTransmission (_deviceAddress);
118- _i2cPort->write (reg);
119- _i2cPort->write (data);
120- return _i2cPort->endTransmission ();
121+ _i2cPort->beginTransmission (_deviceAddress);
122+ _i2cPort->write (reg);
123+ _i2cPort->write (data);
124+ return _i2cPort->endTransmission ();
121125}
0 commit comments