Line 13... |
Line 13... |
13 |
// Function : sht_rd (temp, truehumid) // |
13 |
// Function : sht_rd (temp, truehumid) // |
14 |
// Return : temperature & true humidity in float values // |
14 |
// Return : temperature & true humidity in float values // |
15 |
// // |
15 |
// // |
16 |
/////////////////////////////////////////////////////////////////////////////// |
16 |
/////////////////////////////////////////////////////////////////////////////// |
17 |
|
17 |
|
18 |
#define sht_data_pin PIN_D3 //(I use A1 as data input) |
18 |
#define sht_data_pin PIN_D0 |
19 |
#define sht_clk_pin PIN_C4 |
19 |
#define sht_clk_pin PIN_D1 |
20 |
|
- |
|
21 |
|
- |
|
22 |
|
20 |
|
23 |
|
21 |
|
24 |
//***** Function to alert SHT75 ***** |
22 |
//***** Function to alert SHT75 ***** |
25 |
|
23 |
|
26 |
void comstart (void) |
24 |
void comstart (void) |
27 |
{ |
25 |
{ |
28 |
output_float(sht_data_pin); //data high |
26 |
output_float(sht_data_pin); //data high |
29 |
output_bit(sht_clk_pin, 0); //clk low |
27 |
output_low(sht_clk_pin); //clk low |
30 |
delay_us(1); |
28 |
delay_us(2); |
31 |
output_bit(sht_clk_pin, 1); //clk high |
29 |
output_high(sht_clk_pin); //clk high |
32 |
delay_us(1); |
30 |
delay_us(2); |
33 |
output_bit(sht_data_pin, 0); //data low |
31 |
output_low(sht_data_pin); //data low |
34 |
delay_us(1); |
32 |
delay_us(2); |
35 |
output_bit(sht_clk_pin, 0); //clk low |
33 |
output_low(sht_clk_pin); //clk low |
- |
|
34 |
delay_us(5); |
- |
|
35 |
output_high(sht_clk_pin); //clk high |
36 |
delay_us(2); |
36 |
delay_us(2); |
37 |
output_bit(sht_clk_pin, 1); //clk high |
- |
|
38 |
delay_us(1); |
- |
|
39 |
output_float(sht_data_pin); //data high |
37 |
output_float(sht_data_pin); //data high |
40 |
delay_us(1); |
38 |
delay_us(2); |
41 |
output_bit(sht_clk_pin, 0); //clk low |
39 |
output_low(sht_clk_pin); //clk low |
42 |
} |
40 |
} |
43 |
|
41 |
|
44 |
|
42 |
|
45 |
//***** Function to write data to SHT75 ***** |
43 |
//***** Function to write data to SHT75 ***** |
46 |
|
44 |
|
Line 48... |
Line 46... |
48 |
{ |
46 |
{ |
49 |
int8 i, mask = 0x80; |
47 |
int8 i, mask = 0x80; |
50 |
int1 ack; |
48 |
int1 ack; |
51 |
|
49 |
|
52 |
//Shift out command |
50 |
//Shift out command |
53 |
delay_us(4); |
51 |
delay_us(5); |
- |
|
52 |
for(i=0; i<8; i++) |
- |
|
53 |
{ |
- |
|
54 |
output_low(sht_clk_pin); //clk low |
54 |
for(i=0; i<8> 0) output_float(sht_data_pin); //data high if MSB high |
55 |
if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high |
55 |
else output_bit(sht_data_pin, 0); //data low if MSB low |
56 |
else output_low(sht_data_pin); //data low if MSB low |
56 |
delay_us(1); |
57 |
delay_us(2); |
57 |
output_bit(sht_clk_pin, 1); //clk high |
58 |
output_high(sht_clk_pin); //clk high |
58 |
delay_us(1); |
59 |
delay_us(2); |
59 |
mask = mask >> 1; //shift to next bit |
60 |
mask = mask >> 1; //shift to next bit |
60 |
} |
61 |
} |
61 |
|
62 |
|
62 |
//Shift in ack |
63 |
//Shift in ack |
63 |
output_bit(sht_clk_pin, 0); //clk low |
64 |
output_low(sht_clk_pin); //clk low |
64 |
delay_us(1); |
65 |
delay_us(2); |
65 |
ack = input(sht_data_pin); //get ack bit |
66 |
ack = input(sht_data_pin); //get ack bit |
66 |
output_bit(sht_clk_pin, 1); //clk high |
67 |
output_high(sht_clk_pin); //clk high |
67 |
delay_us(1); |
68 |
delay_us(2); |
68 |
output_bit(sht_clk_pin, 0); //clk low |
69 |
output_low(sht_clk_pin); //clk low |
69 |
return(ack); |
70 |
return(ack); |
70 |
} |
71 |
} |
71 |
|
72 |
|
72 |
|
73 |
|
73 |
//***** Function to read data from SHT75 ***** |
74 |
//***** Function to read data from SHT75 ***** |
Line 81... |
Line 82... |
81 |
|
82 |
|
82 |
//shift in MSB data |
83 |
//shift in MSB data |
83 |
for(i=0; i<8; i++) |
84 |
for(i=0; i<8; i++) |
84 |
{ |
85 |
{ |
85 |
iobyte = iobyte << 1; |
86 |
iobyte = iobyte << 1; |
86 |
output_bit(sht_clk_pin, 1); //clk high |
87 |
output_high(sht_clk_pin); //clk high |
87 |
delay_us(1); |
88 |
delay_us(2); |
88 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
89 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
89 |
else iobyte |= mask0; |
90 |
else iobyte |= mask0; |
90 |
output_bit(sht_clk_pin, 0); //clk low |
91 |
output_low(sht_clk_pin); //clk low |
91 |
delay_us(1); |
92 |
delay_us(2); |
92 |
} |
93 |
} |
93 |
|
94 |
|
94 |
//send ack 0 bit |
95 |
//send ack 0 bit |
95 |
output_bit(sht_data_pin, 0); //data low |
96 |
output_low(sht_data_pin); //data low |
96 |
delay_us(1); |
97 |
delay_us(2); |
97 |
output_bit(sht_clk_pin, 1); //clk high |
98 |
output_high(sht_clk_pin); //clk high |
- |
|
99 |
delay_us(5); |
- |
|
100 |
output_low(sht_clk_pin); //clk low |
98 |
delay_us(2); |
101 |
delay_us(2); |
99 |
output_bit(sht_clk_pin, 0); //clk low |
- |
|
100 |
delay_us(1); |
- |
|
101 |
output_float(sht_data_pin); //data high |
102 |
output_float(sht_data_pin); //data high |
102 |
|
103 |
|
103 |
//shift in LSB data |
104 |
//shift in LSB data |
104 |
for(i=0; i<8; i++) |
105 |
for(i=0; i<8; i++) |
105 |
{ |
106 |
{ |
106 |
iobyte = iobyte << 1; |
107 |
iobyte = iobyte << 1; |
107 |
output_bit(sht_clk_pin, 1); //clk high |
108 |
output_high(sht_clk_pin); //clk high |
108 |
delay_us(1); |
109 |
delay_us(2); |
109 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
110 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
110 |
else iobyte |= mask0; |
111 |
else iobyte |= mask0; |
111 |
output_bit(sht_clk_pin, 0); //clk low |
112 |
output_low(sht_clk_pin); //clk low |
112 |
delay_us(1); |
113 |
delay_us(2); |
113 |
} |
114 |
} |
114 |
|
115 |
|
115 |
//send ack 1 bit |
116 |
//send ack 1 bit |
116 |
output_float(sht_data_pin); //data high |
117 |
output_float(sht_data_pin); //data high |
117 |
delay_us(1); |
- |
|
118 |
output_bit(sht_clk_pin, 1); //clk high |
- |
|
119 |
delay_us(2); |
118 |
delay_us(2); |
- |
|
119 |
output_high(sht_clk_pin); //clk high |
- |
|
120 |
delay_us(5); |
120 |
output_bit(sht_clk_pin, 0); //clk low |
121 |
output_low(sht_clk_pin); //clk low |
121 |
|
122 |
|
122 |
return(iobyte); |
123 |
return(iobyte); |
123 |
} |
124 |
} |
124 |
|
125 |
|
125 |
|
126 |
|
Line 128... |
Line 129... |
128 |
void comwait (void) |
129 |
void comwait (void) |
129 |
{ |
130 |
{ |
130 |
int16 sht_delay; |
131 |
int16 sht_delay; |
131 |
|
132 |
|
132 |
output_float(sht_data_pin); //data high |
133 |
output_float(sht_data_pin); //data high |
133 |
output_bit(sht_clk_pin, 0); //clk low |
134 |
output_low(sht_clk_pin); //clk low |
134 |
delay_us(1); |
135 |
delay_us(2); |
135 |
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms |
136 |
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms |
136 |
{ |
137 |
{ |
137 |
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready |
138 |
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready |
138 |
delay_us(10); |
139 |
delay_us(10); |
139 |
} |
140 |
} |
Line 145... |
Line 146... |
145 |
void comreset (void) |
146 |
void comreset (void) |
146 |
{ |
147 |
{ |
147 |
int8 i; |
148 |
int8 i; |
148 |
|
149 |
|
149 |
output_float(sht_data_pin); //data high |
150 |
output_float(sht_data_pin); //data high |
150 |
output_bit(sht_clk_pin, 0); //clk low |
151 |
output_low(sht_clk_pin); //clk low |
151 |
delay_us(2); |
152 |
delay_us(5); |
152 |
for(i=0; i<9; i++) |
153 |
for(i=0; i<9; i++) |
153 |
{ |
154 |
{ |
154 |
output_bit(sht_clk_pin, 1); //toggle clk 9 times |
155 |
output_high(sht_clk_pin); //toggle clk 9 times |
155 |
delay_us(2); |
156 |
delay_us(5); |
156 |
output_bit(sht_clk_pin, 0); |
157 |
output_low(sht_clk_pin); |
157 |
delay_us(2); |
158 |
delay_us(5); |
158 |
} |
159 |
} |
159 |
comstart(); |
160 |
comstart(); |
160 |
} |
161 |
} |
161 |
|
162 |
|
162 |
|
163 |
|
Line 240... |
Line 241... |
240 |
void sht_init (void) |
241 |
void sht_init (void) |
241 |
{ |
242 |
{ |
242 |
comreset(); //reset SHT75 |
243 |
comreset(); //reset SHT75 |
243 |
delay_ms(20); //delay for power-up |
244 |
delay_ms(20); //delay for power-up |
244 |
} |
245 |
} |
245 |
|
- |
|