Tedd OKANO
/
TempSensor_LM75B
Revision 0:b1a5601983d4, committed 2010-01-23
- Comitter:
- okano
- Date:
- Sat Jan 23 13:45:00 2010 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2cBusDevice.h Sat Jan 23 13:45:00 2010 +0000 @@ -0,0 +1,58 @@ +/* + * I2C device base class + * + * A base class for all I2C devices. + * This manages the device address and transfers + * + * Copyright (c) 2010 Tedd OKANO + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 1.0 15-Jan-2010 a. 1st release + * revision 1.1 23-Jan-2010 a. The word "MBED_I2cBusDevice" is used instead of _I2cBusDevice_ to avoid symbol conflict + * b. copyright notice added + */ + +#ifndef MBED_I2cBusDevice +#define MBED_I2cBusDevice + +#include "mbed.h" + +class I2cBusDevice { +public: + + I2cBusDevice( I2C *i2c, char dev_address ) { + bus = i2c; + device = dev_address; + } + + ~I2cBusDevice() { + } + + int write( char *data, int length ) { + return ( bus->write( device, data, length) ); + } + + int read( char *data, int length ) { + return ( bus->read( device, data, length) ); + } + + int read( char reg_ptr, char *data, int length ) { + if ( bus->write( device, ®_ptr, 1 ) ) + return ( 1 ); + + if ( bus->read( device, data, length ) ) + return ( 1 ); + + return ( 0 ); + } + +protected: + I2C *bus; + char device; + +private: + static char i2c_error; +} +; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TempSensor_LM75B.h Sat Jan 23 13:45:00 2010 +0000 @@ -0,0 +1,94 @@ +/* + * I2C digital temperature sensor "LM75B" library + * + * LM75B is an I2C based digital temperature sensor + * http://www.nxp.com/pip/LM75B_2.html + * + * This is a library to operate this chip easy. + * + * Copyright (c) 2010 Tedd OKANO + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 1.0 16-Jan-2010 a. 1st release + * revision 1.1 23-Jan-2010 a. class name has been changed from LM75B to TempSensor_LM75B + * b. copyright notice added + */ + +#ifndef MBED_TempSensor_LM75B +#define MBED_TempSensor_LM75B + + +#include "mbed.h" +#include "I2cBusDevice.h" + + +// LM75B IIC address +const char LM75B_base_addr = 0x90; + +// LM75B registers +const char Conf = 0x01; +const char Temp = 0x00; +const char Tos = 0x03; +const char Thyst = 0x02; + + +class TempSensor_LM75B : I2cBusDevice { +public: + + TempSensor_LM75B( I2C *i2c, char dev_address = LM75B_base_addr, char vConf = 0x00, short vTos = 0x5000, short vThyst = 0x4B00 ) : I2cBusDevice( i2c, dev_address ) { + char data[ 3 ]; + + data[ 0 ] = Conf; + data[ 1 ] = vConf; + + if ( write( data, 2 ) ) + ; + + data[ 0 ] = Tos; + data[ 1 ] = (char)(vTos >> 8); + data[ 2 ] = (char)vTos; + + if ( write( data, 3 ) ) + ; + + data[ 0 ] = Thyst; + data[ 1 ] = (char)(vThyst >> 8); + data[ 2 ] = (char)vThyst; + + if ( write( data, 3 ) ) + ; + } + + ~TempSensor_LM75B() { + } + + int temp_short( void ) { + char data[ 2 ]; + + if ( read( Temp, data, 2 ) ) + return ( 1e6 ); + + return ( (((short)data[ 0 ]) << 8 | data[ 1 ]) >> 5 ); + } + + float temp( void ) { + return ( (float)(temp_short()) / 8.0 ); + } + + operator float( void ) { + return( temp() ); + } + +#if 0 + operator short( void ) { + return( temp_short() ); + } +#endif + + + +private: +} +; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TempSensor_LM75B_test.cpp Sat Jan 23 13:45:00 2010 +0000 @@ -0,0 +1,53 @@ +/* + * I2C digital temperature sensor "LM75B" library test app + * + * LM75B is an I2C based digital temperature sensor + * http://www.nxp.com/pip/LM75B_2.html + * + * Expecting to use the pins 9 and 10 for I2C bus + * these pins should be pulled-up properly. + * + * The temperature read out will be shown on terminal on the PC screen. + * + * In this demo code, two LM75B devices can be driven. + * These two devices should have different I2C address setting + * using its address pins (LM75B's A0 to A2 (pins 5 to 7)). + * One LM75B should have all those pins tied to GND. + * And another should have the pin A0(pin7) pulled-up. + * + * From the software, those devices can be accessed by I2C addresses + * "0x90" and "0x92". + * It will not be as "0x90" and "0x91" because the address has + * 7 bit only and stuffed to left. So the "A0" setting become 0xX2. + * The LSB does not care because it will be set by I2C libraly when + * it transfer the data for read and write. + * + * This code is new version that can handle the LM75B are the objects. + * + * Copyright (c) 2010 Tedd OKANO + * Released under the MIT License: http://mbed.org/license/mit + * + * revision 1.0 16-Jan-2010 a. 1st release + * revision 1.1 23-Jan-2010 a. class name has been changed from LM75B to TempSensor_LM75B + * b. copyright notice added + */ + +#include "mbed.h" +#include "TempSensor_LM75B.h" + +Serial pc(USBTX, USBRX); // tx, rx + +I2C i2c( p9, p10 ); // sda, scl + +TempSensor_LM75B thermo_sensor_0( &i2c ); // sensor_0 using with default I2C address "0x90". +TempSensor_LM75B thermo_sensor_1( &i2c, 0x92 ); // sensor_1 gaved I2C address since that address pin A0=HIGH. + +int main() { + int i = 0; + + while (1) { + pc.printf( " (%d) sensor_0= %4.1f, sensor_1= %4.1f(degree-C)\n", i++, (float)thermo_sensor_0, (float)thermo_sensor_1 ); + wait( 1 ); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Jan 23 13:45:00 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0