Rev Author Line No. Line
2779 kaklik 1 ///////////////////////////////////////////////////////////////////////////////
2 // //
2836 kaklik 3 // Driver file for SHT1x Temperature & Humidity Sensor //
2779 kaklik 4 // //
2836 kaklik 5 // ***** To initialise SHT1x sensor upon power up ***** //
2779 kaklik 6 // //
7 // Function : sht_init() //
8 // Return : none //
9 // //
10 // //
11 // ***** To measure and caluculate SHT75 temp & real RH ***** //
12 // //
13 // Function : sht_rd (temp, truehumid) //
14 // Return : temperature & true humidity in float values //
15 // //
16 ///////////////////////////////////////////////////////////////////////////////
17  
18 //#define sht_data_pin PIN_D0
19 //#define sht_clk_pin PIN_D1
20  
21  
22 //***** Function to alert SHT75 *****
2836 kaklik 23 // generates a transmission start
24 // _____ ________
25 // DATA: |_______|
26 // ___ ___
27 // SCK : ___| |___| |______
2779 kaklik 28  
2836 kaklik 29 void sht_comstart (void)
2779 kaklik 30 {
31 output_float(sht_data_pin); //data high
32 output_low(sht_clk_pin); //clk low
33 delay_us(2);
34 output_high(sht_clk_pin); //clk high
35 delay_us(2);
36 output_low(sht_data_pin); //data low
37 delay_us(2);
38 output_low(sht_clk_pin); //clk low
39 delay_us(5);
40 output_high(sht_clk_pin); //clk high
41 delay_us(2);
42 output_float(sht_data_pin); //data high
43 delay_us(2);
44 output_low(sht_clk_pin); //clk low
45 }
46  
47  
48 //***** Function to write data to SHT75 *****
49  
2836 kaklik 50 int1 sht_comwrite (int8 iobyte)
2779 kaklik 51 {
52 int8 i, mask = 0x80;
53 int1 ack;
54  
55 //Shift out command
56 delay_us(5);
57 for(i=0; i<8; i++)
58 {
59 output_low(sht_clk_pin); //clk low
60 if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high
61 else output_low(sht_data_pin); //data low if MSB low
62 delay_us(2);
63 output_high(sht_clk_pin); //clk high
64 delay_us(2);
65 mask = mask >> 1; //shift to next bit
66 }
67  
68 //Shift in ack
69 output_low(sht_clk_pin); //clk low
70 delay_us(2);
71 ack = input(sht_data_pin); //get ack bit
72 output_high(sht_clk_pin); //clk high
73 delay_us(2);
74 output_low(sht_clk_pin); //clk low
75 return(ack);
76 }
77  
78  
79 //***** Function to read data from SHT75 *****
80  
2836 kaklik 81 int16 sht_comread (void)
2779 kaklik 82 {
83 int8 i;
84 int16 iobyte = 0;
85 const int16 mask0 = 0x0000;
86 const int16 mask1 = 0x0001;
87  
88 //shift in MSB data
89 for(i=0; i<8; i++)
90 {
91 iobyte = iobyte << 1;
92 output_high(sht_clk_pin); //clk high
93 delay_us(2);
94 if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
95 else iobyte |= mask0;
96 output_low(sht_clk_pin); //clk low
97 delay_us(2);
98 }
99  
100 //send ack 0 bit
101 output_low(sht_data_pin); //data low
102 delay_us(2);
103 output_high(sht_clk_pin); //clk high
104 delay_us(5);
105 output_low(sht_clk_pin); //clk low
106 delay_us(2);
107 output_float(sht_data_pin); //data high
108  
109 //shift in LSB data
110 for(i=0; i<8; i++)
111 {
112 iobyte = iobyte << 1;
113 output_high(sht_clk_pin); //clk high
114 delay_us(2);
115 if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
116 else iobyte |= mask0;
117 output_low(sht_clk_pin); //clk low
118 delay_us(2);
119 }
120  
121 //send ack 1 bit
122 output_float(sht_data_pin); //data high
123 delay_us(2);
124 output_high(sht_clk_pin); //clk high
125 delay_us(5);
126 output_low(sht_clk_pin); //clk low
127  
128 return(iobyte);
129 }
130  
131  
132 //***** Function to wait for SHT75 reading *****
133  
2836 kaklik 134 void sht_comwait (void)
2779 kaklik 135 {
136 int16 sht_delay;
137  
138 output_float(sht_data_pin); //data high
139 output_low(sht_clk_pin); //clk low
140 delay_us(2);
2836 kaklik 141 for(sht_delay=0; sht_delay<350; sht_delay++) // wait for max 350ms (for 14bit measurement)
2779 kaklik 142 {
143 if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready
2836 kaklik 144 delay_ms(1);
2779 kaklik 145 }
146 }
147  
148  
149 //***** Function to reset SHT75 communication *****
2836 kaklik 150 // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
151 // _____________________________________________________ ________
152 // DATA: |_______|
153 // _ _ _ _ _ _ _ _ _ ___ ___
154 // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
155 void sht_comreset (void)
2779 kaklik 156 {
157 int8 i;
158  
159 output_float(sht_data_pin); //data high
160 output_low(sht_clk_pin); //clk low
161 delay_us(5);
162 for(i=0; i<9; i++)
163 {
164 output_high(sht_clk_pin); //toggle clk 9 times
165 delay_us(5);
166 output_low(sht_clk_pin);
167 delay_us(5);
168 }
2836 kaklik 169 sht_comstart();
2779 kaklik 170 }
171  
172  
173 //***** Function to soft reset SHT75 *****
174  
175 void sht_soft_reset (void)
176 {
2836 kaklik 177 sht_comreset(); //SHT75 communication reset
178 sht_comwrite(0x1e); //send SHT75 reset command
2779 kaklik 179 delay_ms(15); //pause 15 ms
180 }
181  
182  
183 //***** Function to measure SHT75 temperature *****
184  
2836 kaklik 185 int16 sht_measuretemp (void)
2779 kaklik 186 {
187 int1 ack;
188 int16 iobyte;
189  
2836 kaklik 190 sht_comstart(); //alert SHT75
191 ack = sht_comwrite(0x03); //send measure temp command and read ack status
2779 kaklik 192 if(ack == 1) return;
2836 kaklik 193 sht_comwait(); //wait for SHT75 measurement to complete
194 iobyte = sht_comread(); //read SHT75 temp data
2779 kaklik 195 return(iobyte);
196 }
197  
198  
199 //***** Function to measure SHT75 RH *****
200  
2836 kaklik 201 int16 sht_measurehumid (void)
2779 kaklik 202 {
203 int1 ack;
204 int16 iobyte;
205  
2836 kaklik 206 sht_comstart(); //alert SHT75
207 ack = sht_comwrite(0x05); //send measure RH command and read ack status
2779 kaklik 208 if(ack == 1) return;
2836 kaklik 209 sht_comwait(); //wait for SHT75 measurement to complete
210 iobyte = sht_comread(); //read SHT75 temp data
2779 kaklik 211 return(iobyte);
212 }
213  
214  
215 //***** Function to calculate SHT75 temp & RH *****
216  
217 void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue)
218 {
2836 kaklik 219 float rh;
2779 kaklik 220  
221 //calculate temperature reading
222 tc = ((float) temp * 0.01) - 40.0;
223  
224 //calculate Real RH reading
225 rh = (float) humid;
226  
227 rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;
228  
229 //calculate True RH reading
230 rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
231 }
232  
233  
234 //***** Function to measure & calculate SHT75 temp & RH *****
235  
236 void sht_rd (float & temp, float & truehumid)
237 {
238 int16 restemp, reshumid;
239 float realhumid;
240 restemp = 0; truehumid = 0;
241  
2836 kaklik 242 restemp = sht_measuretemp(); //measure temp
243 reshumid = sht_measurehumid(); //measure RH
2779 kaklik 244 calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH
245 }
246  
247  
248 //***** Function to initialise SHT75 on power-up *****
249  
250 void sht_init (void)
251 {
2836 kaklik 252 sht_comreset(); //reset SHT75
2779 kaklik 253 delay_ms(20); //delay for power-up
254 }