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