Hello code for "Switch Science mbed LPC824"

Dependencies:   mbed

Fork of SwitchSciencembedLPC824_test by Mako SHIMURA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TempSensor_LM75B.h Source File

TempSensor_LM75B.h

00001 /*
00002  *  I2C digital temperature sensor "LM75B" library
00003  *
00004  *  LM75B is an I2C based digital temperature sensor
00005  *  http://www.nxp.com/pip/LM75B_2.html
00006  *
00007  *  This is a library to operate this chip easy.
00008  *
00009  *  Copyright (c) 2010 Tedd OKANO
00010  *  Released under the MIT License: http://mbed.org/license/mit
00011  *
00012  *  revision 1.0  16-Jan-2010   a. 1st release
00013  *  revision 1.1  23-Jan-2010   a. class name has been changed from LM75B to TempSensor_LM75B
00014  *                              b. copyright notice added
00015  */
00016 
00017 #ifndef        MBED_TempSensor_LM75B
00018 #define        MBED_TempSensor_LM75B
00019 
00020 
00021 #include    "mbed.h"
00022 #include    "I2cBusDevice.h"
00023 
00024 
00025 //  LM75B IIC address
00026 const char    LM75B_base_addr = 0x90;
00027 
00028 //  LM75B registers
00029 const char    Conf            = 0x01;
00030 const char    Temp            = 0x00;
00031 const char    Tos             = 0x03;
00032 const char    Thyst           = 0x02;
00033 
00034 
00035 class TempSensor_LM75B : I2cBusDevice {
00036 public:
00037 
00038     TempSensor_LM75B( I2C *i2c, char dev_address = LM75B_base_addr, char vConf = 0x00, short vTos  = 0x5000, short vThyst = 0x4B00 ) : I2cBusDevice( i2c, dev_address ) {
00039         char    data[ 3 ];
00040 
00041         data[ 0 ]    = Conf;
00042         data[ 1 ]    = vConf;
00043 
00044         if ( write( data, 2 ) )
00045             ;
00046 
00047         data[ 0 ]    = Tos;
00048         data[ 1 ]    = (char)(vTos >> 8);
00049         data[ 2 ]    = (char)vTos;
00050 
00051         if ( write( data, 3 ) )
00052             ;
00053 
00054         data[ 0 ]    = Thyst;
00055         data[ 1 ]    = (char)(vThyst >> 8);
00056         data[ 2 ]    = (char)vThyst;
00057 
00058         if ( write( data, 3 ) )
00059             ;
00060     }
00061 
00062     ~TempSensor_LM75B() {
00063     }
00064 
00065     int temp_short( void ) {
00066         char    data[ 2 ];
00067 
00068         if ( read( Temp, data, 2 ) )
00069             return ( 1e6 );
00070 
00071         return ( (((short)data[ 0 ]) << 8 | data[ 1 ]) >> 5 );
00072     }
00073 
00074     float temp( void ) {
00075         return ( (float)(temp_short()) / 8.0 );
00076     }
00077 
00078     operator float( void ) {
00079         return( temp() );
00080     }
00081 
00082 #if 0
00083     operator short( void ) {
00084         return( temp_short() );
00085     }
00086 #endif
00087 
00088 
00089 
00090 private:
00091 }
00092 ;
00093 
00094 #endif