TMP102 temperature sensor test using LPC824.

Dependencies:   test_TMP102 mbed

Committer:
yasubumi
Date:
Sun Nov 19 09:59:21 2017 +0000
Revision:
1:3241f6a5a51e
Parent:
0:26352a9cd499
Child:
2:40c813b1d6a3
TMP102 temperature sensor test for LPC824 without using existing TMP102 module. Basic test only to read temperature.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yasubumi 0:26352a9cd499 1 #include "mbed.h"
yasubumi 1:3241f6a5a51e 2
yasubumi 1:3241f6a5a51e 3 // TMP102 I2C slave address(ADD0 connected to GND)
yasubumi 1:3241f6a5a51e 4 #define ADDR_TMP102 0x90
yasubumi 1:3241f6a5a51e 5
yasubumi 1:3241f6a5a51e 6 // TMP102 registers
yasubumi 1:3241f6a5a51e 7 #define TMP102_Temp 0x00
yasubumi 1:3241f6a5a51e 8 #define TMP102_Conf 0x01
yasubumi 1:3241f6a5a51e 9 #define TMP102_Tlow 0x02
yasubumi 1:3241f6a5a51e 10 #define TMP102_Thigh 0x03
yasubumi 0:26352a9cd499 11
yasubumi 1:3241f6a5a51e 12 // Init I2C (SDA: dp4, SCL: dp5)
yasubumi 1:3241f6a5a51e 13 I2C i2c(dp4, dp5);
yasubumi 1:3241f6a5a51e 14
yasubumi 1:3241f6a5a51e 15 int main() {
yasubumi 1:3241f6a5a51e 16 while(1) {
yasubumi 1:3241f6a5a51e 17 i2c.write(ADDR_TMP102, 0x00, 1); //Pointer to the temperature register
yasubumi 0:26352a9cd499 18
yasubumi 1:3241f6a5a51e 19 char reg[2]={0,0};
yasubumi 1:3241f6a5a51e 20 i2c.read(ADDR_TMP102, reg, 2); // read two bytes data
yasubumi 1:3241f6a5a51e 21
yasubumi 1:3241f6a5a51e 22 // calculate temperature
yasubumi 1:3241f6a5a51e 23 int16_t res = ((int8_t)reg[0] << 4) | ((uint8_t)reg[1] >> 4);
yasubumi 1:3241f6a5a51e 24 float temp = (float) ((float)res * 0.0625);
yasubumi 1:3241f6a5a51e 25
yasubumi 1:3241f6a5a51e 26 printf("Temp: %f\n\r", temp);
yasubumi 0:26352a9cd499 27 wait(1.0);
yasubumi 0:26352a9cd499 28 }
yasubumi 0:26352a9cd499 29 }