原创 读出SHT10

2008-7-30 16:25 2212 2 2 分类: MCU/ 嵌入式

程序是在学习板上完成的,用的是1602显示…………


#include <reg52.h> //Microcontroller specific library, e.g. port definitions
#include <intrins.h> //Keil library (is used for _nop()_ operation) 
#include <math.h>    //Keil library 
#include <stdio.h>   //Keil library


#define uchar unsigned char
#define uint unsigned  int


sbit DATA  =P1^2; //定义通讯数据端口
sbit SCK  =P1^3; //定义通讯时钟端口


sbit dula="P2"^6;
sbit wela="P2"^7;
sbit rs="P3"^5;
sbit lcden="P3"^4;
sbit led="P2"^5;


#define noACK 0       //继续传输数据,用于判断是否结束通讯
#define ACK   1       //结束数据传输;


#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 init_uart(void);
void s_connectionreset(void);
void s_transstart(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
char s_write_byte(unsigned char value);
char s_read_byte(unsigned char ack);
void calc_sth11(float *p_humidity ,float *p_temperature);
float calc_dewpoint(float h,float t);
void delay (unsigned int time);


void delay_lcd(uint z)  //lcd的延时子程序
{
 uint x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}



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


enum {TEMP,HUMI};


//----------------------------------------------------------------------------------
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. 5 us   
    SCK="0";
  }
  DATA="1";                           //release DATA-line
  SCK="1";                            //clk #9 for ack
  error="DATA";                       //check ack (DATA will be pulled down by SHT11)
  SCK="0";       
  return error;                     //error=1 in case of no acknowledge
}


//----------------------------------------------------------------------------------
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 
    SCK="0";       
  }
  DATA=!ack;                        //in case of "ack==1" pull down DATA-Line
  SCK="1";                            //clk #9 for ack
  _nop_();_nop_();_nop_();          //pulswith approx. 5 us
  SCK="0";         
  DATA="1";                           //release DATA-line
  return val;
}



void s_transstart(void)
// 启动传输
//       _____         ________
// 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";    
}


//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// 连接复位;
//       _____________________________________________________         ________
// 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
}



         
//----------------------------------------------------------------------------------
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;
}


//----------------------------------------------------------------------------------
void init_uart()
//----------------------------------------------------------------------------------
//9600 bps @ 11.059 MHz
{SCON  = 0x52;   
 TMOD  = 0x20;   
 TCON  = 0x69;  
 TH1   = 0xfd;   
}


//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input :  humi [Ticks] (12 bit)
//          temp [Ticks] (14 bit)
// output:  humi [%RH]
//          temp [癈]
{ 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 [癈]


  t_C=t*0.01 - 40;                  //calc. temperature from ticks to [癈]
  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 [癈]
  *p_humidity=rh_true;              //return humidity[%RH]
}


//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input:   humidity [%RH], temperature [癈]
// output:  dew point [癈]
{ 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 write_com(uchar com)  //写指令
{
 rs=0;
 lcden=0;
 P0=com;
 delay_lcd(5);
 lcden=1;
 delay_lcd(5);
 lcden=0; 
}


void write_date(uchar date)  //写数据
{
 rs=1;
 lcden=0;
 P0=date;
 delay_lcd(5);
 lcden=1;
 delay_lcd(5);
 lcden=0; 
}


void lcd_init()   //初始化
{
 dula=0;
 wela=0;
 led=0;
 lcden=0;
 write_com(0x38);  //显示模式设置
 write_com(0x0c);
 write_com(0x06);
 write_com(0x01); //清屏
 write_com(0x80);
}


//*******显示一个字符函数*********


void LCD_disp_char(uchar x,uchar y,uchar dat)
{
  uchar address;
     if(y==1)
              address="0x80"+x;
     else
              address="0xc0"+x;
     write_com(address);
     write_date(dat);
}


void GotoXY(unsigned char x, unsigned char y)  //显示列x,行y
{
 uchar address;
     if(y==1)
              address="0x80"+x;
     else
              address="0xc0"+x;


     write_com(address);
}


//将字符输出到液晶显示
void Print(unsigned char *str)
{
 while(*str!='\0')
 
  { write_date(*str);  
 str++;
  }  
}



void main()
// 示例程序,完成如下功能
// 1. 复位
// 2. 湿度的12测量以及温度的14位精度测量
// 3. 补偿及修正温湿度
// 4. 计数并得出绝对湿度


{ value humi_val,temp_val;
  float dew_point;
  unsigned char error,checksum;
 uint wendu;
 uint shidu;


  init_uart();
  s_connectionreset();
 lcd_init();
  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 reset
    else
    {
  humi_val.f=(float)humi_val.i;                   //converts integer to float
       temp_val.f=(float)temp_val.i;                   //converts integer to float
       calc_sth11(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
       dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point


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



   LCD_disp_char(7,1,wendu/100+'0');   
   LCD_disp_char(8,1,(wendu%100)/10+'0');
   LCD_disp_char(10,1,(wendu%100)%10+'0');
   GotoXY(1,1);
            Print("TEMP");
   GotoXY(9,1);
            Print(".");
   GotoXY(12,1);
            Print("C"); //在液晶上显示 字母C


   LCD_disp_char(7,2,shidu/100+ '0');   
   LCD_disp_char(8,2,(shidu%100)/10+'0');
   LCD_disp_char(10,2,(shidu%100)%10+'0');
   GotoXY(1,2);
            Print("HUMI");
   GotoXY(9,2);
            Print(".");
   GotoXY(12,2);
           Print("%"); //在液晶上显示 字母%
    }
  }
}


 

文章评论0条评论)

登录后参与讨论
我要评论
0
2
关闭 站长推荐上一条 /3 下一条