📄 3_ds.lst
字号:
425 /*void main(void)
426 {
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 8
427 do
428 {
429 delay(1); // 延时1ms
430 convert(); // 启动温度转换,需要750ms
431 delay(1000); // 延时1s
432 RdTemp(); // 读取温度
433 }
434 while(1);
435 }
436
437 */
438
439 /***************************校验CRC16,CRC8************************************/
440 unsigned char crc16(unsigned char *chcmd,unsigned char lenth,unsigned char num) //校验码crc16计算
-
441 {
442 1 unsigned char crc16_h=0xff,crc16_l=0xff;
443 1 unsigned char save_h=0x00,save_l=0x00;
444 1 unsigned char ch=0xa0,cl=0x01;
445 1 unsigned char i,j;
446 1 unsigned char dat;
447 1
448 1 for(i=0;i<lenth;i++)
449 1 {
450 2 dat=*chcmd;
451 2 chcmd++;
452 2 crc16_h=crc16_h^dat;
453 2 for(j=0;j<8;j++)
454 2 {
455 3 save_h=crc16_h;
456 3 save_l=crc16_l;
457 3 crc16_l=crc16_l>>1;
458 3 crc16_h=crc16_h>>1;
459 3
460 3 if((save_l&0x01)==0x01) crc16_h=crc16_h|0x80;
461 3 if((save_h&0x01)==0x01)
462 3 {
463 4 crc16_l=crc16_l^ch;
464 4 crc16_h=crc16_h^cl;
465 4 }
466 3 }
467 2 }
468 1 if(num==1) return crc16_h;
469 1 if(num==0) return crc16_l;
470 1 }
471
472 void serial() interrupt 4
473 {
474 1 unsigned char p;
475 1 unsigned char a;
476 1 if (RI)
477 1 {
478 2
479 2 a=SBUF;
480 2 redata[0]=redata[1];
481 2 redata[1]=redata[2];
482 2 redata[2]=redata[3];
483 2 redata[3]=redata[4];
484 2 redata[4]=redata[5];
485 2 redata[5]=redata[6];
486 2 redata[6]=redata[7];
487 2 redata[7]=a;
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 9
488 2 RI=0;
489 2 }
490 1 if((redata[0]==0x01))
491 1 {
492 2 if(redata[1]==0x03)
493 2 {
494 3
495 3 //int485=1;
496 3 EA=0; //关总中断
497 3 sedata[0]=0x01;
498 3 sedata[1]=0x03;
499 3 sedata[2]=0x04;
500 3 sedata[3]=s.s_t[0];
501 3 sedata[4]=s.s_t[1];
502 3 sedata[5]=ds1.ds1_t[0];
503 3 sedata[6]=ds1.ds1_t[1];
504 3 sedata[7]=ds2.ds2_t[0];
505 3 sedata[8]=ds2.ds2_t[1];
506 3 sedata[9]=ds3.ds3_t[0];
507 3 sedata[10]=ds3.ds3_t[1];
508 3
509 3 sedata[11]=crc16(sedata,11,1);
510 3 sedata[12]=crc16(sedata,11,0);
511 3
512 3
513 3 for(p=0;p<13;p++)
514 3 {
515 4 SBUF=sedata[p];
516 4 while(!TI);TI=0;
517 4 }
518 3 ds1.ds1_t[0]=0;
519 3 ds2.ds2_t[0]=0;
520 3 ds3.ds3_t[0]=0;
521 3 ds1.ds1_t[1]=0;
522 3 ds2.ds2_t[1]=0;
523 3 ds3.ds3_t[1]=0;
524 3 EA=1; //开总中断
525 3
526 3
527 3 }
528 2 }
529 1 RI=0;
530 1 TI=0;
531 1
532 1 }
533 void init(void)
534 {
535 1 TMOD = 0x20; //自动再装入8位计数器
536 1 PCON = 0X00; //波特率倍率为零
537 1 SCON = 0x50; //采用方式一进行串口数据通信
538 1 TH1 = 0xfd;
539 1 TL1 = 0xfd;
540 1 PS = 1; //若PS=1,则串行口指定为高中断优先级,否则为低中断优先级
541 1 ES = 1; //当ES=1 允许串行中断
542 1 EA = 1; //当EA=1时,中断总允许
543 1 TR1 = 1; //启动定时器,产生波特率
544 1 RI = 0; //清接收中断标志位
545 1 TI = 0; //清发送中断标志位
546 1 // int485=0; // RE低电平有效的接收允许,DE高电平有效的发送允许
547 1
548 1 }
549 //----------------------------------------------------------------------------------
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 10
550 void main()
551 //----------------------------------------------------------------------------------
552 // sample program that shows how to use SHT11 functions
553 // 1. connection reset
554 // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
555 // 3. calculate humidity [%RH] and temperature [癈]
556 // 4. calculate dew point [癈]
557 // 5. print temperature, humidity, dew point
558
559 { value humi_val,temp_val;
560 1 //value temp_val;
561 1 //float dew_point;
562 1 unsigned char error,checksum;
563 1 // unsigned char checksum;
564 1 // unsigned int i;
565 1 //unsigned char tplsb,tpmsb;
566 1 ds_num=0x07;
567 1 // int485=1;
568 1 // init_uart();
569 1 init();
570 1 s_connectionreset();
571 1 while(1)
572 1 {
573 2 delay(1); // 延时1ms
574 2 convert(); // 启动温度转换,需要750ms
575 2 delay(1000); // 延时1s
576 2
577 2 RdTemp(); // 读取温度
578 2
579 2 //ds1.ds1_temp=(tpmsb*256+tplsb)*6.25;
580 2 //ds1.ds1_temp=ds1_tem*100;
581 2
582 2 error=0;
583 2 //error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
584 2 error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
585 2 //s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
586 2 //if(error!=0) s_connectionreset(); //in case of an error: connection reset
587 2 //else
588 2 //{ //humi_val.f=(float)humi_val.i; //converts integer to float
589 2 temp_val.f=(float)temp_val.i; //converts integer to float
590 2 calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
591 2 //calc_sth11(&temp_val.f,&temp_val.f);
592 2 //dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
593 2 //printf("temp:%dC humi:%d%%\n",temp_val.i,humi_val.i);
594 2 //printf("temp:%5.2fC humi:%5.2f%%\n",temp_val.f,humi_val.f);
595 2 //printf("%5.1f,%5.1f\n",temp_val.f,ds_temp);
596 2 // printf("%d,%d\n",temp_val.i,humi_val.i);
597 2 // while(!TI);TI=0;
598 2
599 2 //tm=&temp_val.f;
600 2 //SBUF=*(tm+1);
601 2 //s_temph=temp_val.f;
602 2 //s_temph=s_temph*100;
603 2 //s_templ=(temp_val.f-s_temph)*100+1;
604 2 //s_templ=s_temp*100;
605 2 //ds_temph=ds_temp;
606 2 //ds_templ=(ds_temp-ds_temph)*100+1;
607 2 //SBUF=s_temph;
608 2 //while(!TI);TI=0;
609 2 //SBUF=*tm;
610 2 //SBUF=s_templ;
611 2 //while(!TI);TI=0;
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 11
612 2 s.s_temp=temp_val.f*100;
613 2
614 2
615 2 //}
616 2 //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
617 2 //for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
618 2 //-----------------------------------------------------------------------------------
-
619 2 }
620 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1760 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 21 33
PDATA SIZE = ---- ----
DATA SIZE = 11 84
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -