Statistics
| Branch: | Tag: | Revision:

mongoose / examples / CC3200 / tmp006.c @ eaef5bd1

History | View | Annotate | Download (1.22 KB)

1
/*
2
 * Copyright (c) 2014-2016 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "tmp006.h"
7

    
8
#include "mongoose.h"
9

    
10
#include "i2c_if.h"
11

    
12
#define TMP006_REG_SENSOR_VOLTAGE 0x00
13
#define TMP006_REG_DIE_TEMP 0x01
14
#define TMP006_REG_CONFIG 0x02
15

    
16
bool tmp006_init(uint8_t addr, enum tmp006_conversion_rate conv_rate,
17
                 bool drdy_en) {
18
  unsigned char val[3] = {TMP006_REG_CONFIG, 0x80, 0};
19
  /* Reset first */
20
  if (I2C_IF_Write(addr, val, 3, 1) != 0) return false;
21
  val[1] = 0x70 | (conv_rate << 1) | drdy_en;
22
  return (I2C_IF_Write(addr, val, 3, 1) == 0);
23
}
24

    
25
double tmp006_read_sensor_voltage(uint8_t addr) {
26
  unsigned char reg = TMP006_REG_SENSOR_VOLTAGE;
27
  unsigned char val[2] = {0, 0};
28
  int status = I2C_IF_ReadFrom(addr, &reg, 1, val, 2);
29
  if (status < 0) return -1000;
30
  int voltage = (val[0] << 8) | val[1];
31
  if (val[0] & 0x80) voltage = -((1 << 16) - voltage);
32
  return voltage * 0.00015625;
33
}
34

    
35
double tmp006_read_die_temp(uint8_t addr) {
36
  unsigned char reg = TMP006_REG_DIE_TEMP;
37
  unsigned char val[2] = {0, 0};
38
  int status = I2C_IF_ReadFrom(addr, &reg, 1, val, 2);
39
  if (status < 0) return -1000;
40
  int temp = (val[0] << 6) | (val[1] >> 2);
41
  if (val[0] & 0x80) temp = -((1 << 14) - temp);
42
  return temp / 32.0;
43
}