Skip to content

Commit 8e7c643

Browse files
author
FischerMoseley
committed
Added mode (burst/shutdown) configuration ability
Currently untested
1 parent ef69944 commit 8e7c643

File tree

5 files changed

+186
-34
lines changed

5 files changed

+186
-34
lines changed

examples/Example2_SetThermocoupleType/Example2_SetThermocoupleType.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <SparkFun_MCP9600.h>
2121
MCP9600 tempSensor;
22-
thermocoupleType type = S_type; //the type of thermocouple to change to!
22+
thermocoupleType type = TYPE_S; //the type of thermocouple to change to!
2323

2424
void setup(){
2525
Serial.begin(115200);

examples/Example4_SetResolution/Example4_SetResolution.ino

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
#include <SparkFun_MCP9600.h>
4444
MCP9600 tempSensor;
4545

46-
ambientResolution ambientRes = RES_ZERO_POINT_0625;
47-
thermocoupleResolution thermocoupleRes = RES_14_BIT;
46+
Ambient_Resolution ambientRes = RES_ZERO_POINT_0625;
47+
Thermocouple_Resolution thermocoupleRes = RES_14_BIT;
4848

4949
void setup(){
5050
Serial.begin(115200);
@@ -69,12 +69,6 @@ void setup(){
6969
//set the resolution on the ambient (cold) junction
7070
tempSensor.setAmbientResolution(ambientRes);
7171
tempSensor.setThermocoupleResolution(thermocoupleRes);
72-
73-
//this is just debug, remove it if you're reading this
74-
Serial.print("Ambient Resolution Config successful?: ");
75-
Serial.println(tempSensor.getAmbientResolution() == ambientRes);
76-
Serial.print("Thermocouple Resolution Config successful?: ");
77-
Serial.println(tempSensor.getThermocoupleResolution() == thermocoupleRes);
7872
}
7973

8074
void loop(){ //print the thermocouple, ambient and delta temperatures every 200ms
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Configuring Burst Mode with the MCP9600 Thermocouple Amplifier
3+
By: Fischer Moseley
4+
SparkFun Electronics
5+
Date: July 10, 2019
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware License).
7+
8+
This example configures the shutdown (or "operating") mode that the MCP9600 runs in. Shutdown mode disables all
9+
power consuming activities on the MCP9600, including measurements, but it will still respond to I2C commands sent
10+
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!
13+
14+
Hardware Connections:
15+
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
16+
Plug the sensor onto the shield
17+
Serial.print it out at 115200 baud to serial monitor.
18+
*/
19+
20+
#include <SparkFun_MCP9600.h>
21+
MCP9600 tempSensor;
22+
Shutdown_Mode mode = BURST;
23+
Burst_Sample samples = SAMPLES_32;
24+
25+
void setup(){
26+
Serial.begin(115200);
27+
//check if the sensor is connected
28+
if(tempSensor.isConnected()){
29+
Serial.println("Device will acknowledge!");
30+
}
31+
else {
32+
Serial.println("Device did not acknowledge! Freezing.");
33+
while(1); //hang forever
34+
}
35+
36+
//check if the Device ID is correct
37+
if(tempSensor.checkDeviceID()){
38+
Serial.println("Device ID is correct!");
39+
}
40+
else {
41+
Serial.println("Device ID is not correct! Freezing.");
42+
while(1); //hang forever
43+
}
44+
45+
//put the MCP9600 into burst mode!
46+
tempSensor.setShutdownMode(BURST);
47+
tempSensor.setBurstSamples(samples);
48+
}
49+
50+
void loop(){
51+
if(tempSensor.burstAvailable()){ //if there's a new burst measurement, get the data and print it
52+
53+
//print the thermocouple, ambient and delta temperatures
54+
Serial.print("Thermocouple: ");
55+
Serial.print(tempSensor.thermocoupleTemp());
56+
Serial.print(" °C Ambient: ");
57+
Serial.print(tempSensor.ambientTemp());
58+
Serial.print(" °C Temperature Delta: ");
59+
Serial.print(tempSensor.tempDelta());
60+
Serial.print(" °C");
61+
Serial.println();
62+
63+
//clear the register and start a new burst cycle!
64+
tempSensor.startBurst();
65+
}
66+
delay(200); //we want to be able to see our data, so we won't let it print any faster than 200ms
67+
}

src/SparkFun_MCP9600.cpp

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,25 @@ float MCP9600::tempDelta(){
6868
return ((float)raw * DEV_RESOLUTION);
6969
}
7070

71+
signed long MCP9600::rawADC(){
72+
for(byte attempts; attempts <= retryAttempts; attempts++){
73+
_i2cPort->beginTransmission(DEV_ADDR);
74+
_i2cPort->write(RAW_ADC);
75+
_i2cPort->endTransmission();
76+
77+
if(_i2cPort->requestFrom(DEV_ADDR, 3) != 0){
78+
signed long data = _i2cPort->read() << 16;
79+
data |= _i2cPort->read() << 8;
80+
data |= _i2cPort->read();
81+
return data;
82+
}
83+
}
84+
}
85+
7186

7287
/*--------------------------- Measurement Configuration --------------- */
7388

74-
bool MCP9600::setAmbientResolution(ambientResolution res){
89+
bool MCP9600::setAmbientResolution(Ambient_Resolution res){
7590
uint8_t config = readSingleRegister(DEVICE_CONFIG); //get current device configuration so we don't wipe everything else
7691
bitWrite(config, 7, res); //set the bit that controls the ambient (cold) junction resolution
7792

@@ -80,12 +95,12 @@ bool MCP9600::setAmbientResolution(ambientResolution res){
8095
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
8196
}
8297

83-
ambientResolution MCP9600::getAmbientResolution(){
98+
Ambient_Resolution MCP9600::getAmbientResolution(){
8499
uint8_t config = readSingleRegister(DEVICE_CONFIG); //grab current device configuration
85100
return bitRead(config, 7); //return 7th bit from config register
86101
}
87102

88-
bool MCP9600::setThermocoupleResolution(thermocoupleResolution res){
103+
bool MCP9600::setThermocoupleResolution(Thermocouple_Resolution res){
89104
uint8_t config = readSingleRegister(DEVICE_CONFIG); //grab current device configuration so we don't wipe everything else
90105
bool highResolutionBit = bitRead(res, 1);
91106
bool lowResolutionBit = bitRead(res, 0);
@@ -97,17 +112,17 @@ bool MCP9600::setThermocoupleResolution(thermocoupleResolution res){
97112
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
98113
}
99114

100-
thermocoupleResolution MCP9600::getThermocoupleResolution(){
115+
Thermocouple_Resolution MCP9600::getThermocoupleResolution(){
101116
uint8_t config = readSingleRegister(DEVICE_CONFIG); //grab current device configuration
102-
thermocoupleResolution res; //define new thermocoupleResolution enum to return
117+
Thermocouple_Resolution res; //define new thermocoupleResolution enum to return
103118
bool highResolutionBit = bitRead(config, 6);
104119
bool lowResolutionBit = bitRead(config, 5);
105120
bitWrite(res, 1, highResolutionBit); //set 1st bit of the enum to the 6th bit of the config register
106121
bitWrite(res, 0, lowResolutionBit); //set 0th bit of the enum to the 5th bit of the config register
107122
return res;
108123
}
109124

110-
uint8_t MCP9600::setThermocoupleType(thermocoupleType type){
125+
uint8_t MCP9600::setThermocoupleType(Thermocouple_Type type){
111126
uint8_t config = readSingleRegister(THERMO_SENSOR_CONFIG); //grab current device configuration so we don't wipe everything else
112127
bitClear(config, 4); //clear the necessary bits so that they can be set
113128
bitClear(config, 5);
@@ -119,7 +134,7 @@ uint8_t MCP9600::setThermocoupleType(thermocoupleType type){
119134
return 0; //otherwise return 0
120135
}
121136

122-
thermocoupleType MCP9600::getThermocoupleType(){
137+
Thermocouple_Type MCP9600::getThermocoupleType(){
123138
uint8_t config = readSingleRegister(THERMO_SENSOR_CONFIG);
124139
return (config >> 4); //clear the non-thermocouple-type bits in the config registe
125140
}
@@ -141,6 +156,57 @@ uint8_t MCP9600::getFilterCoeffecients(){
141156
return ((config << 5) >> 5); //clear the non-filter-coeffecients data in the config register
142157
}
143158

159+
bool MCP9600::setBurstSamples(Burst_Sample samples){
160+
uint8_t config = readSingleRegister(DEVICE_CONFIG);
161+
bool highResolutionBit = bitRead(samples, 2);
162+
bool midResolutionBit = bitRead(samples, 1);
163+
bool lowResolutionBit = bitRead(samples, 0);
164+
bitWrite(config, 4, highResolutionBit); //write 2nd bit of samples to 4th of config
165+
bitWrite(config, 3, midResolutionBit); //write 1st bit of samples to 3rd of config
166+
bitWrite(config, 2, lowResolutionBit); //write 0th bit of samples to 2nd of config
167+
168+
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
170+
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
171+
}
172+
173+
Burst_Sample MCP9600::getBurstSamples(){
174+
uint8_t config = readSingleRegister(DEVICE_CONFIG);
175+
bool highResolutionBit = bitRead(config, 4);
176+
bool midResolutionBit = bitRead(config, 3);
177+
bool lowResolutionBit = bitRead(config, 2);
178+
Burst_Sample samples;
179+
bitWrite(samples, 2, highResolutionBit); //write 4th bit of config to 2nd bit of samples
180+
bitWrite(samples, 1, midResolutionBit); //write 3rd bit of config to 1st bit of samples
181+
bitWrite(samples, 0, lowResolutionBit); //write 2nd bit of config to 0th bit of samples
182+
return samples;
183+
}
184+
185+
bool MCP9600::burstAvailable(){
186+
uint8_t status = readSingleRegister(SENSOR_STATUS);
187+
return (status >> 7); //return only the 7th bit where the burst complete flag is
188+
}
189+
190+
bool MCP9600::startBurst(){
191+
uint8_t status = readSingleRegister(SENSOR_STATUS);
192+
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
194+
}
195+
196+
bool MCP9600::setShutdownMode(Shutdown_Mode mode){
197+
uint8_t config = readSingleRegister(DEVICE_CONFIG);
198+
config = (config >> 2) << 2; //clear last two bits of the device config register
199+
config |= mode;
200+
201+
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
203+
return failed; //return 1 if the write failed or the register wasn't properly set, 0 otherwise
204+
}
205+
206+
Shutdown_Mode MCP9600::getShutdownMode(){
207+
uint8_t config = readSingleRegister(DEVICE_CONFIG);
208+
return ((config << 6) >> 6); //clear all bits except the last two and return
209+
}
144210

145211
/*------------------------- Internal I2C Abstraction ---------------- */
146212

src/SparkFun_MCP9600.h

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,53 @@ enum MCP9600_Register {
3434
HOT_JUNC_TEMP = 0x00,
3535
DELTA_JUNC_TEMP = 0x01,
3636
COLD_JUNC_TEMP = 0x02,
37-
STATUS = 0x04,
37+
RAW_ADC = 0x03,
38+
SENSOR_STATUS = 0x04,
3839
THERMO_SENSOR_CONFIG = 0x05,
3940
DEVICE_CONFIG = 0x06,
4041
DEVICE_ID = 0x20,
4142
};
4243

43-
enum thermocoupleType {
44-
K_type = 0b000,
45-
J_type = 0b001,
46-
T_type = 0b010,
47-
N_type = 0b011,
48-
S_type = 0b100,
49-
E_type = 0b101,
50-
B_type = 0b110,
51-
R_type = 0b111,
44+
enum Thermocouple_Type {
45+
TYPE_K = 0b000,
46+
TYPE_J = 0b001,
47+
TYPE_T = 0b010,
48+
TYPE_N = 0b011,
49+
TYPE_S = 0b100,
50+
TYPE_E = 0b101,
51+
TYPE_B = 0b110,
52+
TYPE_R = 0b111,
5253
};
5354

54-
enum ambientResolution {
55+
enum Ambient_Resolution {
5556
RES_ZERO_POINT_0625 = 0b0,
5657
RES_ZERO_POINT_25 = 0b1,
5758
};
5859

59-
enum thermocoupleResolution {
60+
enum Thermocouple_Resolution {
6061
RES_18_BIT = 0b00,
6162
RES_16_BIT = 0b01,
6263
RES_14_BIT = 0b10,
6364
RES_12_BIT = 0b11,
6465
};
6566

67+
enum Burst_Sample {
68+
SAMPLES_1 = 0b000,
69+
SAMPLES_2 = 0b001,
70+
SAMPLES_4 = 0b010,
71+
SAMPLES_8 = 0b011,
72+
SAMPLES_16 = 0b100,
73+
SAMPLES_32 = 0b101,
74+
SAMPLES_64 = 0b110,
75+
SAMPLES_128 = 0b111,
76+
};
77+
78+
enum Shutdown_Mode {
79+
NORMAL = 0b00,
80+
SHUTDOWN = 0b01,
81+
BURST = 0b10,
82+
};
83+
6684
class MCP9600{
6785
public:
6886

@@ -79,18 +97,25 @@ class MCP9600{
7997
float thermocoupleTemp(); //Returns the thermocouple temperature in degrees Celcius
8098
float ambientTemp(); //Returns the ambient (IC die) temperature in degrees Celcius
8199
float tempDelta(); //Returns the difference in temperature between the thermocouple and ambient junctions, in degrees Celcius
100+
signed long rawADC(); //Returns the raw contents of the raw ADC register
82101

83102
//Measurement configuration
84-
bool setAmbientResolution(ambientResolution res); //Changes the resolution on the cold (ambient) junction, for either 0.0625 or 0.25 degree C resolution. Lower resolution reduces conversion time.
85-
ambientResolution getAmbientResolution(); //Returns the resolution on the cold (ambient) junction, for either 0.0625 or 0.25 degree C resolution. Lower resolution reduces conversion time.
86-
bool setThermocoupleResolution(thermocoupleResolution res); //Changes the resolution on the hot (thermocouple) junction, for either 18, 16, 14, or 12-bit resolution. Lower resolution reduces conversion time.
87-
thermocoupleResolution getThermocoupleResolution(); //Returns the resolution on the hot (thermocouple) junction, for either 18, 16, 14, or 12-bit resolution. Lower resolution reduces conversion time.
103+
bool setAmbientResolution(Ambient_Resolution res); //Changes the resolution on the cold (ambient) junction, for either 0.0625 or 0.25 degree C resolution. Lower resolution reduces conversion time.
104+
Ambient_Resolution getAmbientResolution(); //Returns the resolution on the cold (ambient) junction, for either 0.0625 or 0.25 degree C resolution. Lower resolution reduces conversion time.
105+
bool setThermocoupleResolution(Thermocouple_Resolution res); //Changes the resolution on the hot (thermocouple) junction, for either 18, 16, 14, or 12-bit resolution. Lower resolution reduces conversion time.
106+
Thermocouple_Resolution getThermocoupleResolution(); //Returns the resolution on the hot (thermocouple) junction, for either 18, 16, 14, or 12-bit resolution. Lower resolution reduces conversion time.
88107

89-
uint8_t setThermocoupleType(thermocoupleType type); //Changes the type of thermocouple connected to the MCP9600. Supported types are KJTNSEBR.
90-
thermocoupleType getThermocoupleType(); //Returns the type of thermocouple connected to the MCP9600 as found in its configuration register. Supported types are KJTNSEBR.
108+
uint8_t setThermocoupleType(Thermocouple_Type type); //Changes the type of thermocouple connected to the MCP9600. Supported types are KJTNSEBR.
109+
Thermocouple_Type getThermocoupleType(); //Returns the type of thermocouple connected to the MCP9600 as found in its configuration register. Supported types are KJTNSEBR.
91110
uint8_t setFilterCoeffecients(uint8_t coeffecient); //Changes the weight of the on-chip exponential moving average filter. Set this to 0 for no filter, 1 for minimum filter, and 7 for maximum filter.
92111
uint8_t getFilterCoeffecients(); //Returns the weight of the on-chip exponential moving average filter.
93112

113+
bool setBurstSamples(Burst_Sample samples); //Changes the amount of samples to take in burst mode. Returns 0 if set sucessfully, 1 otherwise.
114+
Burst_Sample getBurstSamples(); //Returns the amount of samples to take in burst mode, according to the device's configuration register.
115+
bool burstAvailable(); //Returns true if all the burst samples have been taken and the results are ready. Returns false otherwise.
116+
bool startBurst(); //Initiates a burst on the MCP9600.
117+
bool setShutdownMode(Shutdown_Mode mode); //Changes the shutdown "operating" mode of the MCP9600. Configurable to Normal, Shutdown, and Burst. Returns 0 if properly set, 1 otherwise.
118+
Shutdown_Mode getShutdownMode(); //Returns the shutdown "operating" mode of the MCP9600. Configurable to Normal, Shutdown, and Burst.
94119

95120
//Internal I2C Abstraction
96121
private:

0 commit comments

Comments
 (0)