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

📄 sht10.c

📁 SHT10温湿度测试
💻 C
字号:
#include "reg52.h"
#include "intrins.h" //Keil library (is used for _nop()_ operation)  
#include "math.h"    //Keil library  
#include "stdio.h"
#include "LED.h"
#include "44KEY.h"

#define uchar unsigned char
#define uint unsigned int

sbit DATA      = P3^6;         //定义通讯数据端口
sbit SCK       = P3^7;

typedef union  
{ unsigned int i;      //定义了两个共用体
  float f; 
} value; 

enum {TEMP,HUMI};      //TEMP=0,HUMI=1

uint tempvalue,tempvalue2;
uint flag;
float wenduf,shiduf;
unsigned int wendu,shidu;
 
#define noACK 0             //用于判断是否结束通讯
#define ACK   1             //结束数据传输
                            //adr  command  r/w 
#define STATUS_REG_W 0x06   //000   0011    0 
#define STATUS_REG_R 0x07   //000   0011    1 
#define MEASURE_TEMP 0x03   //000   0001    1 
#define MEASURE_HUMI 0x05   //000   0010    1 
#define RESET        0x1e   //000   1111    0 

/****************定义函数****************/
void s_transstart(void);               //启动传输函数
void s_connectionreset(void);          //连接复位函数
char s_write_byte(unsigned char value);//DHT90写函数
char s_read_byte(unsigned char ack);   //DHT90读函数
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿
void calc_dht90(float *p_humidity ,float *p_temperature);//温湿度补偿
void delay_n10us(uint n);                     //延时函数

static void mDelay(unsigned int j)
{
    uint i;
	for(;j>0;j--)
	{
	    for(i=124;i>0;i--)
		{;}
	}
}

/*-------------------------------------- 
;模块名称:delay_n10us(); 
;功    能:延时函数,延时约n个10us
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.1(函数版本Function Version)
;修改日期:2008.08.26
;修改说明:修改为较精确的延时函数,"_nop_()"延时1us@12M晶振
;-------------------------------------*/
void delay_n10us(uint n)  //延时n个10us@12M晶振
{       
        uint i;           
        for(i=n;i>0;i--)    
        {
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); 
		}
}   

/*-------------------------------------- 
;模块名称:s_transstart(); 
;功    能:启动传输函数
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/  
void s_transstart(void) 
// generates a transmission start  
//       _____         ________ 
// DATA:      |_______| 
//           ___     ___ 
// SCK : ___|   |___|   |______ 
{   
   DATA=1; SCK=0;                   //Initial state 
   _nop_(); 
   SCK=1; 
   _nop_(); 
   DATA=0; 
   _nop_(); 
   SCK=0;   
   _nop_();_nop_();_nop_(); 
   SCK=1; 
   _nop_(); 
   DATA=1;        
   _nop_(); 
   SCK=0;        
} 

/*-------------------------------------- 
;模块名称:s_connectionreset(); 
;功    能:连接复位函数
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/ 
void s_connectionreset(void) 
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart 
//       _____________________________________________________         ________ 
// DATA:                                                      |_______| 
//          _    _    _    _    _    _    _    _    _        ___     ___ 
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______ 
{   
  unsigned char i;  
  DATA=1; SCK=0;                    //Initial state 
  for(i=0;i<9;i++)                  //9 SCK cycles 
  { 
    SCK=1;
    SCK=0; 
  } 
  s_transstart();                   //transmission start 
} 

/*-------------------------------------- 
;模块名称:s_write_byte(); 
;功    能:DHT90写函数
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/ 
char s_write_byte(unsigned char value) 
//---------------------------------------------------------------------------------- 
// writes a byte on the Sensibus and checks the acknowledge  
{  
  unsigned char i,error=0;   
  for (i=0x80;i>0;i/=2)             //shift bit for masking 
  {  
    if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS 
    else DATA=0;                         
    SCK=1;                          //clk for SENSI-BUS 
    _nop_();_nop_();_nop_();        //pulswith approx. 3 us     
    SCK=0; 
  } 
  DATA=1;                           //release DATA-line 
  SCK=1;                            //clk #9 for ack  
  error=DATA;                       //check ack (DATA will be pulled down by DHT90),DATA在第9个
 
  _nop_();_nop_();_nop_();
  SCK=0;
  DATA=1;                           //release DATA-line 
  return error;                     //error=1 in case of no acknowledge //返回:0成功,1失败
} 
 

/*-------------------------------------- 
;模块名称:s_read_byte(); 
;功    能:DHT90读函数
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/ 
char s_read_byte(unsigned char ack)  
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"  
{  
  unsigned char i,val=0; 
  DATA=1;                           //release DATA-line 
  for (i=0x80;i>0;i/=2)             //shift bit for masking 
  { SCK=1;                          //clk for SENSI-BUS 
    if (DATA) val=(val | i);        //read bit   
	_nop_();_nop_();_nop_();        //pulswith approx. 3 us
    SCK=0;              
  } 
  if(ack==1)DATA=0;                 //in case of "ack==1" pull down DATA-Line 
  else DATA=1;                      //如果是校验(ack==0),读取完后结束通讯
  _nop_();_nop_();_nop_();          //pulswith approx. 3 us 
  SCK=1;                            //clk #9 for ack 
  _nop_();_nop_();_nop_();          //pulswith approx. 3 us  
  SCK=0;                 
  _nop_();_nop_();_nop_();          //pulswith approx. 3 us 
  DATA=1;                           //release DATA-line 
  return val; 
} 
 
/*-------------------------------------- 
;模块名称:s_measure(); 
;功    能:测量温湿度函数
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/ 
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) 
// makes a measurement (humidity/temperature) with checksum 
{  
  unsigned error=0; 
  unsigned int i; 
 
  s_transstart();                   //transmission start 
  switch(mode){                     //send command to sensor 
    case TEMP  : error+=s_write_byte(MEASURE_TEMP); break; 
    case HUMI  : error+=s_write_byte(MEASURE_HUMI); break; 
    default     : break;    
  } 
  for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement 
  if(DATA) error+=1;                // or timeout (~2 sec.) is reached 
  *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB) 
  *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB) 
  *p_checksum =s_read_byte(noACK);  //read checksum 
  return error; 
} 
 
/*-------------------------------------- 
;模块名称:calc_dht90(); 
;功    能:温湿度补偿函数
;占用资源:--
;参数说明:--
;创建日期:2008.08.15 
;版    本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/ 
void calc_dht90(float *p_humidity ,float *p_temperature)
// calculates temperature [C] and humidity [%RH] 
// input :  humi [Ticks] (12 bit) 
//          temp [Ticks] (14 bit)
// output:  humi [%RH]
//          temp [C]
{ const float C1=-4.0;              // for 12 Bit
  const float C2=+0.0405;           // for 12 Bit
  const float C3=-0.0000028;        // for 12 Bit
  const float T1=+0.01;             // for 14 Bit @ 5V
  const float T2=+0.00008;           // for 14 Bit @ 5V 

  float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit 
  float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  float rh_lin;                     // rh_lin:  Humidity linear
  float rh_true;                    // rh_true: Temperature compensated humidity
  float t_C;                        // t_C   :  Temperature [C]

  t_C=t*0.01 - 40;                  //calc. temperature from ticks to [C]
  rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
  if(rh_true>100)rh_true=100;       //cut if the value is outside of
  if(rh_true<0.1)rh_true=0.1;       //the physical possible range

  *p_temperature=t_C;               //return temperature [C]
  *p_humidity=rh_true;              //return humidity[%RH]
}

//*********************第二部分DHT90设置   END****************************************

void Display1(tempvalue) //在数码管上显示温度          
{   
    uint i,temp;
	temp = tempvalue;
    sel1= temp%10;
    temp= temp/10;
    sel2= temp%10;
    temp= temp/10;
    sel3= temp%10;
    temp= temp/10;
    sel4= temp%10;
	sel8= 10;
    while(i<=100)
    {
        led_showdian();
	    i++; 
    }
	   
}

void Display2(tempvalue2) //在数码管上显示湿度          
{   
    uint i,temp;
	temp = tempvalue2;
    sel1= temp%10;
    temp= temp/10;
    sel2= temp%10;
    temp= temp/10;
    sel3= temp%10;
    temp= temp/10;
    sel4= temp%10;
	sel8= 11;
    while(i<=100)
    {
        led_showdian();
	    i++; 
    }
	   
}
//*********主函数*****************
void SHT10()
{
	value humi_val,temp_val;
        unsigned char error,checksum; 
       
        	
        s_connectionreset(); 

        delay_n10us(20000);     //延时0.2s

        while(1) 
        { error=0; 
          error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //measure humidity 
          error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //measure temperature 
          if(error!=0) s_connectionreset();                 //in case of an error: connection  
          else 
          { humi_val.f=(float)humi_val.i;                   //converts integer to float
            temp_val.f=(float)temp_val.i;                   //converts integer to float
            calc_dht90(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
	        //wenduf=temp_val.f; 
            //shiduf=humi_val.f;

            wendu=10*temp_val.f; 
            shidu=10*humi_val.f;

			if(flag==1)               //如果测量的是温度
           {
		        Display1(wendu);
            }
			else if(flag==2)               //如果测量的是湿度
           { 
                Display2(shidu);
            } 
			else if(flag==3)               //如果上位机测量
           { 
                //wenduf=temp_val.f; 
                //shiduf=humi_val.f;
				break;
            } 
			
			KeyScan();
            KeyFree();
			if(key==1)        //如果要退出   
            {
	            mDelay(100);
                if(key==1)
	            {
                        key=0;
		                P0=0xff;
                        wendu = 0;
						shidu = 0;
				        P0=0xff;
                        flag=0;
						//wenduf=temp_val.f; 
                        //shiduf=humi_val.f;
						break;
                  }
                 }   
           delay_n10us(80000);                                //延时约0.8s
        }
}        
}  

⌨️ 快捷键说明

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