⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shtxx_sample_code.lst

📁 这是单片机驱动温湿度传感器SHT11温湿度传感器的源代码
💻 LST
📖 第 1 页 / 共 4 页
字号:
 622   1          {
 623   2              _Nop();           
 624   2              SCL=0;       /*置时钟线为低,准备接收数据位*/
 625   2              _Nop();
 626   2              _Nop();         /*时钟低电平周期大于4.7μs*/
 627   2              _Nop();
 628   2              _Nop();
 629   2              _Nop();
 630   2              SCL=1;       /*置时钟线为高使数据线上数据有效*/
 631   2              _Nop();
 632   2              _Nop();
 633   2              retc=retc<<1;
 634   2              if(SDA==1)retc=retc+1; /*读数据位,接收的数据位放入retc中 */
 635   2              _Nop();
 636   2              _Nop(); 
 637   2              }
 638   1              SCL=0;    
 639   1              _Nop();
 640   1              _Nop();
 641   1              return(retc);
 642   1      }
 643          
 644          
 645          void Ack_I2c(bit a)             //主控器进行应答信号
 646          {
 647   1        
 648   1              if(a==0)SDA=0;     /*在此发出应答或非应答信号 */
 649   1          else SDA=1;
 650   1              _Nop();
 651   1              _Nop();
 652   1              _Nop();      
 653   1              SCL=1;
 654   1          _Nop();
 655   1          _Nop();              //时钟低电平周期大于4μ
 656   1          _Nop();
 657   1          _Nop();
 658   1          _Nop();  
 659   1              SCL=0;                /*清时钟线,钳住I2C总线以便继续接收*/
 660   1          _Nop();
 661   1          _Nop();    
 662   1      }
 663          //bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)          //向有子地址器件发送多字节数据函数 
 664          //子地址为8位的数据传送
 665          bit ISendStr_8(uchar add,uchar son_add,uchar dat)               //向子地址为8位的器件发送多字节数据函数 
 666          {
 667   1              Start_I2c();               /*启动总线*/
 668   1              SendByte(add);            /*发送器件地址*/
 669   1          if(ack==0)return(0);
 670   1              SendByte(son_add);            /*发送器件子地址*/
 671   1          if(ack==0)return(0);
 672   1          SendByte(dat);               /*发送数据*/
 673   1          if(ack==0)return(0);
C51 COMPILER V7.07   SHTXX_SAMPLE_CODE                                                     04/04/2007 18:18:08 PAGE 12  

 674   1              Stop_I2c();                 /*结束总线*/ 
 675   1              return(1);
 676   1      }
 677          //子地址为16位的数据传送
 678          bit ISendStr_16(uchar add,uint son_add,uchar dat)               //向子地址为16位的器件发送多字节数据函数 
 679          {
 680   1              Start_I2c();               /*启动总线*/
 681   1              SendByte(add);            /*发送器件地址*/
 682   1          if(ack==0)return(0);
 683   1              SendByte(son_add/256);            /*发送器件子地址*/
 684   1          if(ack==0)return(0);
 685   1              SendByte(son_add%256);            /*发送器件子地址*/
 686   1          if(ack==0)return(0);
 687   1          SendByte(dat);               /*发送数据*/
 688   1          if(ack==0)return(0);
 689   1              Stop_I2c();                 /*结束总线*/ 
 690   1              return(1);
 691   1      }
 692          
 693          
 694          
 695          //bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)           //向有子地址器件读取多字节数据函数
 696          //子地址为8位的数据传送
 697          uchar IRcvStr_8(uchar add,uchar son_add)                //向子地址为8位的器件读取多字节数据函数
 698          {
 699   1      
 700   1              uchar i;
 701   1              Start_I2c();               /*启动总线*/
 702   1              SendByte(add);            /*发送器件地址*/
 703   1              SendByte(son_add);            /*发送器件子地址2*/
 704   1              Start_I2c();
 705   1              SendByte(add+0x01);
 706   1              i=RcvByte();
 707   1              Ack_I2c(1);                 /*发送非应位*/
 708   1              Stop_I2c();                    /*结束总线*/ 
 709   1              return (i);
 710   1      }
 711          //子地址为16位的数据传送
 712          uchar IRcvStr_16(uchar add,uint son_add)                //向子地址为16位的器件读取多字节数据函数
 713          {
 714   1      
 715   1              uchar i;
 716   1              Start_I2c();               /*启动总线*/
 717   1              SendByte(add);            /*发送器件地址*/
 718   1              SendByte(son_add/256);            /*发送器件子地址1*/
 719   1              SendByte(son_add%256);            /*发送器件子地址2*/
 720   1              Start_I2c();
 721   1              SendByte(add+0x01);
 722   1              i=RcvByte();
 723   1              Ack_I2c(1);                 /*发送非应位*/
 724   1              Stop_I2c();                    /*结束总线*/ 
 725   1              return (i);
 726   1      }
 727          
 728          
 729          //----------------------------------------------------------------------------------
 730          char s_write_byte(unsigned char value)
 731          //----------------------------------------------------------------------------------
 732          // writes a byte on the Sensibus and checks the acknowledge 
 733          { 
 734   1        unsigned char i,error=0;  
 735   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
C51 COMPILER V7.07   SHTXX_SAMPLE_CODE                                                     04/04/2007 18:18:08 PAGE 13  

 736   1        { if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS
 737   2          else DATA=0;                        
 738   2          SCK=1;                          //clk for SENSI-BUS
 739   2          _nop_();_nop_();_nop_();        //pulswith approx. 5 us     
 740   2          SCK=0;
 741   2        }
 742   1        DATA=1;                           //release DATA-line
 743   1        SCK=1;                            //clk #9 for ack 
 744   1        error=DATA;                       //check ack (DATA will be pulled down by SHT11)
 745   1        SCK=0;        
 746   1        return error;                     //error=1 in case of no acknowledge
 747   1      }
 748          
 749          //----------------------------------------------------------------------------------
 750          char s_read_byte(unsigned char ack)
 751          //----------------------------------------------------------------------------------
 752          // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 
 753          { 
 754   1        unsigned char i,val=0;
 755   1        DATA=1;                           //release DATA-line
 756   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
 757   1        { SCK=1;                          //clk for SENSI-BUS
 758   2          if (DATA) val=(val | i);        //read bit  
 759   2          SCK=0;                                       
 760   2        }
 761   1        DATA=!ack;                        //in case of "ack==1" pull down DATA-Line
 762   1        SCK=1;                            //clk #9 for ack
 763   1        _nop_();_nop_();_nop_();          //pulswith approx. 5 us 
 764   1        SCK=0;                                                    
 765   1        DATA=1;                           //release DATA-line
 766   1        return val;
 767   1      }
 768          
 769          //----------------------------------------------------------------------------------
 770          void s_transstart(void)
 771          //----------------------------------------------------------------------------------
 772          // generates a transmission start 
 773          //       _____         ________
 774          // DATA:      |_______|
 775          //           ___     ___
 776          // SCK : ___|   |___|   |______
 777          {  
 778   1         DATA=1; SCK=0;                   //Initial state
 779   1         _nop_();
 780   1         SCK=1;
 781   1         _nop_();
 782   1         DATA=0;
 783   1         _nop_();
 784   1         SCK=0;  
 785   1         _nop_();_nop_();_nop_();
 786   1         SCK=1;
 787   1         _nop_();
 788   1         DATA=1;                 
 789   1         _nop_();
 790   1         SCK=0;                  
 791   1      }
 792          
 793          //----------------------------------------------------------------------------------
 794          void s_connectionreset(void)
 795          //----------------------------------------------------------------------------------
 796          // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
 797          //       _____________________________________________________         ________
C51 COMPILER V7.07   SHTXX_SAMPLE_CODE                                                     04/04/2007 18:18:08 PAGE 14  

 798          // DATA:                                                      |_______|
 799          //          _    _    _    _    _    _    _    _    _        ___     ___
 800          // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
 801          {  
 802   1        unsigned char i; 
 803   1        DATA=1; SCK=0;                    //Initial state
 804   1        for(i=0;i<9;i++)                  //9 SCK cycles
 805   1        { SCK=1;
 806   2          SCK=0;
 807   2        }
 808   1        s_transstart();                   //transmission start
 809   1      }
 810          
 811          //----------------------------------------------------------------------------------
 812          char s_softreset(void)
 813          //----------------------------------------------------------------------------------
 814          // resets the sensor by a softreset 
 815          { 
 816   1        unsigned char error=0;  
 817   1        s_connectionreset();              //reset communication
 818   1        error+=s_write_byte(RESET);       //send RESET-command to sensor
 819   1        return error;                     //error=1 in case of no response form the sensor
 820   1      }
 821          
 822          //----------------------------------------------------------------------------------
 823          char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
 824          //----------------------------------------------------------------------------------
 825          // reads the status register with checksum (8-bit)
 826          { 
 827   1        unsigned char error=0;
 828   1        s_transstart();                   //transmission start
 829   1        error=s_write_byte(STATUS_REG_R); //send command to sensor
 830   1        *p_value=s_read_byte(ACK);        //read status register (8-bit)
 831   1        *p_checksum=s_read_byte(noACK);   //read checksum (8-bit)  
 832   1        return error;                     //error=1 in case of no response form the sensor
 833   1      }
 834          
 835          //----------------------------------------------------------------------------------
 836          char s_write_statusreg(unsigned char *p_value)
 837          //----------------------------------------------------------------------------------
 838          // writes the status register with checksum (8-bit)
 839          { 
 840   1        unsigned char error=0;
 841   1        s_transstart();                   //transmission start
 842   1        error+=s_write_byte(STATUS_REG_W);//send command to sensor
 843   1        error+=s_write_byte(*p_value);    //send value of status register
 844   1        return error;                     //error>=1 in case of no response form the sensor
 845   1      }
 846                                                                     
 847          //----------------------------------------------------------------------------------
 848          char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
 849          //----------------------------------------------------------------------------------
 850          // makes a measurement (humidity/temperature) with checksum
 851          { 
 852   1        unsigned error=0;
 853   1        unsigned int i;
 854   1      
 855   1        s_transstart();                   //transmission start
 856   1        switch(mode){                     //send command to sensor
 857   2          case TEMP   : error+=s_write_byte(MEASURE_TEMP); break;
 858   2          case HUMI   : error+=s_write_byte(MEASURE_HUMI); break;
 859   2          default     : break;         
C51 COMPILER V7.07   SHTXX_SAMPLE_CODE                                                     04/04/2007 18:18:08 PAGE 15  

 860   2        }
 861   1        for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
 862   1        if(DATA) error+=1;                // or timeout (~2 sec.) is reached
 863   1        *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
 864   1        *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
 865   1        *p_checksum =s_read_byte(noACK);  //read checksum
 866   1        return error;
 867   1      }
 868          
 869          //----------------------------------------------------------------------------------
 870          void init_uart()
 871          //----------------------------------------------------------------------------------
 872          //9600 bps @ 11.059 MHz 
 873          {SCON  = 0x52;    
 874   1       TMOD  = 0x20;    
 875   1       TCON  = 0x69;    
 876   1       TH1   = 0xfd;    
 877   1      }
 878          
 879          //----------------------------------------------------------------------------------------
 880          void calc_sth11(float *p_humidity ,float *p_temperature)
 881          //----------------------------------------------------------------------------------------
 882          // calculates temperature [癈] and humidity [%RH] 
 883          // input :  humi [Ticks] (12 bit) 
 884          //          temp [Ticks] (14 bit)
 885          // output:  humi [%RH]
 886          //          temp [癈]
 887          { const float C1=-4.0;              // for 12 Bit
 888   1        const float C2=+0.0405;           // for 12 Bit
 889   1        const float C3=-0.0000028;        // for 12 Bit
 890   1        const float T1=+0.01;             // for 14 Bit @ 5V
 891   1        const float T2=+0.00008;           // for 14 Bit @ 5V 
 892   1      
 893   1        float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit 
 894   1        float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
 895   1        float rh_lin;                     // rh_lin:  Humidity linear
 896   1        float rh_true;                    // rh_true: Temperature compensated humidity
 897   1        float t_C;                        // t_C   :  Temperature [癈]
 898   1      
 899   1        t_C=t*0.01 - 40;                  //calc. temperature from ticks to [癈]
 900   1        rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
 901   1        rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
 902   1        if(rh_true>100)rh_true=100;       //cut if the value is outside of
 903   1        if(rh_true<0.1)rh_true=0.1;       //the physical possible range
 904   1      
 905   1        *p_temperature=t_C;               //return temperature [癈]
 906   1        *p_humidity=rh_true;              //return humidity[%RH]
 907   1      }
 908          
 909          //--------------------------------------------------------------------
 910          float calc_dewpoint(float h,float t)
 911          //--------------------------------------------------------------------
 912          // calculates dew point
 913          // input:   humidity [%RH], temperature [癈]
 914          // output:  dew point [癈]
 915          { float logEx,dew_point;
 916   1        logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
 917   1        dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
 918   1        return dew_point;
 919   1      }
 920          
 921          //----------------------------------------------------------------------------------
C51 COMPILER V7.07   SHTXX_SAMPLE_CODE                                                     04/04/2007 18:18:08 PAGE 16  

 922          void main()
 923          //----------------------------------------------------------------------------------
 924          // sample program that shows how to use SHT11 functions
 925          // 1. connection reset 
 926          // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
 927          // 3. calculate humidity [%RH] and temperature [癈]
 928          // 4. calculate dew point [癈]
 929          // 5. print temperature, humidity, dew point  
 930          
 931          { 
 932   1              
 933   1              uchar i = 0;
 934   1              uchar v = 0;
 935   1              uchar keydata = 0;
 936   1              uint dat_ad = 0;
 937   1              uchar num_ad = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -