?? main.lst
字號:
237
238 //----------------------------------------------------------------------------------
239 char s_write_byte(unsigned char value)
240 //----------------------------------------------------------------------------------
241 // writes a byte on the Sensibus and checks the acknowledge
C51 COMPILER V8.02 MAIN 06/02/2007 10:41:58 PAGE 5
242 {
243 1 unsigned char i,error=0;
244 1 for (i=0x80;i>0;i/=2) //shift bit for masking
245 1 { if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
246 2 else DATA=0;
247 2 SCK=1; //clk for SENSI-BUS
248 2 _nop_();_nop_();_nop_(); //pulswith approx. 5 us
249 2 SCK=0;
250 2 }
251 1 DATA=1; //release DATA-line
252 1 SCK=1; //clk #9 for ack
253 1 error=DATA; //check ack (DATA will be pulled down by SHT11)
254 1 SCK=0;
255 1 return error; //error=1 in case of no acknowledge
256 1 }
257
258 //----------------------------------------------------------------------------------
259 char s_read_byte(unsigned char ack)
260 //----------------------------------------------------------------------------------
261 // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
262 {
263 1 unsigned char i,val=0;
264 1 DATA=1; //release DATA-line
265 1 for (i=0x80;i>0;i/=2) //shift bit for masking
266 1 { SCK=1; //clk for SENSI-BUS
267 2 if (DATA) val=(val | i); //read bit
268 2 SCK=0;
269 2 }
270 1 DATA=!ack; //in case of "ack==1" pull down DATA-Line
271 1 SCK=1; //clk #9 for ack
272 1 _nop_();_nop_();_nop_(); //pulswith approx. 5 us
273 1 SCK=0;
274 1 DATA=1; //release DATA-line
275 1 return val;
276 1 }
277
278 //----------------------------------------------------------------------------------
279 void s_transstart(void)
280 //----------------------------------------------------------------------------------
281 // generates a transmission start
282 // _____ ________
283 // DATA: |_______|
284 // ___ ___
285 // SCK : ___| |___| |______
286 {
287 1 DATA=1; SCK=0; //Initial state
288 1 _nop_();
289 1 SCK=1;
290 1 _nop_();
291 1 DATA=0;
292 1 _nop_();
293 1 SCK=0;
294 1 _nop_();_nop_();_nop_();
295 1 SCK=1;
296 1 _nop_();
297 1 DATA=1;
298 1 _nop_();
299 1 SCK=0;
300 1 }
301
302 //----------------------------------------------------------------------------------
303 void s_connectionreset(void)
C51 COMPILER V8.02 MAIN 06/02/2007 10:41:58 PAGE 6
304 //----------------------------------------------------------------------------------
305 // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
306 // _____________________________________________________ ________
307 // DATA: |_______|
308 // _ _ _ _ _ _ _ _ _ ___ ___
309 // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
310 {
311 1 unsigned char i;
312 1 DATA=1; SCK=0; //Initial state
313 1 for(i=0;i<9;i++) //9 SCK cycles
314 1 { SCK=1;
315 2 SCK=0;
316 2 }
317 1 s_transstart(); //transmission start
318 1 }
319
320 //----------------------------------------------------------------------------------
321 char s_softreset(void)
322 //----------------------------------------------------------------------------------
323 // resets the sensor by a softreset
324 {
325 1 unsigned char error=0;
326 1 s_connectionreset(); //reset communication
327 1 error+=s_write_byte(RESET); //send RESET-command to sensor
328 1 return error; //error=1 in case of no response form the sensor
329 1 }
330
331 //----------------------------------------------------------------------------------
332 char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
333 //----------------------------------------------------------------------------------
334 // reads the status register with checksum (8-bit)
335 {
336 1 unsigned char error=0;
337 1 s_transstart(); //transmission start
338 1 error=s_write_byte(STATUS_REG_R); //send command to sensor
339 1 *p_value=s_read_byte(ACK); //read status register (8-bit)
340 1 *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
341 1 return error; //error=1 in case of no response form the sensor
342 1 }
343
344 //----------------------------------------------------------------------------------
345 char s_write_statusreg(unsigned char *p_value)
346 //----------------------------------------------------------------------------------
347 // writes the status register with checksum (8-bit)
348 {
349 1 unsigned char error=0;
350 1 s_transstart(); //transmission start
351 1 error+=s_write_byte(STATUS_REG_W);//send command to sensor
352 1 error+=s_write_byte(*p_value); //send value of status register
353 1 return error; //error>=1 in case of no response form the sensor
354 1 }
355
356 //----------------------------------------------------------------------------------
357 char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
358 //----------------------------------------------------------------------------------
359 // makes a measurement (humidity/temperature) with checksum
360 {
361 1 unsigned error=0;
362 1 unsigned int i;
363 1
364 1 s_transstart(); //transmission start
365 1 switch(mode){ //send command to sensor
C51 COMPILER V8.02 MAIN 06/02/2007 10:41:58 PAGE 7
366 2 case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
367 2 case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
368 2 default : break;
369 2 }
370 1 for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
371 1 if(DATA) error+=1; // or timeout (~2 sec.) is reached
372 1 *(p_value) =s_read_byte(ACK); //read the first byte (MSB)
373 1 *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
374 1 *p_checksum =s_read_byte(noACK); //read checksum
375 1 return error;
376 1 }
377
378 //----------------------------------------------------------------------------------
379 void init_uart()
380 //----------------------------------------------------------------------------------
381 //9600 bps @ 11.059 MHz
382 {SCON = 0x52;
383 1 TMOD = 0x20;
384 1 TCON = 0x69;
385 1 TH1 = 0xfd;
386 1 }
387
388 //----------------------------------------------------------------------------------------
389 void calc_sth11(float *p_humidity ,float *p_temperature)
390 //----------------------------------------------------------------------------------------
391 // calculates temperature [癈] and humidity [%RH]
392 // input : humi [Ticks] (12 bit)
393 // temp [Ticks] (14 bit)
394 // output: humi [%RH]
395 // temp [癈]
396 { const float C1=-4.0; // for 12 Bit
397 1 const float C2=+0.0405; // for 12 Bit
398 1 const float C3=-0.0000028; // for 12 Bit
399 1 const float T1=+0.01; // for 14 Bit @ 5V
400 1 const float T2=+0.00008; // for 14 Bit @ 5V
401 1
402 1 float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
403 1 float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
404 1 float rh_lin; // rh_lin: Humidity linear
405 1 float rh_true; // rh_true: Temperature compensated humidity
406 1 float t_C; // t_C : Temperature [癈]
407 1
408 1 t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
409 1 rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
410 1 rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
411 1 if(rh_true>100)rh_true=100; //cut if the value is outside of
412 1 if(rh_true<0.1)rh_true=0.1; //the physical possible range
413 1
414 1 *p_temperature=t_C; //return temperature [癈]
415 1 *p_humidity=rh_true; //return humidity[%RH]
416 1 }
417
418 //--------------------------------------------------------------------
419 float calc_dewpoint(float h,float t)
420 //--------------------------------------------------------------------
421 // calculates dew point
422 // input: humidity [%RH], temperature [癈]
423 // output: dew point [癈]
424 { float logEx,dew_point;
425 1 logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
426 1 dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
427 1 return dew_point;
C51 COMPILER V8.02 MAIN 06/02/2007 10:41:58 PAGE 8
428 1 }
429
430 //----------------------------------------------------------------------------------
431 void main()
432 //----------------------------------------------------------------------------------
433 // sample program that shows how to use SHT11 functions
434 // 1. connection reset
435 // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
436 // 3. calculate humidity [%RH] and temperature [癈]
437 // 4. calculate dew point [癈]
438 // 5. print temperature, humidity, dew point
439
440 { value humi_val,temp_val;
441 1 float dew_point;
442 1 unsigned char error,checksum;
443 1 unsigned int i;
444 1
445 1 init_uart();
446 1 s_connectionreset();
447 1 while(1)
448 1 { error=0;
449 2 error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
450 2 error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
451 2 if(error!=0) s_connectionreset(); //in case of an error: connection reset
452 2 else
453 2 { humi_val.f=(float)humi_val.i; //converts integer to float
454 3 temp_val.f=(float)temp_val.i; //converts integer to float
455 3 calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
456 3 dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
457 3 printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
458 3 }
459 2 //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
460 2 for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
461 2 //-----------------------------------------------------------------------------------
-
462 2 }
463 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1523 ----
CONSTANT SIZE = 54 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 21 89
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 + -