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

📄 r_8.c

📁 温湿度传感器读写程序
💻 C
📖 第 1 页 / 共 2 页
字号:
        const_ss_flag = 1;
		counter = 0;
      }
    else if((kkk&K_SET)==0)
	 { // set
      set_start++;
	  counter = 0;
      if(set_start > 1)
        {
          rh_seting = 1;
	      rh_ss_flag = !rh_ss_flag;
          set_humi = EEPROMread(SET_HUMI_DZ);
          if(set_humi > 99) set_humi = 99;
		  disp_num(set_humi);        
          set_start = 0;
		}
	 }
    if((kkk&K_SET)==0)
	  { 
	   kkk = read_key();
       while((kkk&0x16) != 0x16)
        {
         WDR();
         kkk = read_key();
 		 counter = 0;
        }
      }
}




//-----------------------------------------------------------------------------
//1、写 一个字节并检查状态
// writes a byte on the Sensibus and checks the acknowledge  
//-----------------------------------------------------------------------------
char s_write_byte(char dat) 
{  
  char i,error=0;   
  
  for (i=0x80;i>0;i/=2)             //shift bit for masking 
  { if (i & dat) DATA_H;          //masking value with i , write to SENSI-BUS 
    else DATA_L;                         
    SCK_H;                          //clk for SENSI-BUS 
    asm("nop");  asm("nop");  asm("nop");
    SCK_L; 
  } 
  DATA_H;                           //release DATA-line 
  SCK_H;   
  
  DDRB &=~ BIT(PB5);                //clk #9 for ack  
  delay_ms(1);                      // 这条不能少 
  error=(PINB & 0x20);              //check ack (DATA will be pulled down by SHT11) 
  DDRB |= BIT(PB5);

  SCK_L;   
 return error;                     //error=1 in case of no acknowledge 
} 
 
//---------------------------------------------------------------------------
//2、读 一个字节并检查状态
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"  
//---------------------------------------------------------------------------
char s_read_byte(char ack) 
{  
  char i,val = 0; 
  DATA_H;                           //release DATA-line 

  DDRB &=~ BIT(PB5);
  for (i=0x80;i>0;i/=2)             //shift bit for masking 
  { SCK_H;                          //clk for SENSI-BUS 
	if (PINB & 0x20) val = (val | i); //read bit   
    SCK_L;              
  } 

  DDRB |= BIT(PB5);
  //delay_ms(1);
  if(ack==1) DATA_L;                //in case of "ack==1" pull down DATA-Line 
  SCK_H;                            //clk #9 for ack 
  asm("nop");  asm("nop");  asm("nop");
  SCK_L;                 
  DATA_H;                           //release DATA-line 
  return val; 
} 
 
//-----------------------------------------------------------------------------
//3、启动传输
// generates a transmission start  
//       _____         ________ 
// DATA:      |_______| 
//           ___     ___ 
// SCK : ___|   |___|   |______ 
//-----------------------------------------------------------------------------
void s_transstart(void) 
{   
   DATA_H; 
   SCK_L;                   //Initial state 
   asm("nop");
   SCK_H; 
   asm("nop");
   DATA_L; 
   asm("nop");
   SCK_L;   
   asm("nop");   asm("nop");   asm("nop");
   SCK_H; 
   asm("nop");
   DATA_H;        
   asm("nop");
   SCK_L;        
} 
 
//-----------------------------------------------------------------------------
// 4、通讯 RESET
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart 
//       _____________________________________________________         ________ 
// DATA:                                                      |_______| 
//          _    _    _    _    _    _    _    _    _        ___     ___ 
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______ 
//-----------------------------------------------------------------------------
void s_connectionreset(void) 
{   
  char i;  
  DATA_H; 
  SCK_L;                    //Initial state 
  for(i=0;i<9;i++)          //9 SCK cycles 
  { SCK_H; 
    SCK_L; 
  } 
  s_transstart();          //transmission start 
} 
 
//-----------------------------------------------------------------------------
// 5、软件复位 SHT
// resets the sensor by a softreset  
//-----------------------------------------------------------------------------
char s_softreset(void) 
{  
  char error = 0;   
  
  s_connectionreset();              //reset communication 
  error += s_write_byte(RESET);       //send RESET-command to sensor 
  return error;                     //error=1 in case of no response form the sensor 
} 
 
//-----------------------------------------------------------------------------
// 6、读寄存器状态
// reads the status register with checksum (8-bit) 
//-----------------------------------------------------------------------------
char s_read_statusreg(char *p_value, char *p_checksum) 
{  
  char error=0;
 
  s_transstart();                     //transmission start 
  error = s_write_byte(STATUS_REG_R); //send command to sensor 
  *p_value = s_read_byte(ACK);        //read status register (8-bit) 
  *p_checksum = s_read_byte(noACK);   //read checksum (8-bit)   
  return error;                       //error=1 in case of no response form the sensor 
} 
 
//-----------------------------------------------------------------------------
// 7、写寄存器状态
// writes the status register with checksum (8-bit) 
//-----------------------------------------------------------------------------
char s_write_statusreg(char  *p_value) 
{  
  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 
} 
                   
//-----------------------------------------------------------------------------
// 8、测量温湿度
// makes a measurement (humidity/temperature) with checksum 
//-----------------------------------------------------------------------------
char s_measure(char *p_value, char *p_checksum, char mode) 
{  
  char 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;    
  } 

  DDRB &=~ BIT(PB5);
  for (i=0; i<65535; i++)
   {
    WDR();
	if ((PINB & 0x20)==0) break; //wait until sensor has finished the measurement 
   }

  if ((PINB & 0x20) > 0) error+=1;  // or timeout (~2 sec.) is reached 
  DDRB |= BIT(PB5);

  *(p_value+1)  = s_read_byte(ACK);    //read the first byte (MSB) 
  *(p_value)    = s_read_byte(noACK);    //read the second byte (LSB) 
 // *p_checksum =s_read_byte(noACK);  //read checksum 
  return error; 
} 
 
//-----------------------------------------------------------------------------
// 9、温、湿度修正
// calculates temperature [C] and humidity [%RH]  
// input :  humi [Ticks] (12 bit)  
//          temp [Ticks] (14 bit) 
// output:  humi [%RH] 
//          temp [C] 
//-----------------------------------------------------------------------------
//void calc_sth11(float *p_humidity ,float *p_temperature) // 指针不能用float
// 因为是12位,占用双字节,故用 指针为 int 类型,否则不正确!
void calc_sth11(unsigned int *p_humidity ,unsigned int *p_temperature)
{
  float RH = *p_humidity;             // rh: 湿度 Humidity [Ticks] 12 Bit
  float T  = *p_temperature;          // t:  温度 Temperature [Ticks] 14 Bit
  float RH_line;                       // rh_lin:  Humidity linear
  float RH_true;                      // rh_true: Temperature compensated humidity
  float T_C;                          // t_C   :  温度 Temperature [ C]

  WDR();
  T_C = T*0.01 - 40;                 // 温度calc. Temperature from ticks to [ C]

// 相对湿度的非线性  补偿()
// 为了补偿湿度传感器的非线性以获取准确数据,可使用如下公式修正
  RH_line  = C3*RH*RH + C2*RH + C1;  //calc. Humidity from ticks to [%RH]主要部分
  RH_line  = RH_line + 6;   
// 相对湿度的环境温度 补偿

  RH_true = (T_C-25)*(T1+T2*RH) + RH_line + set_const;   //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] 
  WDR();
} 

//-----------------------------------------------------------------------------
// 10 计算露点
// calculates dew point 
// input:   humidity [%RH], temperature [C] 
// output:  dew point [C] 
//-----------------------------------------------------------------------------
/*
unsigned long calc_dewpoint(unsigned long h, unsigned long t) 
{ float logEx,dew_point ; 

  logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2) ; 
  dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5 - logEx) ;
  return dew_point; 
} 
*/

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
 void   disp_num(char num)
{
    char num_h, num_l;
    
    if(num > 100)
	  {
	    a1_h; b1_h; c1_h; d1_h; e1_h; f1_h; g1_h;
	    a2_h; b2_h; c2_h; d2_h; e2_h; f2_h; g2_h;
        return;
	  }
	num = htod(num);
    num_h = (num & 0xf0) >> 4;
    num_l = num & 0x0f;
    switch(num_l)
     {
	  case 0:
	    a2_l; b2_l; c2_l; d2_l; e2_l; f2_l; g2_h;
        break;
	  case 1:
	    a2_h; b2_l; c2_l; d2_h; e2_h; f2_h; g2_h;
        break;
	  case 2:
	    a2_l; b2_l; c2_h; d2_l; e2_l; f2_h; g2_l;
        break;
	  case 3:
	    a2_l; b2_l; c2_l; d2_l; e2_h; f2_h; g2_l;
        break;
	  case 4:
	    a2_h; b2_l; c2_l; d2_h; e2_h; f2_l; g2_l;
        break;
	  case 5:
	    a2_l; b2_h; c2_l; d2_l; e2_h; f2_l; g2_l;
        break;
	  case 6:
	    a2_l; b2_h; c2_l; d2_l; e2_l; f2_l; g2_l;
        break;
	  case 7:
	    a2_l; b2_l; c2_l; d2_h; e2_h; f2_h; g2_h;
        break;
	  case 8:
	    a2_l; b2_l; c2_l; d2_l; e2_l; f2_l; g2_l;
        break;
	  case 9:
	    a2_l; b2_l; c2_l; d2_l; e2_h; f2_l; g2_l;
        break;
	 }
    switch(num_h)
     {
	  case 0:
	    a1_l; b1_l; c1_l; d1_l; e1_l; f1_l; g1_h;
        break;
	  case 1:
	    a1_h; b1_l; c1_l; d1_h; e1_h; f1_h; g1_h;
        break;
	  case 2:
	    a1_l; b1_l; c1_h; d1_l; e1_l; f1_h; g1_l;
        break;
	  case 3:
	    a1_l; b1_l; c1_l; d1_l; e1_h; f1_h; g1_l;
        break;
	  case 4:
	    a1_h; b1_l; c1_l; d1_h; e1_h; f1_l; g1_l;
        break;
	  case 5:
	    a1_l; b1_h; c1_l; d1_l; e1_h; f1_l; g1_l;
        break;
	  case 6:
	    a1_l; b1_h; c1_l; d1_l; e1_l; f1_l; g1_l;
        break;
	  case 7:
	    a1_l; b1_l; c1_l; d1_h; e1_h; f1_h; g1_h;
        break;
	  case 8:
	    a1_l; b1_l; c1_l; d1_l; e1_l; f1_l; g1_l;
        break;
	  case 9:
	    a1_l; b1_l; c1_l; d1_l; e1_h; f1_l; g1_l;
        break;
	 }
}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
void   disp_const(signed num)
{
    char num_int;
	if(num > 100)
      {
        a1_h; b1_h; c1_h; d1_h; e1_h; f1_h; g1_h;
	    a2_h; b2_h; c2_h; d2_h; e2_h; f2_h; g2_h;
       return;
	  }
    
    if(num < 0)
	  {
       a1_h; b1_h; c1_h; d1_h; e1_h; f1_h; g1_l;
      }
    else
      {
       a1_h; b1_h; c1_h; d1_h; e1_h; f1_h; g1_h;
      }
    num_int = abs(num);
	switch(num_int)
     {
	  case 0:
	    a2_l; b2_l; c2_l; d2_l; e2_l; f2_l; g2_h;
        break;
	  case 1:
	    a2_h; b2_l; c2_l; d2_h; e2_h; f2_h; g2_h;
        break;
	  case 2:
	    a2_l; b2_l; c2_h; d2_l; e2_l; f2_h; g2_l;
        break;
	  case 3:
	    a2_l; b2_l; c2_l; d2_l; e2_h; f2_h; g2_l;
        break;
	  case 4:
	    a2_h; b2_l; c2_l; d2_h; e2_h; f2_l; g2_l;
        break;
	  case 5:
	    a2_l; b2_h; c2_l; d2_l; e2_h; f2_l; g2_l;
        break;
	  case 6:
	    a2_l; b2_h; c2_l; d2_l; e2_l; f2_l; g2_l;
        break;
	  case 7:
	    a2_l; b2_l; c2_l; d2_h; e2_h; f2_h; g2_h;
        break;
	  case 8:
	    a2_l; b2_l; c2_l; d2_l; e2_l; f2_l; g2_l;
        break;
	  case 9:
	    a2_l; b2_l; c2_l; d2_l; e2_h; f2_l; g2_l;
        break;
	 }
}

char  htod(char hex_data)
{
    char   bcd_data, temp1;
    char cc;

    bcd_data = temp1 = 0;
    cc = 1;
    while(cc)
       {
        if(temp1 == hex_data) cc = 0;
        else
          {
            bcd_data++;
            temp1++;
            if((bcd_data & 0x0f) > 9)
              bcd_data = (bcd_data&0xf0) + 0x10;
          }
       }
    return(bcd_data);
}


//===========================================================     
//               
//===========================================================     
void delay_ms(unsigned int a)          //DELAY TIME ms
{
    unsigned int i,j;
    for(i=0; i<a; i++)
	 {
      WDR();
	  for(j=1; j<140; j++);
	 }
}



⌨️ 快捷键说明

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