-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHT15.c
205 lines (186 loc) · 4.62 KB
/
SHT15.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <xc.h>
#include "SHT15.h"
/* ==== DECLARACIONES ==== */
//#define _XTAL_FREQ 8000000 //Velocidad del OSC Externo/Interno
#define _XTAL_FREQ 32000000 //Velocidad del OSC Externo/Interno
/* Función para armar dato de 16bits */
unsigned int make16(unsigned char MSB, unsigned char LSB)
{
/* H: MS_Byte; L: LS_Byte */
return ((MSB<<8)|LSB);
}
/* Envia el Comando específico de START para el SHT15 */
void Start_SHT15(void)
{
/*
* START SEQUENCE FOR SHT15 SENSOR
*
* ------- -------
* SCK _____| |___| |_______
*
* ------- -------
* SDA _| |___________| |___
*
*
*/
WRITE_SDA(); //SDA (RC4) Salida
SDA = 1;
__delay_ms(1);
SCK = 1;
__delay_ms(1);
SDA = 0;
__delay_ms(1);
SCK = 0;
__delay_ms(1);
SCK = 1;
__delay_ms(1);
SDA = 1;
__delay_ms(1);
SCK = 0;
__delay_ms(1);
//SDA = 0;
}
/* Configura los pines SDA y SCK */
void setSHT15(void)
{
SCK_DIG(); //SCK Analógico OFF
SDA_DIG(); //SDA Analógico OFF
WRITE_SCK(); //SCK Output
WRITE_SDA(); //SDA Output
}
/* Lee Humedad[1]/Temperatura[0] del SHT15 */
double Read_SHT15(unsigned char Opcion)
{
unsigned int T=0;
signed int RH=0;
double RH_Lineal=0,RH_Real=0,Temperatura=0;
/* ==== Leer Temperatura ==== */
Start_SHT15();
SendByte_SHT15(SHT15_TEMP);
T = Read2Bytes_SHT15(); //Lee 2 Bytes
SkipCRC();
Temperatura = D1 + (T * D2);
if(Opcion == 1) /* === Humedad === */
{
/* ==== Leer Humedad ==== */
Start_SHT15();
SendByte_SHT15(SHT15_HUMD);
RH = Read2Bytes_SHT15();
SkipCRC();
RH_Lineal = C1 + (C2 * RH) + (C3 * RH * RH);
//Temperatura: 20°C - 30°C
if(20 < Temperatura && Temperatura < 30)
{
return RH_Lineal;
} //Temperatura dentro de Rango de Operación -40°C - 123.8°C
else if (-40 < Temperatura && Temperatura < 123.8)
{
/* ==== Compensación de la Señal de Humedad por Temperatura ==== */
/* Para temperaturas significativamente diferentes a 25°C (77°F) */
RH_Real = (Temperatura - 25)*(T1 + T2*RH) + RH_Lineal;
return RH_Real;
} //Temperatura Fuera de Rango, por lo tanto RH será errónea.
else
{
return 0;
}
}
else if(Opcion == 0) /* === Temperatura === */
{ /* === Dentro del Rango? === */
if(-40 < Temperatura && Temperatura < 123.8)
return Temperatura;
else
return 0; //No.
}
else /* === Opción != 0 && Opción != 1 === */
{
return 0;
}
}
/* Envia Byte al SHT15 */
void SendByte_SHT15(unsigned char Registro)
{
unsigned char i;
WRITE_SDA();
for(i = 0 ; i < 8 ; i++)
{
SCK = 0;
__delay_ms(1);
SDA = (Registro & 0x80)>>7;
__delay_ms(1);
SCK = 1;
__delay_ms(1);
Registro = Registro<<1;
}
/* ===== Wait for SHT15 to acknowledge ===== */
SCK = 0;
__delay_ms(1);
READ_SDA();
while (SDA == 1); //Wait for SHT to pull line low
SCK = 1;
__delay_ms(1);
SCK = 0; //Falling edge of 9th clock
__delay_ms(1);
/* ===== Measurement 80mS for 12bit ===== */
while(SDA==1); //Wait for SHT to finish measurement (SDA will be pulled low)
}
/* Lee 2 Bytes del SHT15 */
unsigned int Read2Bytes_SHT15(void)
{
//8 bits
unsigned char Buffer_SHT[Buff_Size];
unsigned char i,j;
//16 bits
unsigned int in_byte;
for(j=0; j<2;j++)
{
SCK = 0;
READ_SDA();
for(i = 0; i < 8; i++)
{
SCK = 0;
__delay_ms(1);
SCK = 1;
__delay_ms(1);
in_byte = in_byte << 1;
if(SDA == 1) in_byte = in_byte|0x01;
}
//Send acknowledge to SHT7x
SCK = 0;
WRITE_SDA();
SDA = 0;
SCK = 1;
__delay_ms(1);
SCK = 0; //Falling edge of 9th clock
__delay_ms(1);
Buffer_SHT[j] = in_byte;
}
return make16(Buffer_SHT[0],Buffer_SHT[1]);
}
/* Skip CRC */
void SkipCRC(void)
{
/* === Lee el CRC, no lo usa === */
unsigned char i;
unsigned int crc;
SCK = 0;
READ_SDA();
for(i = 0; i < 8; i++)
{
SCK = 0;
__delay_ms(1);
SCK = 1;
__delay_ms(1);
crc = crc << 1;
if(SDA == 1) crc = crc|0x01;
}
//Send acknowledge to SHT7x
SCK = 0;
WRITE_SDA();
SDA = 0;
SCK = 1;
__delay_ms(1);
SCK = 0; //Falling edge of 9th clock
__delay_ms(1);
// return crc;
}