Skip to content

Commit 09fe262

Browse files
author
FischerMoseley
committed
Reworked some examples, made I2C abstraction functions public class members
1 parent f413187 commit 09fe262

File tree

4 files changed

+79
-31
lines changed

4 files changed

+79
-31
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 = TYPE_S; //the type of thermocouple to change to!
22+
Thermocouple_Type type = TYPE_S; //the type of thermocouple to change to!
2323

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

examples/Example6_ConfigureTemperatureAlert/Example6_ConfigureTemperatureAlert.ino

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ uint8_t risingAlert = 1; //What alert to use for detecting cold -> hot transitio
2626
uint8_t fallingAlert = 3; //What alert to use for detecting hot -> cold transitions.
2727
//These numbers are arbitrary and can be anything from 1 to 4, but just can't be equal!
2828

29-
float alertTemp = 25.5; //What temperature to trigger the alert at (before hysteresis).
29+
float alertTemp = 29.5; //What temperature to trigger the alert at (before hysteresis).
3030
//This is about the surface temperature of my finger, but please change this if
3131
//you have colder/warmer hands or if the ambient temperature is different.
3232
uint8_t hysteresis = 2; //How much hysteresis to have, in degrees Celcius. Feel free to adjust this, but 2°C seems to be about right.
@@ -53,48 +53,71 @@ void setup(){
5353
while(1); //hang forever
5454
}
5555

56-
//configure the temperature alert for when the thermocouple is touched
57-
tempSensor.configAlertEnable(risingAlert, 0); //disable the alert (if it was already enabled) while we configure it
56+
tempSensor.configAlertHysteresis(risingAlert, hysteresis);
5857
tempSensor.configAlertTemp(risingAlert, alertTemp);
5958
tempSensor.configAlertJunction(risingAlert, 0);
60-
tempSensor.configAlertHysteresis(risingAlert, hysteresis);
6159
tempSensor.configAlertEdge(risingAlert, HIGH);
6260
tempSensor.configAlertLogicLevel(risingAlert, HIGH);
6361
tempSensor.configAlertMode(risingAlert, 1);
64-
tempSensor.configAlertEnable(risingAlert, 1); //enable the alert!
62+
tempSensor.configAlertEnable(risingAlert, true);
6563

66-
//configure the temperature alert for when the thermocouple is released!
67-
tempSensor.configAlertEnable(fallingAlert, 0); //disable the alert (if it was already enabled) while we configure it
64+
tempSensor.configAlertHysteresis(fallingAlert, hysteresis);
6865
tempSensor.configAlertTemp(fallingAlert, alertTemp);
6966
tempSensor.configAlertJunction(fallingAlert, 0);
70-
tempSensor.configAlertHysteresis(fallingAlert, hysteresis);
7167
tempSensor.configAlertEdge(fallingAlert, HIGH);
7268
tempSensor.configAlertLogicLevel(fallingAlert, HIGH);
7369
tempSensor.configAlertMode(fallingAlert, 1);
74-
tempSensor.configAlertEnable(fallingAlert, 1); //enable the alert!
70+
tempSensor.configAlertEnable(fallingAlert, true);
71+
72+
Serial.print("alert 1 hysteresis: ");
73+
Serial.println(tempSensor.readSingleRegister(ALERT1_HYSTERESIS), BIN);
74+
Serial.print("alert 1 limit: ");
75+
Serial.println(tempSensor.readDoubleRegister(ALERT1_LIMIT), BIN);
76+
Serial.print("alert 1 config: ");
77+
Serial.println(tempSensor.readSingleRegister(ALERT1_CONFIG), BIN);
78+
79+
Serial.print("alert 3 hysteresis: ");
80+
Serial.println(tempSensor.readSingleRegister(ALERT3_HYSTERESIS), BIN);
81+
Serial.print("alert 3 limit: ");
82+
Serial.println(tempSensor.readDoubleRegister(ALERT3_LIMIT), BIN);
83+
Serial.print("alert 3 config: ");
84+
Serial.println(tempSensor.readSingleRegister(ALERT3_CONFIG), BIN);
7585
}
7686

7787
unsigned long clock = millis();
7888
uint16_t updateTime = 200;
7989
void loop(){
80-
if(clock < (millis() + updateTime)){
90+
if((clock + updateTime) < millis()){
8191
Serial.print("Thermocouple: ");
8292
Serial.print(tempSensor.thermocoupleTemp());
8393
Serial.print(" °C Ambient: ");
8494
Serial.print(tempSensor.ambientTemp());
8595
Serial.print(" °C Temperature Delta: ");
8696
Serial.print(tempSensor.tempDelta());
8797
Serial.print(" °C");
98+
99+
if(tempSensor.isTempGreaterThanLimit(risingAlert)){
100+
Serial.print(" Temperature exceeds limit 1!");
101+
}
102+
103+
if(tempSensor.isTempGreaterThanLimit(fallingAlert)){
104+
Serial.print(" Temperature exeeds limit 3!");
105+
}
106+
88107
Serial.println();
108+
clock = millis();
89109
}
90110

91-
if(tempSensor.isAlertTriggered(risingAlert)){
92-
Serial.println("Thermocouple has been touched!");
93-
tempSensor.clearAlert(risingAlert);
94-
}
111+
if(Serial.available()){
112+
byte foo = Serial.read();
113+
if(foo == '1'){
114+
Serial.print("clearing alert 1: ");
115+
Serial.println(tempSensor.clearAlertPin(1));
116+
}
95117

96-
if(tempSensor.isAlertTriggered(fallingAlert)){
97-
Serial.println("Thermocouple has been released!");
98-
tempSensor.clearAlert(fallingAlert);
118+
if(foo == '3'){
119+
Serial.print("clearing alert 3: ");
120+
Serial.println(tempSensor.clearAlertPin(3));
121+
}
99122
}
100123
}

src/SparkFun_MCP9600.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ bool MCP9600::configAlertTemp(uint8_t number, float temp){
260260
if(temp < 0) signedTempLimit *= -1; //if the original temp limit was negative we shifted away the sign bit, so reapply it if necessary
261261

262262
//write the new temp limit to the MCP9600, return if it was successful
263-
bool failed = writeSingleRegister(tempLimitRegister, highByte(signedTempLimit));
264-
failed |= writeSingleRegister(tempLimitRegister + 0x01, lowByte(signedTempLimit));
265-
return failed;
263+
return writeDoubleRegister(tempLimitRegister, signedTempLimit);
266264
}
267265

268266
bool MCP9600::configAlertJunction(uint8_t number, bool junction){
@@ -419,7 +417,7 @@ bool MCP9600::configAlertEnable(uint8_t number, bool enable){
419417
return writeSingleRegister(alertConfigRegister, config);
420418
}
421419

422-
bool MCP9600::clearAlert(uint8_t number){
420+
bool MCP9600::clearAlertPin(uint8_t number){
423421
MCP9600_Register alertConfigRegister; //pick the register we need to use
424422
switch (number) {
425423
case 1:
@@ -448,14 +446,28 @@ bool MCP9600::clearAlert(uint8_t number){
448446

449447
//write the new register to the MCP9600, return if it was successful
450448
return writeSingleRegister(alertConfigRegister, alertConfig);
451-
452449
}
453450

454-
bool MCP9600::isAlertTriggered(uint8_t number){
451+
bool MCP9600::isTempGreaterThanLimit(uint8_t number){
455452
if(number > 4) return; //if a nonexistent alert number is given, return with nothing
456453
uint8_t status = readSingleRegister(SENSOR_STATUS);
457-
458-
return bitRead(status, number - 0x01);
454+
switch (number){
455+
case 1:
456+
return bitRead(status, 0);
457+
break;
458+
case 2:
459+
return bitRead(status, 1);
460+
break;
461+
case 3:
462+
return bitRead(status, 2);
463+
break;
464+
case 4:
465+
return bitRead(status, 3);
466+
break;
467+
default:
468+
return;
469+
break;
470+
}
459471
}
460472

461473

@@ -493,4 +505,12 @@ bool MCP9600::writeSingleRegister(MCP9600_Register reg, uint8_t data){
493505
_i2cPort->write(reg);
494506
_i2cPort->write(data);
495507
return (_i2cPort->endTransmission() != 0);
508+
}
509+
510+
bool MCP9600::writeDoubleRegister(MCP9600_Register reg, uint16_t data){
511+
_i2cPort->beginTransmission(_deviceAddress);
512+
_i2cPort->write(reg);
513+
_i2cPort->write(highByte(data));
514+
_i2cPort->write(lowByte(data));
515+
return (_i2cPort->endTransmission() != 0);
496516
}

src/SparkFun_MCP9600.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,20 @@ class MCP9600{
139139
bool configAlertLogicLevel(uint8_t number, bool level); //Configures whether the hardware alert pin is active-high or active-low. Set to 1 for active-high, 0 for active-low.
140140
bool configAlertMode(uint8_t number, bool mode); //Configures whether the MCP9600 treats the alert like a comparator or an interrrupt. Set to 1 for interrupt, 0 for comparator. More information is on pg. 34 of the datasheet.
141141
bool configAlertEnable(uint8_t number, bool enable); //Configures whether or not the interrupt is enabled or not. Set to 1 to enable, or 0 to disable.
142-
bool clearAlert(uint8_t number); //Clears the interrupt bit on the specified alert channel
143-
bool isAlertTriggered(uint8_t number); //Returns true if the interrupt has been triggered, false otherwise
142+
bool clearAlertPin(uint8_t number); //Clears the interrupt on the specified alert channel, resetting the value of the pin.
143+
bool isTempGreaterThanLimit(uint8_t number); //Returns true if the interrupt has been triggered, false otherwise
144144

145+
146+
//debug
147+
uint8_t readSingleRegister(MCP9600_Register reg); //Attempts to read a single register, will keep trying for retryAttempts amount of times
148+
uint16_t readDoubleRegister(MCP9600_Register reg); //Attempts to read two registers, will keep trying for retryAttempts amount of times
149+
bool writeSingleRegister(MCP9600_Register reg, uint8_t data); //Attempts to write data into a single 8-bit register. Does not check to make sure it was written successfully. Returns 0 if there wasn't an error on I2C transmission, and 1 otherwise.
150+
bool writeDoubleRegister(MCP9600_Register reg, uint16_t data); //Attempts to write data into a double (two 8-bit) registers. Does not check to make sure it was written successfully. Returns 0 if there wasn't an error on I2C transmission, and 1 otherwise.
151+
152+
145153
//Internal I2C Abstraction
146154
private:
147155
TwoWire *_i2cPort; //Generic connection to user's chosen I2C port
148156
uint8_t _deviceAddress; //I2C address of the MCP9600
149-
uint8_t readSingleRegister(MCP9600_Register reg); //Attempts to read a single register, will keep trying for retryAttempts amount of times
150-
uint16_t readDoubleRegister(MCP9600_Register reg); //Attempts to read two registers, will keep trying for retryAttempts amount of times
151-
bool writeSingleRegister(MCP9600_Register reg, uint8_t data); //Attempts to write data into a single 8-bit register. Does not check to make sure it was written successfully.
152157
};
153158
#endif

0 commit comments

Comments
 (0)