จำหน่ายและให้คำปรึกษาเกี่ยวกับการใช้งานบอร์ดไมโครคอนโทรลเลอร์ Arduino, Shield for arduino, Module และ Sensor รุ่นต่างๆ

ช่วยดูปัญหาที่เกิดจาก sht11 ให้หน่อยครับ

กลุ่มวงจรที่ใช้ Microcontroller Arduino , PIC , AVR ,ETC.

Moderator: gang

frankula
Rookie
Rookie
โพสต์: 1
ลงทะเบียนเมื่อ: 17/02/2011 11:08 pm

ช่วยดูปัญหาที่เกิดจาก sht11 ให้หน่อยครับ

โพสต์โดย frankula » 17/02/2011 11:23 pm

พอดีผมได้ลองต่อ sht11 กับบอร์ด mcs51 P89V51RD2 แล้วเกิดปัญหาที่ว่า ได้ค่าอุณหภูมิและค่าความชื้นมีค่าติดลบทั้งคู่เลยครับ ผมลองแก้ดูหลายๆๆอย่างแล้วก็ยังไม่เปลี่ยนแปลงเลยครับ

เลยอยากจะขอความช่วยเหลือครับ

นี้Code ครับ

/*--------------------------------------------------------------------------*/
// Program : Humidity & Temperature Sensor demo by SHT11
// Description : Read Humidity & Temperature from SHT11 display value on LCD
// Filename : sht11.c
// C compiler : RIDE 51 V6.4.35
/*--------------------------------------------------------------------------*/
#include <p89v51rx2.h> // Header file register for P89V51RD2BN
#include <intrins.h> // Library for _nop_ function
#include <lcdv51.h> // Library for LCD display

#define MEASURE_TEMP 0x03 // Macro for command read Temperature
#define MEASURE_HUMI 0x05 // Macro for command read Humidity

#define TEMP 0 // Macro for select read Temperature
#define HUMI 1 // Macro for select read Humidity

#define C1 -4 // Constant C1 for calculate Humidity(12 bit resolution)
#define C2 0.0405 // Constant C2 for calculate Humidity(12 bit resolution)
#define C3 -0.0000028 // Constant C3 for calculate Humidity(12 bit resolution)

#define T1 0.01 // Constant T1 for calculate Temperature(14 bit resolution)
#define T2 0.00008 // Constant T2 for calculate Temperature(14 bit resolution)

sbit DATA = P1^7; // Configuration DATA pin
sbit SCK = P1^6; // Configuration SCK pin

/*****************************************************************************/
/*********************** Function for delay a few time ***********************/
/*****************************************************************************/
void wait()
{
char x; // Variable for counter
for(x=0;x<8;x++) // Loop for delay a few time
_nop_(); // Delay 1 MC
}
/*****************************************************************************/
/*********************** Function for write command to sht11 *****************/
/*****************************************************************************/
void sht11_write(unsigned char value)
{
unsigned char i; // Variable for counter
for(i=0x80;i>0;i/=2) // Loop for bit shift for compare data to send
{
if(i & value) // Data bit AND operation logic "1"?
DATA = 1; // Send logic "1" to DATA pin
else
DATA = 0; // Send logic "0" to DATA pin
SCK = 1; // Generate clock at SCK pin
wait(); // Wait a few time
SCK = 0;
}
DATA = 1; // Release DATA pin
SCK = 1; // Generate clock at SCK pin
wait(); // Wait a few time
SCK = 0;
}
/*****************************************************************************/
/*********************** Function for read data from sht11 *******************/
/*****************************************************************************/
char sht11_read()
{
unsigned char i,value;
DATA = 1; // Release DATA pin
for(i=0x80;i>0;i/=2) // Loop for bit shift for compare data to send
{
SCK = 1; // Generate clock at SCK pin
if(DATA)
value = value | i; // Keep data bit
SCK = 0;
}
SCK = 1; // Generate clock at SCK pin
wait(); // Wait a few time
SCK = 0;
DATA = 1;
return(value); // Return data read from sht11
}
/*****************************************************************************/
/*********************** Function for initial a transmission start ***********/
/*****************************************************************************/
void transmission_start(void)
{
DATA = 1; // Set DATA pin
SCK = 0; // Clear SCK pin
wait(); // Wait a few time
SCK = 1; // Set SCK pin
wait(); // Wait a few time
DATA = 0; // Clear DATA pin
wait(); // Wait a few time
SCK = 0; // Clear SCK pin
wait(); // Wait a few time
SCK = 1; // Set SCK pin
wait(); // Wait a few time
DATA = 1; // Set DATA pin
wait(); // Wait a few time
SCK = 0; // Clear SCK pin
}
/*****************************************************************************/
/*********************** Function for connection reset sequence **************/
/*****************************************************************************/
void connection_reset(void)
{
unsigned char i; // Variable for counter
DATA = 1; // first time for DATA pin
SCK = 0; // first time for SCK pin
for(i=0;i<9;i++) // Loop for toggle SCK 9 time
{
SCK = 1; // Generate clock at SCK pin
wait(); // Wait a few time
SCK = 0;
}
transmission_start(); //transmission start
}
/*****************************************************************************/
/************* Function for readout the data from sht11(2 byte) **************/
/*****************************************************************************/
int sht11_read_measure(unsigned char x)
{
int value; // Variable for keep readout data

transmission_start(); // Start connection
if(x==TEMP) // Read Temperature?
{
sht11_write(MEASURE_TEMP); // Send command read Temperature to sht11
delay(270); // Delay for read Temperature(14 bit resolution)
}
else if(x==HUMI) // Read Humidity?
{ //send command to sensor
sht11_write(MEASURE_HUMI);
delay(80); // Delay for read Humidity(12 bit resolution)
}
value = sht11_read(); // Read MSB byte
value<<=8; // Shift bit 8 time before keep next byte
value = value + sht11_read(); // Read LSB byte
return(value); // Return readout value from sht11
}
/*****************************************************************************/
/************* Function for convert data from sht11 to float value ***********/
/*****************************************************************************/
float read_HT_float(unsigned char x)
{
float so_rh, // Variable for keep Humidity readout value
so_t, // Variable for keep Temperature readout value
rh_linear, // Variable for RH linear(Calculate by equation sht11 data sheet)
rh, // Variable for Humidity(Calculate by equation sht11 data sheet)
temp; // Variable for Temperature(Calculate by equation sht11 data sheet)

so_rh = sht11_read_measure(MEASURE_HUMI); // Keep Humidity readout value to so_rh
so_t = sht11_read_measure(MEASURE_TEMP); // Keep Temperature readout value to so_t

temp = so_t*0.01 - 40; // Calculate Temperature
rh_linear = C1 + C2*so_rh + C3*so_rh*so_rh ; // Calculate RH linear
rh = (temp-25)*(T1+T2*so_rh)+rh_linear; // Calculate Humidity

if(x==TEMP) // Read Temperature?
return(temp); // Return Temperature(float type)
else if(x==HUMI) // Read Humidity?
return(rh); // Return Humidity(float type)
}

/*****************************************************************************/
/********************************* Main Program ******************************/
/*****************************************************************************/
void main()
{
int h, // Variable for keep Humidity(integer tyep)
t, // Variable for keep Temperature(integer tyep)
old_h, // Variable for keep old Humidity
old_t; // Variable for keep old Temperature
t = (int)read_HT_float(TEMP); // Read Temperature(integer format)
h = (int)read_HT_float(HUMI); // Read Humidity(integer format)
lcd_init(); // Initial module LCD
lcd_puts(0x80,"Temp: C"); // Display srceen for Temperature
lcd_puts(0xC0,"Humi: %"); // Display srceen for Humidity
while(1) // Infinite loop
{
connection_reset(); // Start connection
if(old_t != t) // Compare old and new Temperature equa?
{
lcd_puts(0x86," "); // Clear old Temperature display
inttolcd(0x86,t); // Display new Temperature value
}
if(old_h != h) // Compare old and new Humidity equa?
{
lcd_puts(0xC6," "); // Clear old Humidity display
inttolcd(0xC6,h); // Display new Humidity value
}
old_t = t; // Keep Temperature value for next compare
old_h = h; // Keep Humidity value for next compare
}
}

ขอบพระคุณเป็นอย่างสูงครับ

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 2 และ บุคคลทั่วไป 0 ท่าน

cron