ATmega8 SHT71 Temperature Humidity and Dew Point Example
-
Upload
hieuhuech1 -
Category
Documents
-
view
5 -
download
1
description
Transcript of ATmega8 SHT71 Temperature Humidity and Dew Point Example
ATmega8 SHT71 Temperature Humidity and Dew point example
ATmega8 SHT71 Temperature Humidity and Dew point examplehttp://www.electronics-base.com/projects/complete-projects/152-atmega8-sht71-temperature-humidity-and-dew-point-example\Published on Monday, 19 March 2012 20:54|Written by Super User| Hits: 4918
In this article is provided 100% tested and working C code for AVR readout of SHT71 Temperature, Humidity and Dew point. Code itself is well commented and is easy to understand if combined withSHT71 Humidity and Temperature Sensor IC article.DATA pin is connected to Port C pin 0.SCK pin is connected to Port C pin 1.Pull-up resistor must be placed on DATA pin. Pull-up resistor of 10k to +5V is used in this example.Be careful that if you use Port C for other things lines that change its direction will change it. For example?1DDRC = 0b00000011; //
If this is the case just change only bits for selected pins for DATA and SCK.Complete project written in Codevision AVR C compiler written for ATmega8 can be downloaded from this link. Complete code is provided below this text and video showing demonstration of this code in action can be found at the bottom of this page. Data is read from serial port by com port terminal - development tool.?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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369/*****************************************************Complete example with more comments and further support can be found at www.Electronics-Base.com under complete projects sectionDate : 3/19/2012Author : www.Electronics-Base.comChip type : ATmega8Program type : ApplicationAVR Core Clock frequency: 8.000000 MHzMemory model : SmallExternal RAM size : 0Data Stack size : 256www.Electronics-Base.com*****************************************************/#include // Standard Input/Output functions#include //must be included to use delay functions#include //must be included to use itoa ftoa functions#include //must be included to use log10 functions#include
typedef union{ unsigned int i;float f;} value;enum {TEMP,HUMI};#define DATA_OUT PORTC.0#define DATA_IN PINC.0#define SCK PORTC.1 #define noACK 0#define ACK 1 //adr command r/w#define STATUS_REG_W 0x06 //000 0011 0#define STATUS_REG_R 0x07 //000 0011 1#define MEASURE_TEMP 0x03 //000 0001 1#define MEASURE_HUMI 0x05 //000 0010 1#define RESET 0x1e //000 1111 0// Declare your global variables hereunsigned int tempervalue[2]={0,0};/***********************************/char SHT_WriteByte(unsigned char bytte){ unsigned char i,error=0; DDRC = 0b00000011; // for (i=0x80;i>0;i/=2) //shift bit for masking{ if (i & bytte) DATA_OUT=1; //masking value with i , write to SENSI-BUSelse DATA_OUT=0; SCK=1; //clk for SENSI-BUSdelay_us(5); //pulswith approx. 5 us SCK=0; }DATA_OUT=1; //release dataline DDRC = 0b00000010; // DATA is OutputSCK=1; //clk #9 for ack delay_us(2);error=DATA_IN; //check ack (DATA will be pulled down by SHT11)delay_us(2);SCK=0; return error; //error=1 in case of no acknowledge}//----------------------------------------------------------------------------------// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" //----------------------------------------------------------------------------------char SHT_ReadByte(unsigned char ack){ unsigned char i,val=0;DDRC = 0b00000010; // DATA is Inputfor (i=0x80;i>0;i/=2) //shift bit for masking{ SCK=1; //clk for SENSI-BUS delay_us(2);if (DATA_IN) val=(val | i); //read bit delay_us(2);SCK=0;} DDRC = 0b00000011; // DATA is OutputDATA_OUT=!ack; //in case of "ack==1" pull down DATA-Line SCK=1; //clk #9 for ackdelay_us(5); //pulswith approx. 5 us SCK=0;DATA_OUT=1; //release DATA-line //ADD BY LUBING return val;} //----------------------------------------------------------------------------------// generates a transmission start // _____ ________// DATA: |_______|// ___ ___// SCK : ___| |___| |______//----------------------------------------------------------------------------------void SHT_Transstart(void){ DDRC = 0b00000011; // DATA is OutputDATA_OUT=1; SCK=0; //Initial statedelay_us(2);SCK=1;delay_us(2);DATA_OUT=0;delay_us(2);SCK=0; delay_us(5);SCK=1;delay_us(2);DATA_OUT=1; delay_us(2);SCK=0; DDRC = 0b00000010; // DATA is Input} //----------------------------------------------------------------------------------// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart// _____________________________________________________ ________// DATA: |_______|// _ _ _ _ _ _ _ _ _ ___ ___// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______//----------------------------------------------------------------------------------void SHT_ConnectionRest(void){ unsigned char i; DDRC = 0b00000011; // DATA is outputDATA_OUT=1; SCK=0; //Initial statefor(i=0;i=1 in case of no response form the sensor} //----------------------------------------------------------------------------------// makes a measurement (humidity/temperature) with checksum//----------------------------------------------------------------------------------char SHT_Measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode){ unsigned error=0;unsigned int temp=0;SHT_Transstart(); //transmission start switch(mode){ //send command to sensorcase TEMP : error+=SHT_WriteByte(MEASURE_TEMP); break;case HUMI : error+=SHT_WriteByte(MEASURE_HUMI); break;default : break; } DDRC = 0b00000010; // DATA is inputwhile (1){ if(DATA_IN==0) break; //wait until sensor has finished the measurement} if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached switch(mode){ //send command to sensorcase TEMP : temp=0;temp=SHT_ReadByte(ACK);temp