📄 text1.lst
字号:
179 1 *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
C51 COMPILER V8.05a TEXT1 07/03/2007 18:01:22 PAGE 4
180 1 *p_checksum =s_read_byte(noACK); //read checksum
181 1 return error;
182 1 }
183
184 //----------------------------------------------------------------------------------
185 void init_uart()
186 //----------------------------------------------------------------------------------
187 //9600 bps @ 11.059 MHz
188 {SCON = 0x52;
189 1 TMOD = 0x20;
190 1 TCON = 0x69;
191 1 TH1 = 0xfd;
192 1 }
193
194 //----------------------------------------------------------------------------------------
195 void calc_sth11(float *p_humidity ,float *p_temperature)
196 //----------------------------------------------------------------------------------------
197 // calculates temperature [癈] and humidity [%RH]
198 // input : humi [Ticks] (12 bit)
199 // temp [Ticks] (14 bit)
200 // output: humi [%RH]
201 // temp [癈]
202 { const float C1=-4.0; // for 12 Bit
203 1 const float C2=+0.0405; // for 12 Bit
204 1 const float C3=-0.0000028; // for 12 Bit
205 1 const float T1=+0.01; // for 14 Bit @ 5V
206 1 const float T2=+0.00008; // for 14 Bit @ 5V
207 1
208 1 float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
209 1 float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
210 1 float rh_lin; // rh_lin: Humidity linear
211 1 float rh_true; // rh_true: Temperature compensated humidity
212 1 float t_C; // t_C : Temperature [癈]
213 1
214 1 t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
215 1 rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
216 1 rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
217 1 if(rh_true>100)rh_true=100; //cut if the value is outside of
218 1 if(rh_true<0.1)rh_true=0.1; //the physical possible range
219 1
220 1 *p_temperature=t_C; //return temperature [癈]
221 1 *p_humidity=rh_true; //return humidity[%RH]
222 1 }
223
224 //--------------------------------------------------------------------
225 float calc_dewpoint(float h,float t)
226 //--------------------------------------------------------------------
227 // calculates dew point
228 // input: humidity [%RH], temperature [癈]
229 // output: dew point [癈]
230 { float logEx,dew_point;
231 1 logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
232 1 dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
233 1 return dew_point;
234 1 }
235
236
237
238
239 //delay()________________________________________________
240 void delay(unsigned char t)
241 {unsigned char i;
C51 COMPILER V8.05a TEXT1 07/03/2007 18:01:22 PAGE 5
242 1 for (i=0;i<t;i++);
243 1 }
244 //delay10ms()____________________________________________
245 void delay10ms(unsigned int count)
246 {unsigned int i,j,k;
247 1 for(i=0;i<count;i++)
248 1 for(j=0;j<10;j++)
249 1 for(k=0;k<120;k++)
250 1 ;
251 1 }
252 void display(void)
253 {unsigned char i;
254 1 for (i=0;i<6;i++)
255 1 {P0=~(1<<i); //低电平驱位选
256 2 P1=DATA_7SEG[led[i]];
257 2 delay(500); //about 1.5ms
258 2 }
259 1 P0=0xff;
260 1 P1=0xff;
261 1 }
262
263
264 //----------------------------------------------------------------------------------
265 void main()
266 //----------------------------------------------------------------------------------
267 // sample program that shows how to use SHT11 functions
268 // 1. connection reset
269 // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
270 // 3. calculate humidity [%RH] and temperature [癈]
271 // 4. calculate dew point [癈]
272 // 5. print temperature, humidity, dew point
273
274 { value humi_val,temp_val;
275 1 float dew_point;
276 1 unsigned char error,checksum,Lkeydata0,Lkeydata1,Lkeydata2,Lkeydata3,Lkeydata4,
277 1 Lkeydata5;
278 1 unsigned int i,j,k;
279 1
280 1 P0=0xff;
281 1 P1=0xff;
282 1 init_uart();
283 1 s_connectionreset();
284 1 while(1)
285 1 { error=0;
286 2 error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
287 2 error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
288 2 if(error!=0) s_connectionreset(); //in case of an error: connection reset
289 2 else
290 2 { humi_val.f=(float)humi_val.i; //converts integer to float
291 3 temp_val.f=(float)temp_val.i; //converts integer to float
292 3 calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
293 3 dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
294 3 printf("温度:%5.1fC 相对湿度:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
295 3
296 3 temp_val.f=temp_val.f*10;
297 3 temp_val.f=temp_val.f-5;
298 3 Lkeydata2= temp_val.f/100;
299 3
300 3 Lkeydata1= (temp_val.f-=Lkeydata2*100)/10;
301 3
302 3 Lkeydata0= temp_val.f-=Lkeydata1*10;
303 3
C51 COMPILER V8.05a TEXT1 07/03/2007 18:01:22 PAGE 6
304 3
305 3
306 3 humi_val.f=humi_val.f*10;
307 3 Lkeydata5= humi_val.f/100;
308 3
309 3 Lkeydata4= (humi_val.f-=Lkeydata5*100)/10;
310 3
311 3 Lkeydata3= humi_val.f-=Lkeydata4*10;
312 3 for (j=0;j<100;j++)
313 3 {
314 4
315 4 led[2]=Lkeydata2;
316 4
317 4 led[1]=Lkeydata1;
318 4 led[0]=Lkeydata0;
319 4 led[5]=Lkeydata5;
320 4
321 4 led[4]=Lkeydata4;
322 4 led[3]=Lkeydata3;
323 4
324 4
325 4
326 4 for(k=0;k<5;k++)
327 4 display();
328 4 }
329 3
330 3
331 3
332 3
333 3 }
334 2 //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
335 2 for (i=0;i<10;i++); //(be sure that the compiler doesn't eliminate this line!)
336 2 //-----------------------------------------------------------------------------------
-
337 2 }
338 1 }
339
340 //----------------------------------------------------------------------------------
341
342
343
344
345
346
347
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1740 ----
CONSTANT SIZE = 47 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 16 94
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -