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.
TMP102.cpp
00001 // library implemtation file 00002 00003 // obviously need to include the header 00004 #include "TMP102.h" 00005 00006 // we now implement each of the methods listed in the header 00007 00008 // note the TMP102:: at the beginning of the method name 00009 TMP102::TMP102(PinName SDA, PinName SCL) 00010 { 00011 //New mbed API objects are created in the contructor using new 00012 i2c_ = new I2C(SDA,SCL); 00013 led_ = new DigitalOut(LED_BLUE); 00014 } 00015 00016 void TMP102::init() 00017 { 00018 i2c_->frequency(400000); // set bus speed to 400 kHz 00019 00020 int ack; // used to store acknowledgement bit 00021 char config_data[2]; // array for data 00022 char reg = CONFIG_REG; // register address 00023 00024 //////// Read current status of configuration register /////// 00025 00026 ack = i2c_->write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address 00027 if (ack) 00028 error(); // if we don't receive acknowledgement, flash error message 00029 00030 ack = i2c_->read(TMP102_R_ADD,config_data,2); // read default 2 bytes from configuration register and store in array 00031 if (ack) 00032 error(); // if we don't receive acknowledgement, flash error message 00033 00034 ///////// Configure the register ////////// 00035 00036 // set conversion rate to 1 Hz - CR1 and CR2 are in the second data byte 00037 config_data[1] |= (1 << 6); // set bit 6 00038 config_data[1] &= ~(1 << 7); // clear bit 7 00039 00040 //////// Send the configured register value to the slave config register //////////// 00041 00042 // create data packet 00043 char data_packet[3] = {reg,config_data[0],config_data[1]}; 00044 00045 ack = i2c_->write(TMP102_W_ADD,data_packet,3); // send the data packet to the slave write address 00046 if (ack) 00047 error(); // if we don't receive acknowledgement, flash error message 00048 00049 } 00050 00051 void TMP102::read_temperature() 00052 { 00053 int ack; // used to store acknowledgement bit 00054 char data[2]; // array for data 00055 char reg = TEMP_REG; // temperature register address 00056 00057 ack = i2c_->write(TMP102_W_ADD,®,1); // send temperature register address 00058 if (ack) 00059 error(); // if we don't receive acknowledgement, flash error message 00060 00061 ack = i2c_->read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array 00062 if (ack) 00063 error(); // if we don't receive acknowledgement, flash error message 00064 00065 int temperature = (data[0] << 4) | (data[1] >> 4); 00066 00067 temperature_ = temperature*0.0625F; 00068 } 00069 00070 // temperature accessor method 00071 float TMP102::get_temperature() 00072 { 00073 read_temperature(); 00074 return temperature_; 00075 } 00076 00077 void TMP102::error() 00078 { 00079 while(1) { // if error, hang while flashing error message 00080 led_->write(0); 00081 wait(0.2); 00082 led_->write(1); 00083 wait(0.2); 00084 00085 } 00086 }
Generated on Sat Aug 20 2022 09:30:41 by
1.7.2