summaryrefslogtreecommitdiffstats
path: root/humidity_sensors/app/sht20.c
blob: 5d6acb704278673c7732708a3302fa337f93bc55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "project.h"

#define SHT20_ADDRESS 0x40

#define TRIGGER_TEMP_MEASURE_HOLD             0xE3
#define TRIGGER_HUMD_MEASURE_HOLD             0xE5
#define TRIGGER_TEMP_MEASURE_NOHOLD           0xF3
#define TRIGGER_HUMD_MEASURE_NOHOLD           0xF5
#define WRITE_USER_REG                        0xE6
#define READ_USER_REG                         0xE7
#define SOFT_RESET                            0xFE


#define MAX_RESOLUTION            ((u8) 0x00)
#define BATTERY_LOW               ((u8) 0x40)
#define RESERVED_MASK             ((u8) 0x38)
#define ENABLE_HEATER             ((u8) 0x04)
#define DISABLE_OTP_RELOAD        ((u8) 0x02)


void
sht20_reset (void)
{

  i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE);
  i2cb_send_data (SOFT_RESET);
  i2cb_stop();
  delay_ms (100);
  printf ("$SNTHR\n");
}


char *
sht20_temp_s (void)
{
  u8 h, l, c, s;
  s32 t, tf;
  float f;
  static char ret[8];

  i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE);
  i2cb_send_data (TRIGGER_TEMP_MEASURE_NOHOLD);
  delay_ms (100);
  i2cb_start_transaction (SHT20_ADDRESS, I2C_READ);

  h = i2cb_read (0);
  l = i2cb_read (0);
  c = i2cb_read (1);

  i2cb_stop();

  t = h;
  t <<= 8;
  t |= l & 0xfc;

  f = t;
  f /= 65536.;
  f *= 175.72;
  f -= 46.85;

  f *= 100.;

  t = (s32) (f + .5);

  ret[0] = '-';

  if (t >= 0)
    s = 1;
  else {
    s = 0;
    t = -t;
  }

  tf = t % 100;
  t = t / 100;

  sprintf (&ret[1], "%ld.%02ld", t, tf);

  return &ret[s];

}

char *
sht20_humid_s (void)
{
  u8 h, l, c;
  u32 t, tf;
  float f;
  static char ret[8];

  i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE);
  i2cb_send_data (TRIGGER_HUMD_MEASURE_NOHOLD);
  delay_ms (100);
  i2cb_start_transaction (SHT20_ADDRESS, I2C_READ);

  h = i2cb_read (0);
  l = i2cb_read (0);
  c = i2cb_read (1);

  i2cb_stop();

  t = h;
  t <<= 8;
  t |= l & 0xfc;

  f = t;
  f /= 65536.;
  f *= 125.;
  f -= 6.;

  if (f < 0)
    f = 0;

  if (f > 100)
    f = 100;

  f *= 100.;

  t = (u32) (f + .5);
  tf = t % 100;
  t = t / 100;

  sprintf (ret, "%ld.%02ld", t, tf);


  return ret;

}

void sht20_setup (void)
{
  uint8_t u;

  i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE);
  i2cb_send_data (READ_USER_REG);
  i2cb_start_transaction (SHT20_ADDRESS, I2C_READ);
  u = i2cb_read (0);
  i2cb_stop();


  u &= RESERVED_MASK;
  u |= MAX_RESOLUTION | DISABLE_OTP_RELOAD;

  // FWIW - it appears to be impossible to turn off the heater
  // with either a reset or write, perhaps a fake chip?
  u &= ~ ENABLE_HEATER;

  i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE);
  i2cb_send_data (WRITE_USER_REG);
  i2cb_send_data (u);
  i2cb_stop();
}