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.
Dependencies: mbed
TMP102.cpp
00001 /* * Print String 00002 * 00003 * Prints a string of characters to the screen buffer, string is cut off after the 83rd pixel. 00004 * @param x - the column number (0 to 83) 00005 * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row 00006 * @author - David Leaming - 25574043 00007 * @ Date - December 2021 00008 * 00009 * Acknowledgements 00010 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016 00011 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries 00012 * 00013 */ 00014 00015 // library implemtation file 00016 00017 // obviously need to include the header 00018 #include "TMP102.h" 00019 00020 // we now implement each of the methods listed in the header 00021 00022 // note the TMP102:: at the beginning of the method name 00023 TMP102::TMP102(PinName sda, PinName scl) 00024 { 00025 // in the constructor, we create the mbed API objects using 'new' 00026 i2c_ = new I2C(sda,scl); 00027 led_ = new DigitalOut(LED_RED); 00028 } 00029 00030 void TMP102::init() 00031 { 00032 i2c_->frequency(400000); // set bus speed to 400 kHz 00033 00034 int ack; // used to store acknowledgement bit 00035 char config_data[2]; // array for data 00036 char reg = CONFIG_REG; // register address 00037 00038 //////// Read current status of configuration register /////// 00039 00040 ack = i2c_->write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address 00041 if (ack) 00042 error(); // if we don't receive acknowledgement, flash error message 00043 00044 ack = i2c_->read(TMP102_R_ADD,config_data,2); // read default 2 bytes from configuration register and store in array 00045 if (ack) 00046 error(); // if we don't receive acknowledgement, flash error message 00047 00048 ///////// Configure the register ////////// 00049 00050 // set conversion rate to 1 Hz - CR1 and CR2 are in the second data byte 00051 config_data[1] |= (1 << 6); // set bit 6 00052 config_data[1] &= ~(1 << 7); // clear bit 7 00053 00054 //////// Send the configured register value to the slave config register //////////// 00055 00056 // create data packet 00057 char data_packet[3] = {reg,config_data[0],config_data[1]}; 00058 00059 ack = i2c_->write(TMP102_W_ADD,data_packet,3); // send the data packet to the slave write address 00060 if (ack) 00061 error(); // if we don't receive acknowledgement, flash error message 00062 00063 } 00064 00065 void TMP102::read_temperature() 00066 { 00067 int ack; // used to store acknowledgement bit 00068 char data[2]; // array for data 00069 char reg = TEMP_REG; // temperature register address 00070 00071 ack = i2c_->write(TMP102_W_ADD,®,1); // send temperature register address 00072 if (ack) 00073 error(); // if we don't receive acknowledgement, flash error message 00074 00075 ack = i2c_->read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array 00076 if (ack) 00077 error(); // if we don't receive acknowledgement, flash error message 00078 00079 int temperature = (data[0] << 4) | (data[1] >> 4); 00080 00081 temperature_ = temperature*0.0625F; 00082 } 00083 00084 // temperature accessor method 00085 float TMP102::get_temperature() 00086 { 00087 read_temperature(); 00088 return temperature_; 00089 } 00090 00091 void TMP102::error() 00092 { 00093 while(1) { // if error, hang while flashing error message 00094 led_->write(0); 00095 wait(0.2); 00096 led_->write(1); 00097 wait(0.2); 00098 } 00099 }
Generated on Sun Jul 31 2022 21:19:10 by
