Skip to content

Commit ad66b22

Browse files
author
FischerMoseley
committed
Fixed example sketches, verified that burst mode is working
Also kind of fixed Example3, but it's still kind of borked. Will open an issue to look into it
1 parent 8e7c643 commit ad66b22

File tree

8 files changed

+50
-125
lines changed

8 files changed

+50
-125
lines changed

MCP9600_Test/MCP9600_Test.ino

Lines changed: 0 additions & 103 deletions
This file was deleted.

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ MCP9600 tempSensor;
1818

1919
void setup(){
2020
Serial.begin(115200);
21+
Wire.begin(10000);
2122

2223
//check if the sensor is connected
2324
if(tempSensor.isConnected()){

examples/Example2_SetThermocoupleType/Example2_SetThermocoupleType.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ thermocoupleType type = TYPE_S; //the type of thermocouple to change to!
2323

2424
void setup(){
2525
Serial.begin(115200);
26+
Wire.begin(10000);
2627

2728
//check that the sensor is connected
2829
if(tempSensor.isConnected()){

examples/Example3_SetFilterCoeffecients/Example3_SetFilterCoeffecients.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
#include <SparkFun_MCP9600.h>
2121
MCP9600 tempSensor;
22-
uint8_t coeffecient = 5;
22+
uint8_t coeffecient = 3;
2323

2424
void setup(){
2525
Serial.begin(115200);
26+
Wire.begin(10000);
2627

2728
//check if the sensor is connected
2829
if(tempSensor.isConnected()){
@@ -47,13 +48,17 @@ void setup(){
4748
Serial.print(coeffecient);
4849
Serial.println("!");
4950

51+
tempSensor.setFilterCoeffecients(coeffecient);
52+
5053
//tell us if the coeffecient was set sucessfully
5154
if(tempSensor.getFilterCoeffecients() == coeffecient){
5255
Serial.println("Filter Coeffecients set sucessfully!");
5356
}
5457

5558
else{
5659
Serial.println("Setting filter coeffecient failed!");
60+
Serial.println("The value of the coeffecient is: ");
61+
Serial.println(tempSensor.getFilterCoeffecients(), BIN);
5762
}
5863
}
5964

@@ -65,6 +70,8 @@ void loop(){ //print the thermocouple, ambient and delta temperatures every 200m
6570
Serial.print(" °C Temperature Delta: ");
6671
Serial.print(tempSensor.tempDelta());
6772
Serial.print(" °C");
73+
Serial.print(" Current Coeffecient: ");
74+
Serial.print(tempSensor.getFilterCoeffecients(), BIN);
6875
Serial.println();
69-
while(1); //delay(200);
76+
delay(200);
7077
}

examples/Example4_SetResolution/Example4_SetResolution.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Thermocouple_Resolution thermocoupleRes = RES_14_BIT;
4848

4949
void setup(){
5050
Serial.begin(115200);
51+
Wire.begin(10000);
5152
//check if the sensor is connected
5253
if(tempSensor.isConnected()){
5354
Serial.println("Device will acknowledge!");

examples/Example5_BurstMode/Example5_BurstMode.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
This example configures the shutdown (or "operating") mode that the MCP9600 runs in. Shutdown mode disables all
99
power consuming activities on the MCP9600, including measurements, but it will still respond to I2C commands sent
1010
over Qwiic. Burst mode is similar, where the MCP9600 is shutdown until the Arduino asks it to wake up and take a
11-
number of samples, apply any filtering, update any outputs, and then go back to sleep. This example walks through
12-
that process!
11+
number of samples, apply any filtering, update any outputs, and then enter shutdown mode. This example walks
12+
through that process!
1313
1414
Hardware Connections:
1515
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
@@ -20,10 +20,12 @@
2020
#include <SparkFun_MCP9600.h>
2121
MCP9600 tempSensor;
2222
Shutdown_Mode mode = BURST;
23-
Burst_Sample samples = SAMPLES_32;
23+
Burst_Sample samples = SAMPLES_8;
2424

2525
void setup(){
2626
Serial.begin(115200);
27+
Wire.begin(10000);
28+
2729
//check if the sensor is connected
2830
if(tempSensor.isConnected()){
2931
Serial.println("Device will acknowledge!");
@@ -43,8 +45,8 @@ void setup(){
4345
}
4446

4547
//put the MCP9600 into burst mode!
48+
tempSensor.setBurstSamples(samples);
4649
tempSensor.setShutdownMode(BURST);
47-
tempSensor.setBurstSamples(samples);
4850
}
4951

5052
void loop(){
@@ -63,5 +65,4 @@ void loop(){
6365
//clear the register and start a new burst cycle!
6466
tempSensor.startBurst();
6567
}
66-
delay(200); //we want to be able to see our data, so we won't let it print any faster than 200ms
6768
}

src/SparkFun_MCP9600.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool MCP9600::setAmbientResolution(Ambient_Resolution res){
9191
bitWrite(config, 7, res); //set the bit that controls the ambient (cold) junction resolution
9292

9393
bool failed = writeSingleRegister(DEVICE_CONFIG, config); //write new config register to MCP9600
94-
failed &= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was set properly
94+
failed |= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was set properly
9595
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
9696
}
9797

@@ -108,7 +108,7 @@ bool MCP9600::setThermocoupleResolution(Thermocouple_Resolution res){
108108
bitWrite(config, 5, lowResolutionBit); //set 5th bit of config register to 0th bit of the resolution
109109

110110
bool failed = writeSingleRegister(DEVICE_CONFIG, config); //write new config register to MCP9600
111-
failed &= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was written properly
111+
failed |= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was written properly
112112
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
113113
}
114114

@@ -143,17 +143,27 @@ uint8_t MCP9600::setFilterCoeffecients(uint8_t coeffecient){
143143
if(coeffecient > 7) return 3; //return immediately if the value is too big
144144

145145
uint8_t config = readSingleRegister(THERMO_SENSOR_CONFIG);
146-
config = config >> 3;
147-
config = config << 3;
148-
config |= coeffecient; //set the necessary bits in the config register
146+
bitWrite(coeffecient, 3, bitRead(config, 3));
147+
bitWrite(coeffecient, 4, bitRead(config, 3));
148+
bitWrite(coeffecient, 5, bitRead(config, 3));
149+
bitWrite(coeffecient, 6, bitRead(config, 3));
150+
bitWrite(coeffecient, 7, bitRead(config, 3));
149151

150-
writeSingleRegister(THERMO_SENSOR_CONFIG, config);
151-
return;
152+
//config = config >> 3;
153+
//config = config << 3;
154+
//config |= coeffecient; //set the necessary bits in the config register
155+
156+
return writeSingleRegister(THERMO_SENSOR_CONFIG, coeffecient);
152157
}
153158

154159
uint8_t MCP9600::getFilterCoeffecients(){
155160
uint8_t config = readSingleRegister(THERMO_SENSOR_CONFIG);
156-
return ((config << 5) >> 5); //clear the non-filter-coeffecients data in the config register
161+
uint8_t coeff = 0;
162+
bitWrite(coeff, 0, bitRead(config, 0));
163+
bitWrite(coeff, 1, bitRead(config, 1));
164+
bitWrite(coeff, 2, bitRead(config, 2));
165+
166+
return coeff; //clear the non-filter-coeffecients data in the config register
157167
}
158168

159169
bool MCP9600::setBurstSamples(Burst_Sample samples){
@@ -166,7 +176,7 @@ bool MCP9600::setBurstSamples(Burst_Sample samples){
166176
bitWrite(config, 2, lowResolutionBit); //write 0th bit of samples to 2nd of config
167177

168178
bool failed = writeSingleRegister(DEVICE_CONFIG, config); //write new config register to MCP9600
169-
failed &= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was written properly
179+
failed |= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was written properly
170180
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
171181
}
172182

@@ -190,7 +200,11 @@ bool MCP9600::burstAvailable(){
190200
bool MCP9600::startBurst(){
191201
uint8_t status = readSingleRegister(SENSOR_STATUS);
192202
bitWrite(status, 7, 0); //clear the 7th bit of the status register, and send over I2C
193-
return writeSingleRegister(SENSOR_STATUS, status); //return whether the write was successful
203+
204+
bool failed = writeSingleRegister(SENSOR_STATUS, status); //return whether the write was successful
205+
failed |= setShutdownMode(BURST);
206+
207+
return failed;
194208
}
195209

196210
bool MCP9600::setShutdownMode(Shutdown_Mode mode){
@@ -199,13 +213,16 @@ bool MCP9600::setShutdownMode(Shutdown_Mode mode){
199213
config |= mode;
200214

201215
bool failed = writeSingleRegister(DEVICE_CONFIG, config); //write new config register to MCP9600
202-
failed &= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was written properly
216+
failed |= (readSingleRegister(DEVICE_CONFIG) != config); //double check that it was written properly
203217
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
204218
}
205219

206220
Shutdown_Mode MCP9600::getShutdownMode(){
207221
uint8_t config = readSingleRegister(DEVICE_CONFIG);
208-
return ((config << 6) >> 6); //clear all bits except the last two and return
222+
uint8_t mode = 0;
223+
bitWrite(mode, 0, bitRead(config, 0));
224+
bitWrite(mode, 1, bitRead(config, 1));
225+
return mode; //clear all bits except the last two and return
209226
}
210227

211228
/*------------------------- Internal I2C Abstraction ---------------- */

src/SparkFun_MCP9600.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ enum Burst_Sample {
7676
};
7777

7878
enum Shutdown_Mode {
79-
NORMAL = 0b00,
80-
SHUTDOWN = 0b01,
81-
BURST = 0b10,
79+
NORMAL = 0x00,
80+
SHUTDOWN = 0x01,
81+
BURST = 0x02,
8282
};
8383

8484
class MCP9600{

0 commit comments

Comments
 (0)