Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ou_OLED_TMP102 ou_mbed_tmp102
test_TMP102.cpp
00001 #include "test_TMP102.h" 00002 00003 test_TMP102::test_TMP102(PinName sda, PinName scl, char address) 00004 : i2c_p(new I2C(sda, scl)), i2c(*i2c_p),adr(address) 00005 { 00006 init(); 00007 } 00008 test_TMP102::test_TMP102(I2C &i2c_obj, char address) 00009 : i2c_p(NULL), i2c(i2c_obj), adr(address) 00010 { 00011 init(); 00012 } 00013 00014 test_TMP102::~test_TMP102() 00015 { 00016 if(NULL!=i2c_p) 00017 delete i2c_p; 00018 } 00019 00020 void test_TMP102::init(void) 00021 { 00022 } 00023 00024 float test_TMP102::read(void) 00025 { 00026 char reg[2]= {0,0}; 00027 char pointer = TMP102_Temp; 00028 i2c.write(adr, &pointer, 1); //Pointer to the temperature register 00029 i2c.read(adr, reg, 2); // read two bytes data 00030 00031 int16_t res; 00032 // Bit 0 of second byte will always be 0 in 12-bit readings and 1 in 13-bit 00033 if(reg[1]&0x01) // 13 bit mode 00034 { 00035 // combine bytes to create a signed int 00036 res = ((int8_t)reg[0] << 5) | ((uint8_t)reg[1] >> 3); 00037 // Temperature data can be + or -, if it should be negative, 00038 // convert 13 bit to 16 bit and use the 2s compliment. 00039 if (res > 0xFFF) res |= 0xE000; 00040 } else // 12 bit mode 00041 { 00042 res = ((int8_t)reg[0] << 4) | ((uint8_t)reg[1] >> 4); 00043 if (res > 0x7FF) res |= 0xF000; 00044 } 00045 00046 float temp = (float)(res * 0.0625); 00047 return temp; 00048 } 00049 00050 float test_TMP102::readTempC(void) 00051 { 00052 return read(); 00053 } 00054 00055 float test_TMP102::readTempF(void) 00056 { 00057 return (float)((double)readTempC()*9.0/5.0 + 32.0); 00058 } 00059 00060 void test_TMP102::setConversionRate(char rate) 00061 { 00062 char reg[2] = {0, 0}; 00063 char pointer = TMP102_Conf; 00064 rate = rate & 0x03; // make sure rate is not set higher than 3. 00065 00066 i2c.write(adr, &pointer, 1); //Pointer to the conf register 00067 i2c.read(adr, reg, 2); // read two bytes data 00068 00069 reg[1] &= 0x3F; // Clear CR0/1 (bit 6 and 7 of second byte) 00070 reg[1] |= rate << 6; // Shift in new conversion rate 00071 00072 char data[3]; 00073 data[0] = pointer; 00074 data[1] = reg[0]; 00075 data[2] = reg[1]; 00076 i2c.write(adr, data, 3); 00077 } 00078 00079 void test_TMP102::setExtendedMode(bool mode) 00080 { 00081 char reg[2] = {0, 0}; 00082 char pointer = TMP102_Conf; 00083 00084 i2c.write(adr, &pointer, 1); //Pointer to the conf register 00085 i2c.read(adr, reg, 2); // read two bytes data 00086 00087 reg[1] &= 0xEF; // Clear EM (bit 4 of second byte) 00088 reg[1] |= mode<<4; // Shift in new exentended mode bit 00089 00090 char data[3]; 00091 data[0] = pointer; 00092 data[1] = reg[0]; 00093 data[2] = reg[1]; 00094 i2c.write(adr, data, 3); 00095 } 00096 00097 test_TMP102::operator float(void) 00098 { 00099 return (read()); 00100 } 00101 00102 void test_TMP102::sleep(void) 00103 { 00104 char reg; 00105 char pointer = TMP102_Conf; 00106 i2c.write(adr, &pointer, 1); //Pointer to the conf register 00107 i2c.read(adr, ®, 1); // read one byte data 00108 00109 reg |= 0x01; // set SD (bit 0 of first byte) 00110 00111 char data[2]; 00112 data[0] = pointer; 00113 data[1] = reg; 00114 i2c.write(adr, data, 2); 00115 } 00116 00117 void test_TMP102::wakeup(void) 00118 { 00119 char reg; 00120 char pointer = TMP102_Conf; 00121 i2c.write(adr, &pointer, 1); //Pointer to the conf register 00122 i2c.read(adr, ®, 1); // read one byte data 00123 00124 reg &= 0xFE; // Clear SD (bit 0 of first byte) 00125 00126 char data[2]; 00127 data[0] = pointer; 00128 data[1] = reg; 00129 i2c.write(adr, data, 2); 00130 } 00131 00132 void test_TMP102::oneShot(void) 00133 { 00134 char reg; 00135 char pointer = TMP102_Conf; 00136 i2c.write(adr, &pointer, 1); //Pointer to the conf register 00137 i2c.read(adr, ®, 1); // read one byte data 00138 00139 reg |= 0x80; // Set OS (bit 7 of first byte) 00140 00141 char data[2]; 00142 data[0] = pointer; 00143 data[1] = reg; 00144 i2c.write(adr, data, 2); 00145 00146 do { 00147 wait(0.026); 00148 i2c.read(adr, ®, 1); 00149 reg &= 0x80; 00150 } while (reg == 0); 00151 }
Generated on Sat Jul 16 2022 01:40:29 by
1.7.2