Rob Toulson / Mbed 2 deprecated PE_07-07_I2CTempSensor

Dependencies:   mbed

Committer:
robt
Date:
Mon Oct 15 21:26:40 2012 +0000
Revision:
0:0c9628dc60f4
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robt 0:0c9628dc60f4 1 /*Program Example 7.7: Mbed communicates with TMP102 temperature sensor, and scales and displays readings to screen.
robt 0:0c9628dc60f4 2 */
robt 0:0c9628dc60f4 3 #include "mbed.h"
robt 0:0c9628dc60f4 4 I2C tempsensor(p9, p10); //sda, sc1
robt 0:0c9628dc60f4 5 Serial pc(USBTX, USBRX); //tx, rx
robt 0:0c9628dc60f4 6 const int addr = 0x90;
robt 0:0c9628dc60f4 7 char config_t[3];
robt 0:0c9628dc60f4 8 char temp_read[2];
robt 0:0c9628dc60f4 9 float temp;
robt 0:0c9628dc60f4 10
robt 0:0c9628dc60f4 11 int main() {
robt 0:0c9628dc60f4 12 config_t[0] = 0x01; //set pointer reg to 'config register'
robt 0:0c9628dc60f4 13 config_t[1] = 0x60; // config data byte1
robt 0:0c9628dc60f4 14 config_t[2] = 0xA0; // config data byte2
robt 0:0c9628dc60f4 15 tempsensor.write(addr, config_t, 3);
robt 0:0c9628dc60f4 16 config_t[0] = 0x00; //set pointer reg to 'data register'
robt 0:0c9628dc60f4 17 tempsensor.write(addr, config_t, 1); //send to pointer 'read temp'
robt 0:0c9628dc60f4 18 while(1) {
robt 0:0c9628dc60f4 19 wait(1);
robt 0:0c9628dc60f4 20 tempsensor.read(addr, temp_read, 2); //read the two-byte temp data
robt 0:0c9628dc60f4 21 temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4); //convert data
robt 0:0c9628dc60f4 22 pc.printf("Temp = %.2f degC\n\r", temp);
robt 0:0c9628dc60f4 23 }
robt 0:0c9628dc60f4 24 }