Craig Evans
/
2645_I2C_TMP102
Example code how to get temperature from the TMP102 temperature sensor
Revision 1:dd5fb735acf1, committed 2016-02-05
- Comitter:
- eencae
- Date:
- Fri Feb 05 17:25:03 2016 +0000
- Parent:
- 0:21a200b880d7
- Commit message:
- Initial commit.
Changed in this revision
TMP102.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 21a200b880d7 -r dd5fb735acf1 TMP102.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TMP102.lib Fri Feb 05 17:25:03 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/eencae/code/TMP102/#8818842a3573
diff -r 21a200b880d7 -r dd5fb735acf1 main.cpp --- a/main.cpp Fri Feb 05 16:23:38 2016 +0000 +++ b/main.cpp Fri Feb 05 17:25:03 2016 +0000 @@ -1,30 +1,21 @@ /* -2645_I2C_TMP102 +2645_I2C_TMP102_Library Sample code from ELEC2645 Week 17 Lab -Demonstrates how to read the temperature from an I2C sensor +Demonstrates how to re-factor the TMP102 code into a library (c) Craig A. Evans, University of Leeds, Feb 2016 */ #include "mbed.h" - -// addresses for ADD0 connected to GND -#define TMP102_ADD 0x48 -#define TMP102_R_ADD 0x91 -#define TMP102_W_ADD 0x90 +// include the library header, ensure the library has been imported into the project +#include "TMP102.h" -// register addresses -#define TEMP_REG 0x00 -#define CONFIG_REG 0x01 -#define THIGH_REG 0x02 -#define TLOW_REG 0x03 - -// I2C peripheral for temperature sensor -I2C tmp102(I2C_SDA,I2C_SCL); +// Create TMP102 object +TMP102 tmp102(I2C_SDA,I2C_SCL); // UART connection for PC Serial pc(USBTX,USBRX); @@ -42,22 +33,19 @@ void init_serial(); // set-up the on-board LEDs and switches void init_K64F(); -// initialisation function for temperature sensor -void init_TMP102(); -// function to read temperature from sensor (in degrees C) -float get_temperature(); int main() { - // initialise the board, serial port and sensor + // initialise the board and serial port init_K64F(); init_serial(); - init_TMP102(); + // call the sensor init method using dot syntax + tmp102.init(); while (1) { // read temperature and print over serial port - float T = get_temperature(); + float T = tmp102.get_temperature(); pc.printf("T = %f K\n",T); // small delay - 1s to match the update rate of the sensor (1 Hz) wait(1.0); @@ -84,67 +72,3 @@ sw3.mode(PullNone); } - -void error() -{ - while(1) { // if error, hang while flashing error message - r_led = 0; - wait(0.2); - r_led = 1; - wait(0.2); - } -} - -void init_TMP102() -{ - tmp102.frequency(400000); // set bus speed to 400 kHz - - int ack; // used to store acknowledgement bit - char config_data[2]; // array for data - char reg = CONFIG_REG; // register address - - //////// Read current status of configuration register /////// - - ack = tmp102.write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address - if (ack) - error(); // if we don't receive acknowledgement, flash error message - - ack = tmp102.read(TMP102_R_ADD,config_data,2); // read default 2 bytes from configuration register and store in array - if (ack) - error(); // if we don't receive acknowledgement, flash error message - - ///////// Configure the register ////////// - - // set conversion rate to 1 Hz - CR1 and CR2 are in the second data byte - config_data[1] |= (1 << 6); // set bit 6 - config_data[1] &= ~(1 << 7); // clear bit 7 - - //////// Send the configured register value to the slave config register //////////// - - // create data packet - char data_packet[3] = {reg,config_data[0],config_data[1]}; - - ack = tmp102.write(TMP102_W_ADD,data_packet,3); // send the data packet to the slave write address - if (ack) - error(); // if we don't receive acknowledgement, flash error message - -} - -float get_temperature() -{ - int ack; // used to store acknowledgement bit - char data[2]; // array for data - char reg = TEMP_REG; // temperature register address - - ack = tmp102.write(TMP102_W_ADD,®,1); // send temperature register address - if (ack) - error(); // if we don't receive acknowledgement, flash error message - - ack = tmp102.read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array - if (ack) - error(); // if we don't receive acknowledgement, flash error message - - int temperature = (data[0] << 4) | (data[1] >> 4); - - return temperature*0.0625; -} \ No newline at end of file