ATmega8 SHT71 Temperature Humidity and Dew Point Example
ATmega8 SHT71 Temperature Humidity and Dew Point Example
http://www.electronics-base.com/projects/complete-projects/152-atmega8-sht71temperature-humidity-and-dew-point-example
\
Published on Monday, 19 March 2012 20:54 | Written by Super User | Hits: 4918
1 DDRC = 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
/*****************************************************
Complete example with more comments and further support can be found at
www.Electronics-Base.com under complete projects section
Date
: 3/19/2012
Author : www.Electronics-Base.com
Chip type
: ATmega8
Program type
: Application
AVR Core Clock frequency: 8.000000 MHz
Memory model
: Small
External RAM size
: 0
Data Stack size
: 256
www.Electronics-Base.com
*****************************************************/
#include <mega8.h>
// Standard Input/Output functions
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
#include <stdio.h>
//must be included
#include <delay.h>
//must be included
#include <stdlib.h
//must be included
#include <math.h >
typedef union
{ unsigned int i;
float f;
} value;
enum {TEMP,HUMI};
#define
DATA_OUT
#define
DATA_IN
#define
SCK
#define noACK 0
#define ACK
1
PORTC.0
PINC.0
PORTC.1
|_______|
103 // DATA:
//
_
_
_
_
_
_
_
_
_
___
___
104
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|
|___|
|
105 ______
106 //----------------------------------------------------------------------107 ----------108 void SHT_ConnectionRest(void)
{
109 unsigned char i;
110 DDRC = 0b00000011;
// DATA is output
111 DATA_OUT=1; SCK=0;
//Initial state
for(i=0;i<9;i++)
//9
SCK cycles
112
{
SCK=1;
113
delay_us(1);
114
SCK=0;
115
delay_us(1);
116 }
//transmission start
117 SHT_Transstart();
DDRC = 0b00000010;
// DATA is Input
118
}
119 //----------------------------------------------------------------------120 ----------121 // resets the sensor by a softreset
122 //--------------------------------------------------------------------------------123 char SHT_SoftRst(void)
124 {
125 unsigned char error=0;
//reset communication
126 SHT_ConnectionRest();
error+=SHT_WriteByte(RESET);
//send RESET-command to sensor
127
return error;
//error=1 in case of no response form
128 the sensor
129 }
130 //----------------------------------------------------------------------131 ----------reads the status register with checksum (8-bit)
132 //
//----------------------------------------------------------------------133 ----------134 char SHT_Read_StatusReg(unsigned char *p_value, unsigned char *p_checksum)
135 {
136 unsigned char error=0;
SHT_Transstart();
//transmission start
137 error=SHT_WriteByte(STATUS_REG_R); //send command to sensor
138 *p_value=SHT_ReadByte(ACK);
//read status register (8-bit)
139 *p_checksum=SHT_ReadByte(noACK);
//read checksum (8-bit)
return
error;
//error=1
in case of no response form
140
the sensor
141
}
142 //----------------------------------------------------------------------143 ----------144 // writes the status register with checksum (8-bit)
145 //--------------------------------------------------------------------------------146 char SHT_Write_StatusReg(unsigned char *p_value)
147 {
Func2=In Func1=In
State2=T State1=T
Func1=In Func0=In
State1=T State0=T
Func2=In Func1=In
State2=T State1=T
283 ASSR=0x00;
284 TCCR2=0x00;
TCNT2=0x00;
285 OCR2=0x00;
286 // External Interrupt(s) initialization
287 // INT0: Off
288 // INT1: Off
289 MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
290 TIMSK=0x00;
291 // USART initialization
292 // Communication Parameters: 8 Data, 1 Stop, No Parity
293 // USART Receiver: Off
// USART Transmitter: On
294 // USART Mode: Asynchronous
295 // USART Baud Rate: 9600
296 UCSRA=0x00;
297 UCSRB=0x08;
298 UCSRC=0x86;
UBRRH=0x00;
299 UBRRL=0x33;
300 // Analog Comparator initialization
301 // Analog Comparator: Off
302 // Analog Comparator Input Capture by Timer/Counter 1: Off
303 ACSR=0x80;
SFIOR=0x00;
304 SHT_ConnectionRest();
305
306 while (1)
307
{
// Place your code here
308
delay_ms(500);
309
error=0;
310
error+=SHT_Measure((unsigned char*)( &humi_val.i),&checksum,HUMI);
311 //measure humidity
error+=SHT_Measure((unsigned char*) (&temp_val.i),&checksum,TEMP);
312
//measure
temperature
313
error += SHT_Read_StatusReg(&inp, &checksum);
314
if(error!=0)
315
{
316
SHT_ConnectionRest();
//in case of an error:
317 connection reset
putsf("Error");
318
}
319
else
320
{
321
humi_val.f=(float)tempervalue[1];
//converts
integer
to
float
322
temp_val.f=(float)tempervalue[0];
//converts
323
integer to float
324
humi_val.f=Calc_SHT71(humi_val.f,&temp_val.f);
//calculate
325 humidity, temperature
dewpoint=Calc_dewpoint(humi_val.f,temp_val.f) ;
326
ftoa(humi_val.f,2,Humidity);
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