Skip to content

Commit 81d0019

Browse files
Fixed indentation
1 parent 8b5e72e commit 81d0019

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

examples/Example3_SetFilterCoeffecients/Example3_SetFilterCoeffecients.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ void loop(){ //print the thermocouple, ambient and delta temperatures every 200m
6666
Serial.print(tempSensor.tempDelta());
6767
Serial.print(" °C");
6868
Serial.println();
69-
delay(200);
69+
while(1); //delay(200);
7070
}

src/SparkFun_MCP9600.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,67 @@ Distributed as-is; no warranty is given.
2727
#endif
2828

2929
MCP9600::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

3636
bool MCP9600::isConnected(){
37-
_i2cPort->beginTransmission(_deviceAddress);
38-
return (_i2cPort->endTransmission() == 0);
37+
_i2cPort->beginTransmission(_deviceAddress);
38+
return (_i2cPort->endTransmission() == 0);
3939
}
4040

4141
uint16_t MCP9600::deviceID(){
42-
return readDoubleRegister(DEVICE_ID);
42+
return readDoubleRegister(DEVICE_ID);
4343
}
4444

4545
bool MCP9600::checkDeviceID(){
46-
return (highByte(deviceID()) == DEV_ID_UPPER);
46+
return (highByte(deviceID()) == DEV_ID_UPPER);
4747
}
4848

4949
float 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

5454
float 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

5959
float 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

6464
uint8_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

7676
uint8_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

8993
uint8_t MCP9600::readSingleRegister(MCP9600_Register reg){
@@ -114,8 +118,8 @@ uint16_t MCP9600::readDoubleRegister(MCP9600_Register reg){
114118
}
115119

116120
uint8_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

Comments
 (0)