A simply library for the LM75B I2C temperature sensor (forked from "chris/LM75B").
Fork of LM75B by
Revision 0:1be38995591b, committed 2012-10-26
- Comitter:
- chris
- Date:
- Fri Oct 26 20:34:16 2012 +0000
- Child:
- 1:6a70c9303bbe
- Commit message:
- First commit
Changed in this revision
LM75B.cpp | Show annotated file Show diff for this revision Revisions of this file |
LM75B.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LM75B.cpp Fri Oct 26 20:34:16 2012 +0000 @@ -0,0 +1,49 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "LM75B.h" + +LM75B::LM75B(PinName sda, PinName scl) : i2c(sda, scl) +{ + char cmd[2]; + cmd[0] = LM75B_Conf; + cmd[1] = 0x0; + i2c.write( LM75B_ADDR, cmd, 2); +} + + + +LM75B::~LM75B() +{ + +} + +float LM75B::read() +{ + char cmd[2]; + cmd[0] = LM75B_Temp; + + i2c.write( LM75B_ADDR, cmd, 1); // Send command string + i2c.read( LM75B_ADDR, cmd, 2); // Send command string + return ( float((cmd[0]<<8)|cmd[1]) / 256.0 ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LM75B.h Fri Oct 26 20:34:16 2012 +0000 @@ -0,0 +1,64 @@ +/* Copyright (c) 2012 cstyles, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef LM75B_H +#define LM75B_H + +#include "mbed.h" + +// LM75B IIC address +#define LM75B_ADDR 0x90 + +// LM75B registers +#define LM75B_Conf 0x01 +#define LM75B_Temp 0x00 +#define LM75B_Tos 0x03 +#define LM75B_Thyst 0x02 + +//!Library for the LM75B temperature sensor. +/*! +The LM75B is an I2C digital temperature sensor, with a range of -55C to +125C and a 0.125C resolution. +*/ +class LM75B +{ +public: + //!Creates an instance of the class. + /*! + Connect module at I2C address addr using I2C port pins sda and scl. + LM75B + */ + LM75B(PinName sda, PinName scl); + + /*! + Destroys instance. + */ + ~LM75B(); + + //!Reads the current temperature. + /*! + Reads the temperature register of the LM75B and converts it to a useable value. + */ + float read(); + +private: + + I2C i2c; + +}; + +#endif \ No newline at end of file