#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
#define MEASURE_HUMI 0x05
#define TEMP 0
#define HUMI 1
#define C1 -4.0
#define C2 0.0405
#define C3 -0.0000028
#define T1 0.01
#define T2 0.00008
sbit DATA = P3^2;
sbit SCK = P3^3;
/*********************** Function for delay a few time ***********************/
void wait()
{
char x;
for(x=0;x<8;x++)
_nop_();
}
/*********************** SHT11 writing command function *****************/
void sht11_write(unsigned char value)
{
unsigned char i;
for(i=0x80;i>0;i/=2)
{
if(i & value)
DATA = 1;
else
DATA = 0;
SCK = 1;
wait();
SCK = 0;
}
DATA = 1;
SCK = 1;
wait();
SCK = 0;
}
/*********************** SHT11 reading command function *******************/
char sht11_read()
{
unsigned char i,value;
DATA = 1;
for(i=0x80;i>0;i/=2)
{
SCK = 1;
if(DATA)
value = value | i;
SCK = 0;
}
SCK = 1;
wait();
SCK = 0;
DATA = 1;
return(value);
}
/*********************** SHT11 initial transmittion start function ***********/
void transmission_start(void)
{
DATA = 1;
SCK = 0;
wait();
SCK = 1;
wait();
DATA = 0;
wait();
SCK = 0;
wait();
SCK = 1;
wait();
DATA = 1;
wait();
SCK = 0;
}
/*********************** SHT11 connection reset sequence function **************/
void connection_reset()
{
unsigned char i;
DATA = 1;
SCK = 0;
for(i=0;i<9;i++)
{
SCK = 1;
wait();
SCK = 0;
}
transmission_start();
}
/************* SHT11 readout data function (2 byte) **************/
int sht11_read_measure(unsigned char measure_sel)
{
int value;
transmission_start();
if(measure_sel==TEMP)
{
sht11_write(MEASURE_TEMP);
delay(270);
}
else if(measure_sel==HUMI)
{
sht11_write(MEASURE_HUMI);
delay(80);
}
value = sht11_read();
value<<=8;
value = value + sht11_read();
return(value);
}
/************* Function for convert data from sht11 to float value ***********/
float read_HT_float(unsigned char measure_sel)
{
float so_rh,so_t,rh_linear,rh_true,temp;
so_rh = sht11_read_measure(HUMI);
so_t = sht11_read_measure(TEMP);
temp = so_t*0.01- 40;
rh_linear = C1 + (C2*so_rh) + C3*(so_rh*so_rh) ;
rh_true = (temp-25)*(T1+T2*so_rh)+rh_linear;
if(measure_sel==TEMP)
return(temp);
else if(measure_sel==HUMI)
return(rh_true);
}
/********************************* Main Program ******************************/
void main()
{
int h,t,old_h,old_t;
lcd_init();
lcd_puts(0x82,"Temp: C");
lcd_puts(0xC2,"Humi: %");
init_uart();
while(1)
{
connection_reset();
t = (int)read_HT_float(TEMP);
h = (int)read_HT_float(HUMI);
if(old_t != t)
{
lcd_puts(0x88," ");
inttolcd(0x88,t);
}
if(old_h != h)
{
lcd_puts(0xC8," ");
inttolcd(0xC8,h);
}
old_t = t;
old_h = h;
}
}
นี้ครับโค๊ดที่เขียน ต้องขอขอบพระคุณท่าน mcuprojects ที่ช่วยกรุณาให้คำแนะนำกับผมน่ะครับ
พอดีผมแก้ตั้งหลายครั้งแล้วผมยังไม่ทำได้สักทีเลยครับแต่ถึงยังไงก็ช่วยแนะนำด้วยน่ะครับ ขอบพระคุณไว้ล่วงหน้าครับ