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

📄 sht11.c

📁 AVR atmega64串口驱动
💻 C
字号:
#define SHT11_c 
#include "main.h" 



char s_write_byte(unsigned char value) 

{  

  unsigned char i,error=0;   
  for (i=0x80;i>0;i/=2)             
  { if (i&value) 
  { 
    SETDATA( );     
    
        } 
        else  
        { 
         
        CLRDATA( );  
        }                        
    SETSCK( );                      
    SHTyanshi( );         
    CLRSCK( ); 
  } 
  SETDATA( );                       
  SHTyanshi( ); 
  SETSCK( );                         
   
  SHTyanshi( ); 

   DDRG&=~(1<<PG3); 
  abb: if((PING&0x08)==0x00)error+=1;            
  else   goto abb; 
   
   DDRG|=(1<<PG3); 
  
                     
  CLRSCK( );  
  SHTyanshi( );  
     
  return error;                     
} 


char s_read_byte( ) 
{  
  unsigned char i,val=0; 
  SETDATA( );                    
  SHTyanshi( ); 
  for (i=0x80;i>0;i/=2)         
  {  
   SETSCK( );  
   SHTyanshi( );  
                                
   DDRG&=~(1<<PG3); 
   if((PING&0x08)==0x08)val=(val|i);      
   DDRG|=(1<<PG3); 

   CLRSCK( );  
   SHTyanshi( ); 
  } 
  SHTyanshi( ); 
  CLRDATA( );                    
  SHTyanshi( ); 
  SETSCK( );                    
   
  SHTyanshi( ); 

  CLRSCK( ); 
  SHTyanshi( );                                                     
  SETDATA( ); 
  return val; 
} 




void s_transstart(void) 

{   
   SETDATA( );SHTyanshi( );  
   CLRSCK( ); SHTyanshi( ); 
   SETSCK( ); SHTyanshi( ); 
   CLRDATA( );SHTyanshi( ); 

   CLRSCK( ); SHTyanshi( ); 
   SETSCK( ); SHTyanshi( ); 
   SETDATA( );SHTyanshi( ); 

   CLRSCK( );                    
} 


void s_connectionreset(void) 

{   
  unsigned char i;  
  SETDATA( ); SHTyanshi( ); CLRSCK( );                    
  for(i=0;i<9;i++)                  
  { SETSCK( ); 
    SHTyanshi( ); 
        SHTyanshi( ); 
    CLRSCK( ); 
  } 
  s_transstart();                    
} 


char s_softreset(void) 

{  
  unsigned char error=0;   
  s_connectionreset();               
  error+=s_write_byte(RESET);        
  return error;                      
} 


char s_read_statusreg( ) 
{  
  unsigned char error=0; 
  s_transstart();                   //transmission start 
  error=s_write_byte(STATUS_REG_R); //send command to sensor 
  wendu1=s_read_byte( );        //read status register (8-bit) 
  wenxiao=s_read_byte( );   //read checksum (8-bit)   
  return error;                     //error=1 in case of no response form the sensor 
} 


char s_write_statusreg(unsigned char *p_value) 

{  
  unsigned char error=0; 
  s_transstart();                   //transmission start 
  error+=s_write_byte(STATUS_REG_W);//send command to sensor 
  error+=s_write_byte(*p_value);    //send value of status register 
  return error;                     //error>=1 in case of no response form the sensor 
} 
                                                             

char s_measurewen( ) 
{  
  unsigned error=0; 
  unsigned char wan;  

  s_transstart();                   //transmission start 
     
   error+=s_write_byte(MEASURE_TEMP);  
   DDRG&=~(1<<PG3); 
  abb: if((PING&0x08)==0x00)error+=1;            
  else   goto abb; 
    DDRG|=(1<<PG3); 

  wendu1 =s_read_byte( );    //read the first byte (MSB) 
  wendu2=s_read_byte( );    //read the second byte (LSB) 
  wenxiao =s_read_byte( );  //read checksum 
  
  wenduint=wendu1*256+wendu2; 
  return error; 
  } 


char s_measureshi( ) 
{  
  unsigned error=0; 
  unsigned char wan; 
  s_transstart();                    
  error+=s_write_byte(MEASURE_HUMI); 
  DDRG&=~(1<<PG3); 
  abb: if((PING&0x08)==0x00)error+=1;            
  else   goto abb; 
  DDRG|=(1<<PG3); 
  shidu1 =s_read_byte( );    //read the first byte (MSB) 
  shidu2=s_read_byte( );    //read the second byte (LSB) 
  shixiao =s_read_byte( );  //read checksum 
  shiduint=shidu1*256+shidu2; 
  //shiduint=65040; 
  return error; 
} 


void calc_sth11( ) 
{  
  const double C1=-4.0;               
  const double C2=+0.0405;            
  const double C3=-0.0000028;         
  const double T1=+0.01;              
  const double T2=+0.00008;                    
  t_C=wenduf1*0.01 - 40;                  
  rh_lin=C3*shiduf1*shiduf1 + C2*shiduf1 + C1;     
  rh_true=(t_C-25)*(T1+T2*shiduf1)+rh_lin;   
  if(rh_true>100)rh_true=100;       
  if(rh_true<0.1)rh_true=0.1;        
  wenduf2=t_C;                  
  shiduf2=rh_true;               
} 



void adad() 
{  
        s_connectionreset(); 
        error=0; 
   

        error+=s_measureshi( );       
        error+=s_measurewen( );       
        {  
        shiduf1=(double)shiduint;   
        wenduf1=(double)wenduint;   
        calc_sth11( );             
        wendu3=(uint)wenduf2; 
        wenduf3=modf(wenduf2,weng); 
        wendu4=(uchar)(wenduf3*10); 
        wendu=wendu3*10+wendu4; 
        shidu=(uint)shiduf2; 
        } 
  }  
void SHTyanshi( ) 
{ 
asm("nop");asm("nop");asm("nop");asm("nop"); 
asm("nop");asm("nop");asm("nop");asm("nop"); 
asm("nop");asm("nop");asm("nop");asm("nop"); 
asm("nop");asm("nop");asm("nop");asm("nop");  
asm("nop");asm("nop");asm("nop");asm("nop"); 
asm("nop");asm("nop");asm("nop");asm("nop");  
asm("nop");asm("nop");asm("nop");asm("nop"); 
asm("nop");asm("nop");asm("nop");asm("nop");  
asm("nop");asm("nop");asm("nop");asm("nop"); 
asm("nop");asm("nop");asm("nop");asm("nop");  
asm("nop");asm("nop");asm("nop");asm("nop");  
asm("nop");asm("nop");asm("nop");asm("nop"); 
} 


⌨️ 快捷键说明

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