Tedd OKANO
/
LM75B_test
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 * NXP LM75B temperature sensor test 00003 * www.nxp.com/pip/LM75B_2.html 00004 * 00005 * Expecting to use the pins 9 and 10 for I2C bus 00006 * these pins should be pulled-up properly. 00007 * 00008 * The temperature read out will be shown on terminal on the PC screen. 00009 * 00010 * In this demo code, two LM75B devices can be driven. 00011 * These two devices should have different I2C address setting 00012 * using its address pins (LM75B's A0 to A2 (pins 5 to 7)). 00013 * One LM75B should have all those pins tied to GND. 00014 * And another should have the pin A0(pin7) pulled-up. 00015 * 00016 * From the software, those devices can be accessed by I2C addresses 00017 * "0x90" and "0x92". 00018 * It will not be as "0x90" and "0x91" because the address has 00019 * 7 bit only and stuffed to left. So the "A0" setting become 0xX2. 00020 * The LSB does not care because it will be set by I2C libraly when 00021 * it transfer the data for read and write. 00022 */ 00023 00024 #include "mbed.h" 00025 //#include "SDFileSystem.h" 00026 00027 // LM75B IIC address 00028 #define LM75B_ADDR 0x90 00029 00030 // LM75B registers 00031 #define Conf 0x01 00032 #define Temp 0x00 00033 #define Tos 0x03 00034 #define Thyst 0x02 00035 00036 00037 DigitalOut led[] = { LED4, LED3, LED2, LED1 }; 00038 Serial pc(USBTX, USBRX); // tx, rx 00039 00040 I2C i2c( p9, p10 ); // sda, scl 00041 00042 00043 void iic_write( char addr, char reg, char data ); 00044 char iic_read( char addr, char reg ); 00045 short iic_read_short( char addr, char reg ); 00046 void iic_error( void ); 00047 00048 void init_temp_sensor( char dev_num ); 00049 float get_temp( char dev_num ); 00050 00051 int main() { 00052 int i; 00053 00054 init_temp_sensor( 0 ); 00055 init_temp_sensor( 1 ); 00056 00057 while(1) { 00058 pc.printf( " (%d) temp[0]=%6.3f, temp[1]=%6.3f(degree-C)\n", i++, get_temp( 0 ), get_temp( 1 ) ); 00059 wait( 1 ); 00060 } 00061 } 00062 00063 void init_temp_sensor( char dev_num ) 00064 { 00065 dev_num <<= 1; 00066 00067 iic_write( LM75B_ADDR | dev_num, Conf, 0x00 ); // configuration 00068 pc.printf( "sensor[%d] : Conf register read out = 0x%02X\n", dev_num, iic_read( LM75B_ADDR | dev_num, Conf ) ); 00069 pc.printf( "sensor[%d] : Tos register read out = 0x%04X\n", dev_num, iic_read_short( LM75B_ADDR | dev_num, Tos ) ); 00070 pc.printf( "sensor[%d] : Thyst register read out = 0x%04X\n", dev_num, iic_read_short( LM75B_ADDR | dev_num, Thyst ) ); 00071 } 00072 00073 00074 float get_temp( char dev_num ) 00075 { 00076 dev_num <<= 1; 00077 00078 return ( (float)(iic_read_short( LM75B_ADDR | dev_num, Temp )) / 256.0 ); 00079 } 00080 00081 void iic_write( char addr, char reg, char data ) 00082 { 00083 char cmd[2]; 00084 00085 cmd[ 0 ] = reg; 00086 cmd[ 1 ] = data; 00087 00088 if ( i2c.write( addr, cmd, 2) ) 00089 iic_error(); 00090 } 00091 00092 char iic_read( char addr, char reg ) 00093 { 00094 char cmd; 00095 int nack; 00096 00097 cmd = reg; 00098 00099 nack = i2c.write( addr, &cmd, 1); // Send command string 00100 nack |= i2c.read( addr, &cmd, 1); // Send command string 00101 00102 if ( nack ) 00103 iic_error(); 00104 00105 return ( cmd ); 00106 } 00107 00108 00109 short iic_read_short( char addr, char reg ) 00110 { 00111 char cmd[ 2 ]; 00112 int nack; 00113 00114 cmd[ 0 ] = reg; 00115 00116 nack = i2c.write( addr, cmd, 1); // Send command string 00117 nack |= i2c.read( addr, cmd, 2); // Send command string 00118 00119 if ( nack ) 00120 iic_error(); 00121 00122 return ( ((short)cmd[ 0 ]) << 8 | cmd[ 1 ] ); 00123 } 00124 00125 00126 void iic_error( void ) 00127 { 00128 pc.printf( "I2C error\n" ); 00129 00130 for ( int i = 0; i < 4; i++ ) 00131 led[ i ] = i & 0x01; 00132 00133 wait ( 0.2 ); 00134 00135 for ( int i = 0; i < 4; i++ ) 00136 led[ i ] = !(i & 0x01); 00137 00138 wait ( 0.2 ); 00139 } 00140
Generated on Sun Jul 24 2022 19:50:11 by 1.7.2