Skip to content

Commit

Permalink
Merge pull request #13 from mikeysklar/setAmbientResolution
Browse files Browse the repository at this point in the history
setAmbientResolution()
  • Loading branch information
ladyada authored Oct 30, 2024
2 parents bbb08d9 + 930d661 commit 6ea508e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
14 changes: 14 additions & 0 deletions Adafruit_MCP9600.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,17 @@ uint8_t Adafruit_MCP9600::getStatus(void) {

return status.read();
}

/**************************************************************************/
/*!
@brief Sets the resolution for ambient (cold junction) temperature readings
@param res_value Ambient_Resolution enum value to set resolution
*/
/**************************************************************************/
void Adafruit_MCP9600::setAmbientResolution(Ambient_Resolution res_value) {
uint8_t config;
_config_reg->read(&config, 1); // Read the current configuration
config &= ~0xC0; // Clear existing resolution bits
config |= ((~res_value & 0x03) << 6); // Set inverted resolution bits
_config_reg->write(config); // Write back the configuration
}
10 changes: 10 additions & 0 deletions Adafruit_MCP9600.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ typedef enum _resolution {
MCP9600_ADCRESOLUTION_12,
} MCP9600_ADCResolution;

/*! The possible Ambient resolutions */
typedef enum {
RES_ZERO_POINT_25 = 0b00, ///< 0.25°C
RES_ZERO_POINT_125 = 0b01, ///< 0.125°C
RES_ZERO_POINT_0625 = 0b10, ///< 0.0625°C
RES_ZERO_POINT_03125 = 0b11 ///< 0.03125°C
} Ambient_Resolution;

/**************************************************************************/
/*!
@brief MCP9600 driver.
Expand Down Expand Up @@ -91,6 +99,8 @@ class Adafruit_MCP9600 {
MCP9600_ADCResolution getADCresolution(void);
int32_t readADC(void);

void setAmbientResolution(Ambient_Resolution res_value);

void setAlertTemperature(uint8_t alert, float temp);
float getAlertTemperature(uint8_t alert);
void configureAlert(uint8_t alert, bool enabled, bool rising,
Expand Down
33 changes: 23 additions & 10 deletions examples/mcp9600_test/mcp9600_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@

Adafruit_MCP9600 mcp;

/* Set and print ambient resolution */
Ambient_Resolution ambientRes = RES_ZERO_POINT_0625;

void setup()
{
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("MCP9600 HW test");
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("MCP9600 HW test");

/* Initialise the driver with I2C_ADDRESS and the default I2C bus. */
if (! mcp.begin(I2C_ADDRESS)) {
Serial.println("Sensor not found. Check wiring!");
while (1);
}
/* Initialise the driver with I2C_ADDRESS and the default I2C bus. */
if (! mcp.begin(I2C_ADDRESS)) {
Serial.println("Sensor not found. Check wiring!");
while (1);
}

Serial.println("Found MCP9600!");

/* Set and print ambient resolution */
mcp.setAmbientResolution(ambientRes);
Serial.print("Ambient Resolution set to: ");
switch (ambientRes) {
case RES_ZERO_POINT_25: Serial.println("0.25°C"); break;
case RES_ZERO_POINT_125: Serial.println("0.125°C"); break;
case RES_ZERO_POINT_0625: Serial.println("0.0625°C"); break;
case RES_ZERO_POINT_03125: Serial.println("0.03125°C"); break;
}

mcp.setADCresolution(MCP9600_ADCRESOLUTION_18);
Serial.print("ADC resolution set to ");
switch (mcp.getADCresolution()) {
Expand Down

0 comments on commit 6ea508e

Please sign in to comment.