upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Types | Public Member Functions

API for MCP9808 precision temprature sensor. More...

Detailed Description

The MCP9808 digital temperature sensor converts temperatures between -20 deg C and +100 deg C to a digital word with +/- 0.5 deg C (max.) accuracy. The MCP9808 comes with user-programmable registers that provide flexibility for temperature sensing applications. The registers allow user-selectable settings such as Shutdown or low-power modes and the specification of temperature Event and Critical output boundaries. When the temperature changes beyond the specified boundary limits, the MCP9808 outputs an Event signal. The user has the option of setting the event output signal polarity as an active-low or active-high comparator output for thermostat operation, or as temperature event interrupt output for microprocessor-based systems. The event output can also be configured as a Critical temperature output.

Tested with Adafriut MCP9808 board.

mcp9808.jpg
int command;
upm::MCP9808 temp(6);
do {
cout << endl;
cout << "1 - read temp \t";
cout << "2 - upm_delay mode \t";
cout << "3 - wake up" << endl;
cout << "4 - set mode to " << (temp.isCelsius() == true ? "Fahrenheit" : "Celsius") << endl;
cout << "5 - show status bits" << endl;
cout << "6 - Set Tcrit \t";
cout << "7 - Set Tupper \t";
cout << "8 - Set Tlower " << endl;
cout << "9 - Display monitor temps " << endl;
cout << "10 - Enable alert default\t";
cout << "11 - Enable alert interrupt" << endl;
cout << "12 - Clear interrupt \t";
cout << "13 - Clear alert mode" << endl;
cout << "14 - Get Hysteresis\t";
cout << "15 - Set Hysteresis" << endl;
cout << "16 - Get Resolution\t";
cout << "17 - Set Resolution" << endl;
cout << "18 - Get Manufacturer ID" << endl;
cout << "19 - Get Device ID" << endl;
cout << "-1 - exit" << endl;
cout << "Enter a command: ";
cin >> command;
switch (command) {
float t;
case 1:
std::cout << "Temp: " << temp.getTemp() << "* "
<< (temp.isCelsius() ? "Celsius" : "Fahrenheit") << std::endl;
break;
case 2:
cout << "shutdown sensor (upm_delay mode)" << endl;
temp.shutDown();
break;
case 3:
cout << "wake up sensor" << endl;
temp.shutDown(false);
break;
case 4:
cout << "set mode to " << (temp.isCelsius() ? "Fahrenheit" : "Celsius") << endl;
temp.setMode(!temp.isCelsius());
break;
case 5:
cout << "Tcrit = " << temp.isTcrit();
cout << " Tupper = " << temp.isTupper();
cout << " Tlower = " << temp.isTlower();
break;
case 6:
cout << "enter a value";
cin >> t;
temp.setMonitorReg(temp.CRIT_TEMP, t);
break;
case 7:
cout << "enter a value";
cin >> t;
temp.setMonitorReg(temp.UPPER_TEMP, t);
break;
case 8:
cout << "enter a value";
cin >> t;
temp.setMonitorReg(temp.LOWER_TEMP, t);
break;
case 9:
cout << "tcrit = " << temp.getMonitorReg(temp.CRIT_TEMP) << endl;
cout << "tupper = " << temp.getMonitorReg(temp.UPPER_TEMP) << endl;
cout << "tlower = " << temp.getMonitorReg(temp.LOWER_TEMP) << endl;
break;
case 10:
cout << "set alert mode default";
temp.setAlertMode(temp.ALERTCTRL);
break;
case 11:
cout << "set alert mode interrupt";
temp.setAlertMode(temp.ALERTMODE | temp.ALERTCTRL);
break;
case 12:
temp.clearInterrupt();
break;
case 13:
cout << "Clear alerts" << endl;
temp.clearAlertMode();
break;
case 14:
cout << "Hysteresis: " << temp.getHysteresis() << endl;
break;
case 15:
int u;
cout << "enter 1 to 4";
cin >> u;
switch (u) {
case 1:
temp.setHysteresis(temp.HYST_0);
break;
case 2:
temp.setHysteresis(temp.HYST_1_5);
break;
case 3:
temp.setHysteresis(temp.HYST_3_0);
break;
case 4:
default:
temp.setHysteresis(temp.HYST_6_0);
break;
}
break;
case 16:
cout << "Resolution: " << temp.getResolution() << endl;
break;
case 17:
int v;
cout << "enter 1 to 4";
cin >> v;
switch (v) {
case 1:
temp.setResolution(temp.RES_LOW);
break;
case 2:
temp.setResolution(temp.RES_MEDIUM);
break;
case 3:
temp.setResolution(temp.RES_HIGH);
break;
case 4:
default:
temp.setResolution(temp.RES_PRECISION);
break;
}
break;
case 18:
cout << "Manufacturer ID: " << std::hex << temp.getManufacturer() << endl;
break;
case 19:
cout << "Get device ID: " << std::hex << temp.getDevicedId() << endl;
break;
case -1:
break;
default:
cout << endl << "That option is not available. Try again" << endl;
break;
}
} while (command != -1);

Public Types

enum  MCP9808_REG { UPPER_TEMP = 0x02, LOWER_TEMP = 0x03, CRIT_TEMP = 0x04 }
 uint8_t enum containing register addresses used for setting temp thresholds for MCP9808
 
enum  MCP9808_RESOLUTION { RES_LOW = 0x00, RES_MEDIUM = 0x01, RES_HIGH = 0x02, RES_PRECISION = 0x03 }
 uint8_t enum containing the four possible values for MCP9808 resolution register.
 
enum  MCP9808_CONFIG {
  ALERTSTAT = 0x1000, ALERTCTRL = 0x0800, ALERTSEL = 0x0400, ALERTPOL = 0x0200,
  ALERTMODE = 0x0100, HYST_0 = 0x0000, HYST_1_5 = 0x0002, HYST_3_0 = 0x0004,
  HYST_6_0 = 0x0006
}
 uint16_t enum containing alert and hysteresis options for config register.
 

Public Member Functions

 MCP9808 (int bus, uint8_t address=0x18)
 
 ~MCP9808 ()
 
std::string name ()
 
float getTemp (void)
 
void shutDown (bool sleep=true)
 
void setMode (bool celsius=true)
 
bool isCelsius (void)
 
bool isTcrit ()
 
bool isTupper ()
 
bool isTlower ()
 
void setMonitorReg (MCP9808_REG reg, float value)
 
float getMonitorReg (MCP9808_REG reg)
 
void clearInterrupt (void)
 
void setAlertMode (uint16_t command)
 
void clearAlertMode (void)
 
void setHysteresis (MCP9808_CONFIG value)
 
float getHysteresis ()
 
void setResolution (MCP9808_RESOLUTION value)
 
float getResolution ()
 
uint16_t getManufacturer ()
 
uint16_t getDevicedId ()
 

Constructor & Destructor Documentation

MCP9808 ( int  bus,
uint8_t  address = 0x18 
)

MCP9808 constructor

Parameters
busi2c bus the sensor is attached to.
addressDevice address. Default is 0x18.
~MCP9808 ( )

MCP9808 destructor

Member Function Documentation

std::string name ( )
inline

Returns the name of the sensor

float getTemp ( void  )

Returns current temperature.

void shutDown ( bool  sleep = true)

Will cause the devices to either sleep or wakeup.

Parameters
sleepBool, default true to sleep. false to wake.
void setMode ( bool  celsius = true)
inline

setMode - sets temperature reporting mode.

Parameters
celsiusDefault is true. If false all temps will be reported in fahrenheit.
bool isCelsius ( void  )
inline

Returns true if mode is celsius False if fahrenheit.

bool isTcrit ( )
inline

Returns true if TCrit threshold has been crossed Reflects the state of the bit based on the most recent readTemp() operation.

bool isTupper ( )
inline

Returns true if TUpper threshold crossed Reflects the state of the bit based on the most recent readTemp() operation.

bool isTlower ( )
inline

Returns true if TLower threshold crossed. Reflects the state of the bit based on the most recent readTemp() operation.

void setMonitorReg ( MCP9808_REG  reg,
float  value 
)

Sets the value of TCrit, TUpper,TLower registers.

reg - MCP9808_REG enum UPPER_TEMP, LOWER_TEMP or CRIT_TEMP.

Parameters
value- float value representing the set value
float getMonitorReg ( MCP9808_REG  reg)

Returns the current value of TCrit, TUpper, TLower registers.

Parameters
reg- MCP9808_REG enum UPPER_TEMP, LOWER_TEMP or CRIT_TEMP.
void clearInterrupt ( void  )

Clears the interrupt when ALERT_MODE is set to interrupt output and temp threshold crossed.

void setAlertMode ( uint16_t  command)

Sets alert mode. Can use values from MCP9808_CONFIG enum. Values can be combined.

Parameters
command- a combination of options to set desired alert mode. See spcec sheet.
void clearAlertMode ( void  )

Clears Alert Mode – sets all params to default.

void setHysteresis ( MCP9808_CONFIG  value)

Sets hysteresis value.

Parameters
valueMCP9808_CONFIG enum value HYST_0, HYST_1_5, HYST_3_0 or HYST_6_0
float getHysteresis ( )

Returns hysteresis setting as a float value.

void setResolution ( MCP9808_RESOLUTION  value)

Sets resolution of temperature conversion.

Parameters
value- MCP9808_RESOLUTION enum value. RES_LOW = +0.5 C RES_MEDIUM = +0.25 C RES_HIGH = +0.125 C RES_PRECISION = +0.0625 C (default).
float getResolution ( )

Returns float value representing the current resolution setting.

uint16_t getManufacturer ( )

Returns Manufacturer ID. Typically 0x0054;

uint16_t getDevicedId ( )

Returns device ID and revision. Typically 0x0400 With ID in the High byte.


The documentation for this class was generated from the following files: