PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Cảm biến (http://www.picvietnam.com/forum/forumdisplay.php?f=18)
-   -   Giúp đỡ về SHT11 và 16f877a +LCD (http://www.picvietnam.com/forum/showthread.php?t=14714)

tungds 16-04-2012 11:40 PM

Giúp đỡ về SHT11 và 16f877a +LCD
 
1 Attachment(s)
E có tham khảo được code ở trên mạng, và đã đọc cái datasheet để viết hàm main hiển thị lên lcd nhưng k hiểu sao giá trị nhiệt độ thì đúng mà độ ẩm lại sai.Mong các bác xem hộ file đính kèm ở dưới.e cũng đã tham khảo 1 số code của sht11 trên mạng thì thấy cái sht11 e down về có thiếu 1 hàm nhưng lúc paste và edit thì có lỗi mà mãi e k sửa được.Bác nào xem hộ e ở file đính kèm phía dưới với ạ. File pdf ở trong chính là cái code e tham khảo để so sánh với code của e.Hình ảnh mô phỏng ạ, cái độ ẩm nó bị âm
http://i771.photobucket.com/albums/x...1990/sht11.jpg

tdm 17-04-2012 11:50 AM

Trích:

Nguyên văn bởi tungds (Post 56714)
E có tham khảo được code ở trên mạng, và đã đọc cái datasheet để viết hàm main hiển thị lên lcd nhưng k hiểu sao giá trị nhiệt độ thì đúng mà độ ẩm lại sai.Mong các bác xem hộ file đính kèm ở dưới.e cũng đã tham khảo 1 số code của sht11 trên mạng thì thấy cái sht11 e down về có thiếu 1 hàm nhưng lúc paste và edit thì có lỗi mà mãi e k sửa được.Bác nào xem hộ e ở file đính kèm phía dưới với ạ. File pdf ở trong chính là cái code e tham khảo để so sánh với code của e.Hình ảnh mô phỏng ạ, cái độ ẩm nó bị âm
http://i771.photobucket.com/albums/x...1990/sht11.jpg

SHT11.h
Code:

///////////////////////////////////////////////////////////////////////////////
#define ACK              0
// Command byte values        adr cmd  r/w
#define Reset      0x1e  // 000 1111  0   
#define MeasureTemp 0x03  // 000 0001  1   
#define MeasureHumi 0x05  // 000 0010  1
#define WrStatusReg 0x06  // 000 0011  0
#define RdStatusReg 0x07  // 000 0011  1
// Define Data & Clock Pins
#define Data PIN_B4
#define Sck  PIN_B5

short error, chkCRC, Acknowledge;
int TimeOut,err;

/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
long CalcTempValues(long Lx){
 long value;
 float Fx;

  Fx=0.01*(float)Lx;
  Fx=Fx-40;
  Value=Fx;
  return(value);
}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
long CalcHumiValues(long Lx){
 long Value;
 float Fx, Fy;

  Fx=(float)Lx*(float)Lx;
  Fx=Fx*(-0.0000028);
  Fy=(float)Lx*0.0405;
  Fx=Fx+Fy;
  Fx=Fx-4;
  Value=Fx;
  return(value);
}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
void TransmitStart(){

  output_high(Data);
  output_low(Sck);  delay_us(2);
  output_high(SCK); delay_us(2);
  output_low(Data); delay_us(2);
  output_low(SCK);  delay_us(6);
  output_high(SCK); delay_us(2);
  output_high(Data);delay_us(2);
  output_low(SCK);  delay_us(2);
}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
void RST_Connection(){
 int i;

  output_high(Data);
  for(i=1;i<=10;++i) {
      output_high(SCK); delay_us(2);
      output_low(SCK);  delay_us(2);
  }
  TransmitStart();
}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
void RST_Software(){

}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
int WriteStatReg(int command){

}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
int ReadStatReg(int command){

}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
long ReadValue(){
 byte i, ByteHigh=0, ByteLow=0;
 long Lx;

  for(i=1;i<=8;++i){                    // read high byte VALUE from SHT11
    output_high(SCK);
    delay_us(2);
    shift_left(&ByteHigh,1,input(Data));
    output_low(SCK);
    delay_us(2);
  }
  output_low(Data);
      delay_us(2);
  output_high(SCK);
  delay_us(2);
  output_low(SCK);
  output_float(Data);
  delay_us(2);

  for(i=1;i<=8;++i){                    // read low byte VALUE from SHT11
    output_high(SCK);
    delay_us(2);
    shift_left(&ByteLow,1,input(Data));
    output_low(SCK);
    delay_us(2);
  }
  output_high(Data);
      delay_us(2);
  output_high(SCK);
  delay_us(2);
  output_low(SCK);
  output_float(Data);
  delay_us(2);
  Lx=make16(ByteHigh,ByteLow);
  return(Lx);
}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
void SendCommand(int Command){
 byte i;

  for(i=128;i>0;i/=2){
    if(i & Command) output_high(Data);
    else            output_low(Data);
    delay_us(2);
    output_high(SCK);
    delay_us(2);
    output_low(SCK);
  }
  output_float(Data);
  delay_us(2);
  output_high(SCK);
  delay_us(2);
  output_low(SCK);
}

/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
long ReadTemperature(){
 short Acknowledge;
 long Lx,Value;

  TransmitStart();
  SendCommand(MeasureTemp);
  while(input(Data));
  delay_us(2);
  Lx=ReadValue();                        // Read temperature value
  Value=CalcTempValues(Lx);
  return(Value);
}
/////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
long ReadHumidity(){
 long Lx,Value;

  TransmitStart();
  SendCommand(MeasureHumi);
  while(input(Data));
  delay_us(2);
  Lx=ReadValue();                        // Read humidity value
  Value=CalcHumiValues(Lx);
  return(Value);

}
//-----------------------------------------------
float calc_dewpoint(float h,float t)
//-----------------------------------------------
// calculates dew point
// input:  humidity [%RH], temperature [°C]
// output:  dew point [°C]
{ float k,dew_point ;
 
  k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
  dew_point = 243.12*k/(17.62-k);
  return dew_point;
}

main.c
Code:

#include <16F877A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock =20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#include <LCD.h>   
#include <math.h>
#include <sht11.h>

int8 temp=34; 
lcd_init();
int R;
float fRh_lin;
float fRh_true;
float fTemp_true;
float fDew_point;
long lValue_rh;
long lValue_temp;
void main()
{
  port_b_pullups(true);
  lcd_init(); 
  RST_Connection();
  delay_ms(2);
 
  lcd_send_byte(0,0x80);
  printf(lcd_putc "SHT15 Humidity &");
  lcd_send_byte(0,0xC0); 
  printf(lcd_putc " Temperrature  ");
 
  while (TRUE)
  {
      fTemp_true=ReadTemperature();
      fRh_true=ReadHumidity();
      fDew_point=calc_dewpoint(fRh_true,fTemp_true);
      printf("T=%3.2f C\r\n",fTemp_true);
      printf("H=%3.2f%%\r\n",fRh_true);
      printf("Dew Point: %3.6fC\r\n",fDew_point);
      delay_ms(1000);
     
      lcd_send_byte(0,0x80); 
      printf(lcd_putc "T:%3.2f  ",fTemp_true);
      printf(lcd_putc "H:%3.3f  ",fRh_true);
      lcd_send_byte(0,0xC0); 
      printf(lcd_putc "D/P:%3.6fC",fDew_point);
      delay_ms(1000);
     
    }
 
}


tungds 20-04-2012 12:14 PM

hay quá, thanks bác nhiều nhe :X


Múi giờ GMT. Hiện tại là 03:28 PM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam