初始化提交
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
/*!
|
||||
* @file Adafruit_MCP9808_Soft.cpp
|
||||
*
|
||||
* @mainpage Adafruit MCP9808 I2C Temp Sensor
|
||||
*
|
||||
* @section intro_sec Introduction
|
||||
*
|
||||
* I2C Driver for Microchip's MCP9808 I2C Temp sensor
|
||||
*
|
||||
* This is a library for the Adafruit MCP9808 breakout:
|
||||
* http://www.adafruit.com/products/1782
|
||||
*
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
* please support Adafruit and open-source hardware by purchasing products from
|
||||
* Adafruit!
|
||||
*
|
||||
* @section author Author
|
||||
*
|
||||
* K.Townsend (Adafruit Industries)
|
||||
*
|
||||
* @section license License
|
||||
*
|
||||
* BSD (see license.txt)
|
||||
*
|
||||
* @section HISTORY
|
||||
*
|
||||
* v1.0 - First release
|
||||
*/
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#ifdef __AVR_ATtiny85__
|
||||
#include "TinyWireM.h"
|
||||
#define Wire TinyWireM
|
||||
#else
|
||||
#include <SoftwareWire.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "Adafruit_MCP9808_Soft.h"
|
||||
|
||||
/*!
|
||||
* @brief Instantiates a new MCP9808 class
|
||||
*/
|
||||
Adafruit_MCP9808_Soft::Adafruit_MCP9808_Soft() {}
|
||||
|
||||
/*!
|
||||
* @brief Setups the HW
|
||||
* @param *theWire
|
||||
* @return True if initialization was successful, otherwise false.
|
||||
*/
|
||||
bool Adafruit_MCP9808_Soft::begin(SoftwareWire *theWire) {
|
||||
_wire = theWire;
|
||||
_i2caddr = MCP9808_I2CADDR_DEFAULT;
|
||||
return init();
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Setups the HW
|
||||
* @param addr
|
||||
* @param *theWire
|
||||
* @return True if initialization was successful, otherwise false.
|
||||
*/
|
||||
bool Adafruit_MCP9808_Soft::begin(uint8_t addr, SoftwareWire *theWire) {
|
||||
_i2caddr = addr;
|
||||
_wire = theWire;
|
||||
return init();
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief init function
|
||||
* @return True if initialization was successful, otherwise false.
|
||||
*/
|
||||
bool Adafruit_MCP9808_Soft::init() {
|
||||
_wire->begin();
|
||||
|
||||
if (read16(MCP9808_REG_MANUF_ID) != 0x0054)
|
||||
return false;
|
||||
if (read16(MCP9808_REG_DEVICE_ID) != 0x0400)
|
||||
return false;
|
||||
|
||||
write16(MCP9808_REG_CONFIG, 0x0);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Reads the 16-bit temperature register and returns the Centigrade
|
||||
* temperature as a float.
|
||||
* @return Temperature in Centigrade.
|
||||
*/
|
||||
float Adafruit_MCP9808_Soft::readTempC() {
|
||||
float temp = NAN;
|
||||
uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
|
||||
|
||||
if (t != 0xFFFF) {
|
||||
temp = t & 0x0FFF;
|
||||
temp /= 16.0;
|
||||
if (t & 0x1000)
|
||||
temp -= 256;
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Reads the 16-bit temperature register and returns the Fahrenheit
|
||||
* temperature as a float.
|
||||
* @return Temperature in Fahrenheit.
|
||||
*/
|
||||
float Adafruit_MCP9808_Soft::readTempF() {
|
||||
float temp = NAN;
|
||||
uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
|
||||
|
||||
if (t != 0xFFFF) {
|
||||
temp = t & 0x0FFF;
|
||||
temp /= 16.0;
|
||||
if (t & 0x1000)
|
||||
temp -= 256;
|
||||
|
||||
temp = temp * 9.0 / 5.0 + 32;
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Set Sensor to Shutdown-State or wake up (Conf_Register BIT8)
|
||||
* @param sw true = shutdown / false = wakeup
|
||||
*/
|
||||
void Adafruit_MCP9808_Soft::shutdown_wake(boolean sw) {
|
||||
uint16_t conf_shutdown;
|
||||
uint16_t conf_register = read16(MCP9808_REG_CONFIG);
|
||||
if (sw == true) {
|
||||
conf_shutdown = conf_register | MCP9808_REG_CONFIG_SHUTDOWN;
|
||||
write16(MCP9808_REG_CONFIG, conf_shutdown);
|
||||
}
|
||||
if (sw == false) {
|
||||
conf_shutdown = conf_register & ~MCP9808_REG_CONFIG_SHUTDOWN;
|
||||
write16(MCP9808_REG_CONFIG, conf_shutdown);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Shutdown MCP9808
|
||||
*/
|
||||
void Adafruit_MCP9808_Soft::shutdown() { shutdown_wake(true); }
|
||||
|
||||
/*!
|
||||
* @brief Wake up MCP9808
|
||||
*/
|
||||
void Adafruit_MCP9808_Soft::wake() {
|
||||
shutdown_wake(false);
|
||||
delay(250);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get Resolution Value
|
||||
* @return Resolution value
|
||||
*/
|
||||
uint8_t Adafruit_MCP9808_Soft::getResolution() {
|
||||
return read8(MCP9808_REG_RESOLUTION);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Set Resolution Value
|
||||
* @param value
|
||||
*/
|
||||
void Adafruit_MCP9808_Soft::setResolution(uint8_t value) {
|
||||
write8(MCP9808_REG_RESOLUTION, value & 0x03);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Low level 16 bit write procedures
|
||||
* @param reg
|
||||
* @param value
|
||||
*/
|
||||
void Adafruit_MCP9808_Soft::write16(uint8_t reg, uint16_t value) {
|
||||
_wire->beginTransmission(_i2caddr);
|
||||
_wire->write((uint8_t)reg);
|
||||
_wire->write(value >> 8);
|
||||
_wire->write(value & 0xFF);
|
||||
_wire->endTransmission();
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Low level 16 bit read procedure
|
||||
* @param reg
|
||||
* @return value
|
||||
*/
|
||||
uint16_t Adafruit_MCP9808_Soft::read16(uint8_t reg) {
|
||||
uint16_t val = 0xFFFF;
|
||||
uint8_t state;
|
||||
|
||||
_wire->beginTransmission(_i2caddr);
|
||||
_wire->write((uint8_t)reg);
|
||||
state = _wire->endTransmission();
|
||||
|
||||
if (state == 0) {
|
||||
_wire->requestFrom((uint8_t)_i2caddr, (uint8_t)2);
|
||||
val = _wire->read();
|
||||
val <<= 8;
|
||||
val |= _wire->read();
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Low level 8 bit write procedure
|
||||
* @param reg
|
||||
* @param value
|
||||
*/
|
||||
void Adafruit_MCP9808_Soft::write8(uint8_t reg, uint8_t value)
|
||||
{
|
||||
_wire -> beginTransmission(_i2caddr);
|
||||
_wire -> write((uint8_t)reg);
|
||||
_wire -> write(value);
|
||||
_wire -> endTransmission();
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Low level 8 bit read procedure
|
||||
* @param reg
|
||||
* @return value
|
||||
*/
|
||||
uint8_t Adafruit_MCP9808_Soft::read8(uint8_t reg)
|
||||
{
|
||||
uint8_t val = 0xFF;
|
||||
uint8_t state;
|
||||
|
||||
_wire -> beginTransmission(_i2caddr);
|
||||
_wire -> write((uint8_t)reg);
|
||||
state = _wire -> endTransmission();
|
||||
|
||||
if (state == 0)
|
||||
{
|
||||
_wire -> requestFrom((uint8_t)_i2caddr, (uint8_t)1);
|
||||
val = _wire -> read();
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*!
|
||||
* @file Adafruit_MCP9808.h
|
||||
*
|
||||
* I2C Driver for Microchip's MCP9808 I2C Temp sensor
|
||||
*
|
||||
* This is a library for the Adafruit MCP9808 breakout:
|
||||
* http://www.adafruit.com/products/1782
|
||||
*
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
*please support Adafruit and open-source hardware by purchasing products from
|
||||
* Adafruit!
|
||||
*
|
||||
*
|
||||
* BSD license (see license.txt)
|
||||
*/
|
||||
|
||||
#ifndef _ADAFRUIT_MCP9808_SOFT_H
|
||||
#define _ADAFRUIT_MCP9808_SOFT_H
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include <SoftwareWire.h>
|
||||
|
||||
#define MCP9808_I2CADDR_DEFAULT 0x18 ///< I2C address
|
||||
#define MCP9808_REG_CONFIG 0x01 ///< MCP9808 config register
|
||||
|
||||
#define MCP9808_REG_CONFIG_SHUTDOWN 0x0100 ///< shutdown config
|
||||
#define MCP9808_REG_CONFIG_CRITLOCKED 0x0080 ///< critical trip lock
|
||||
#define MCP9808_REG_CONFIG_WINLOCKED 0x0040 ///< alarm window lock
|
||||
#define MCP9808_REG_CONFIG_INTCLR 0x0020 ///< interrupt clear
|
||||
#define MCP9808_REG_CONFIG_ALERTSTAT 0x0010 ///< alert output status
|
||||
#define MCP9808_REG_CONFIG_ALERTCTRL 0x0008 ///< alert output control
|
||||
#define MCP9808_REG_CONFIG_ALERTSEL 0x0004 ///< alert output select
|
||||
#define MCP9808_REG_CONFIG_ALERTPOL 0x0002 ///< alert output polarity
|
||||
#define MCP9808_REG_CONFIG_ALERTMODE 0x0001 ///< alert output mode
|
||||
|
||||
#define MCP9808_REG_UPPER_TEMP 0x02 ///< upper alert boundary
|
||||
#define MCP9808_REG_LOWER_TEMP 0x03 ///< lower alert boundery
|
||||
#define MCP9808_REG_CRIT_TEMP 0x04 ///< critical temperature
|
||||
#define MCP9808_REG_AMBIENT_TEMP 0x05 ///< ambient temperature
|
||||
#define MCP9808_REG_MANUF_ID 0x06 ///< manufacture ID
|
||||
#define MCP9808_REG_DEVICE_ID 0x07 ///< device ID
|
||||
#define MCP9808_REG_RESOLUTION 0x08 ///< resolutin
|
||||
|
||||
/*!
|
||||
* @brief Class that stores state and functions for interacting with
|
||||
* MCP9808 Temp Sensor
|
||||
*/
|
||||
class Adafruit_MCP9808_Soft {
|
||||
public:
|
||||
Adafruit_MCP9808_Soft();
|
||||
bool begin(SoftwareWire *theWire);
|
||||
bool begin(uint8_t addr, SoftwareWire *theWire);
|
||||
|
||||
bool init();
|
||||
float readTempC();
|
||||
float readTempF();
|
||||
uint8_t getResolution(void);
|
||||
void setResolution(uint8_t value);
|
||||
|
||||
void shutdown_wake(boolean sw);
|
||||
void shutdown();
|
||||
void wake();
|
||||
|
||||
void write16(uint8_t reg, uint16_t val);
|
||||
uint16_t read16(uint8_t reg);
|
||||
|
||||
void write8(uint8_t reg, uint8_t val);
|
||||
uint8_t read8(uint8_t reg);
|
||||
|
||||
private:
|
||||
//TwoWire *_wire;
|
||||
SoftwareWire *_wire;
|
||||
uint8_t _i2caddr;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user