Rob Toulson / Mbed 2 deprecated PE_07-07_I2CTempSensor

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Program Example 7.7: Mbed communicates with TMP102 temperature sensor, and scales and displays readings to screen.
00002                                                                              */
00003 #include "mbed.h"
00004 I2C tempsensor(p9, p10);      //sda, sc1
00005 Serial pc(USBTX, USBRX);      //tx, rx
00006 const int addr = 0x90;
00007 char config_t[3];
00008 char temp_read[2];
00009 float temp;
00010 
00011 int main() {
00012   config_t[0] = 0x01;                    //set pointer reg to 'config register'
00013   config_t[1] = 0x60;                    // config data byte1
00014   config_t[2] = 0xA0;                    // config data byte2
00015   tempsensor.write(addr, config_t, 3);
00016   config_t[0] = 0x00;                    //set pointer reg to 'data register'
00017   tempsensor.write(addr, config_t, 1);   //send to pointer 'read temp'
00018   while(1) {
00019     wait(1);
00020     tempsensor.read(addr, temp_read, 2);       //read the two-byte temp data
00021     temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4);  //convert data
00022     pc.printf("Temp = %.2f degC\n\r", temp);
00023   }
00024 }