Skip to content

Commit b2e5a1b

Browse files
author
FischerMoseley
committed
Added functions for interfacing with temperature alerts
Also added a function for detecting if the input range has been exceeded, and also fixed some whitespace.
1 parent ad66b22 commit b2e5a1b

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

src/SparkFun_MCP9600.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ signed long MCP9600::rawADC(){
8383
}
8484
}
8585

86+
bool MCP9600::inputRangeExceeded(){
87+
uint8_t status = readSingleRegister(SENSOR_STATUS);
88+
return bitRead(status, 4);
89+
}
90+
8691

8792
/*--------------------------- Measurement Configuration --------------- */
8893

@@ -225,6 +230,116 @@ Shutdown_Mode MCP9600::getShutdownMode(){
225230
return mode; //clear all bits except the last two and return
226231
}
227232

233+
/*---------------------------- Temperature Alerts ------------------- */
234+
235+
bool configAlert(uint8_t number, float tempLimit, bool junction, uint8_t hysteresis, bool edge, bool activity, bool mode, bool enable){
236+
/* Parameters:
237+
uint8_t number: What temperature alert to configure. Value can range from 1-4.
238+
bool junction: Which junction to latch the alert to. Set to 0 for the thermocouple (hot) junction, or 1 for the ambient (cold) junction
239+
uint8_t hysteresis: How many degrees Celcius of hysteresis to use in the alert. More information on how this behaves is on pg. 33 of the datasheet
240+
bool edge: Configures whether to trigger on the junction going from cold -> hot (rising) or from hot -> cold (falling). Set to true for rising, false for falling.
241+
bool activity: Configures whether the output pin is active-high or active-low. Set to true for active high, false for active low.
242+
bool mode: Configures whether the output pin acts as an interrupt or a comparator. If configured as an interrupt, the interrupt must be cleared
243+
in software before the hardware interrupt pin is deasserted. Set to true for interrupt mode, false for comparator mode.
244+
bool enable: Configures whether or not the alert output pin is enabled. */
245+
246+
MCP9600_Register config_reg; //pick the set of registers we need to write to
247+
MCP9600_Register hysteresis_reg;
248+
MCP9600_Register limit_reg
249+
switch (number){
250+
case 1:
251+
config_reg = ALERT1_CONFIG;
252+
hysteresis_reg = ALERT1_HYSTERESIS;
253+
limit_reg = ALERT1_LIMIT;
254+
break;
255+
256+
case 2:
257+
config_reg = ALERT2_CONFIG;
258+
hysteresis_reg = ALERT2_HYSTERESIS;
259+
limit_reg = ALERT2_LIMIT;
260+
break;
261+
262+
case 3:
263+
config_reg = ALERT3_CONFIG;
264+
hysteresis_reg = ALERT3_HYSTERESIS;
265+
limit_reg = ALERT3_LIMIT;
266+
break;
267+
268+
case 4:
269+
config_reg = ALERT4_CONFIG;
270+
hysteresis_reg = ALERT4_HYSTERESIS;
271+
limit_reg = ALERT4_LIMIT;
272+
break;
273+
274+
default: //if the user didn't pick a valid register, just return with error
275+
return 1;
276+
break;
277+
}
278+
279+
//convert the user's parameters to register values
280+
//configuration register
281+
uint8_t config = 0;
282+
if(junction) bitWrite(config, 4, 1);
283+
if(edge) bitWrite(config, 3, 1);
284+
if(activity) bitWrite(config, 2, 1);
285+
if(mode) bitWrite(config, 1, 1);
286+
if(enable) bitWrite(config, 0, 1);
287+
288+
//temperature limit
289+
uint16_t unsignedLimit = static_cast<uint16_t>(abs(tempLimit) * 4);
290+
unsignedLimit = unsignedLimit << 2;
291+
int16_t signedLimit = static_cast<int16_t> unsignedLimit;
292+
if(tempLimit < 0) signedLimit *= -1; //if the original temp limit was negative we shifted away the sign bit, so reapply it if necessary
293+
294+
295+
//write computed values
296+
bool failed = writeSingleRegister(config_reg, config);
297+
failed |= writeSingleRegister(hysteresis_reg, hysteresis);
298+
failed |= writeSingleRegister(limit_reg, highByte(limit)); //by writing the high and low bytes separately we shouldn't have to worry about any
299+
failed |= writeSingleRegister(limit_reg + 0x01, lowByte(limit)); //signed or unsigned integer typecasts!
300+
return failed;
301+
}
302+
303+
bool MCP9600::clearAlert(uint8_t number){
304+
MCP9600_Register alertConfigRegister; //pick the register we need to use
305+
switch (number) {
306+
case 1:
307+
alertConfigRegister = ALERT1_CONFIG;
308+
break;
309+
310+
case 2:
311+
alertConfigRegister = ALERT2_CONFIG;
312+
break;
313+
314+
case 3:
315+
alertConfigRegister = ALERT3_CONFIG;
316+
break;
317+
318+
case 4:
319+
alertConfigRegister = ALERT4_CONFIG;
320+
break;
321+
322+
default:
323+
return void;
324+
break;
325+
}
326+
//grab the data already in the register so we don't override any other settings!
327+
uint8_t alertConfig = readSingleRegister(alertConfigRegister);
328+
bitWrite(alertConfig, 7, 1);
329+
330+
//write the new register to the MCP9600, return if it was successful
331+
return writeSingleRegister(alertConfigRegister, alertConfig);
332+
333+
}
334+
335+
bool MCP9600::isAlertTriggered(uint8_t number){
336+
if(number > 4) return void; //if a nonexistent alert number is given, return with nothing
337+
uint8_t status = readSingleRegister(SENSOR_STATUS);
338+
339+
return bitRead(status, number - 0x01);
340+
}
341+
342+
228343
/*------------------------- Internal I2C Abstraction ---------------- */
229344

230345
uint8_t MCP9600::readSingleRegister(MCP9600_Register reg){

src/SparkFun_MCP9600.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ enum MCP9600_Register {
3838
SENSOR_STATUS = 0x04,
3939
THERMO_SENSOR_CONFIG = 0x05,
4040
DEVICE_CONFIG = 0x06,
41+
ALERT1_CONFIG = 0x08,
42+
ALERT2_CONFIG = 0x09,
43+
ALERT3_CONFIG = 0x0A,
44+
ALERT4_CONFIG = 0x0B,
45+
ALERT1_HYSTERESIS = 0x0C,
46+
ALERT2_HYSTERESIS = 0x0D,
47+
ALERT3_HYSTERESIS = 0x0E,
48+
ALERT4_HYSTERESIS = 0x0F,
49+
ALERT1_LIMIT = 0x10,
50+
ALERT2_LIMIT = 0x11,
51+
ALERT3_LIMIT = 0x12,
52+
ALERT4_LIMIT = 0x13,
4153
DEVICE_ID = 0x20,
4254
};
4355

@@ -98,12 +110,13 @@ class MCP9600{
98110
float ambientTemp(); //Returns the ambient (IC die) temperature in degrees Celcius
99111
float tempDelta(); //Returns the difference in temperature between the thermocouple and ambient junctions, in degrees Celcius
100112
signed long rawADC(); //Returns the raw contents of the raw ADC register
113+
bool inputRangeExceeded(); //Returns true if the MCP9600's EMF range has been exceeded, and false otherwise.
101114

102115
//Measurement configuration
103116
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.
104117
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.
105118
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.
119+
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.
107120

108121
uint8_t setThermocoupleType(Thermocouple_Type type); //Changes the type of thermocouple connected to the MCP9600. Supported types are KJTNSEBR.
109122
Thermocouple_Type getThermocoupleType(); //Returns the type of thermocouple connected to the MCP9600 as found in its configuration register. Supported types are KJTNSEBR.
@@ -117,6 +130,14 @@ class MCP9600{
117130
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.
118131
Shutdown_Mode getShutdownMode(); //Returns the shutdown "operating" mode of the MCP9600. Configurable to Normal, Shutdown, and Burst.
119132

133+
134+
//Temperature Alerts
135+
bool configAlert(uint8_t number, float tempLimit, //Configures the alert with the number of the alert to use, whether to use the hot or cold junction, how much hysteresis to use (in degrees C),
136+
bool junction, uint8_t hysteresis, bool edge, //which edge to trigger on (rising or falling), activity (active high or active low), mode (comparator or interrupt), and whether everything is enabled
137+
bool activity, bool mode, bool enable);
138+
bool clearAlert(uint8_t number); //Clears the interrupt bit on the specified alert channel
139+
bool isAlertTriggered(uint8_t number); //Returns true if the interrupt has been triggered, false otherwise
140+
120141
//Internal I2C Abstraction
121142
private:
122143
TwoWire *_i2cPort; //Generic connection to user's chosen I2C port

0 commit comments

Comments
 (0)