|
|
การใช้งาน
SHT15
โมดูลวัดอุณหภูมิ และความชื้น |
|
|
|
|
|
|
|
|
การใช้งาน
SHT15 กับ PIC16F648A |
|
|
|
|
|
เนื่องจากในโปรแกรม
ใช้ฟังก์ชั่นทางคณิตศาสตร์ #include <math.h>
และมีการคำนวนแบบ floating point ทำให้ไฟล์มีขนาดใหญ่ เมื่อคอมไพล์แล้ว จะมีขนาดใหญ่กว่า
2K จึงไม่สามารถใช้กับ PIC16F628 ได้ ดังนั้นจึงต้องใช้กับ PIC ที่มี Code
Memory ที่มีขนาดใหญ่ขึ้นเช่น PIC16F648A หรือ PIC16F877 |
|
|
|
|
|
ตัวอย่างโปรแกรม
PIC16F648A |
|
|
//SHT1x Sensor temperature & Humidity
//Read sensor and diaplay to RS232
//Compiler CCS Version 3.249
#case
#include <16F648A.h>
#use delay(clock=4000000)
#fuses XT,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1)
#include <math.h>
#define SHT1xDATA PIN_B4
#define SHT1xSCK PIN_B5
#define noACK 0
#define ACK 1
// SHT1x address=000 is currently supported
// SHT1x command code
//adr command r/w
#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
// constant use for SHT1x Humidity Measurement
#define C1 -4.0
#define C2 0.0405
#define C3 -0.0000028
// constant use for SHT1x Temperature Measurement
#define D1 -40.0
#define D2 0.01
// constant use for SHT1x True Humidity Measurement
#define T1 0.01
#define T2 0.00008
//PIC16F6X8
#define CMCON 0x1F
#define PORTA 0x05
#define PORTB 0x06
void InitialChip(void);
//SHT1x Transmission Start condition
void SHTStart()
{
output_high(SHT1xDATA);
output_low(SHT1xSCK);
output_high(SHT1xSCK);
output_low(SHT1xDATA);
output_low(SHT1xSCK);
output_high(SHT1xSCK);
output_high(SHT1xDATA);
output_low(SHT1xSCK);
}
// SHT1x Connection Reset:
void SHTConReset()
{
int i;
output_high(SHT1xDATA);
for (i=0; i<9; i++)
{
output_high(SHT1xSCK);
delay_us(2);
output_low(SHT1xSCK);
delay_us(2);
}
SHTStart();
}
// SHT1x Address & Command Mode with address=000
int SHTWrite(int Data)
{
int i;
for (i=0x80;i>0;i/=2) //shift bit for masking data
{
if(i&Data)
output_high(SHT1xDATA);
else
output_low(SHT1xDATA);
delay_us(2); //Snend Clock each bit
output_high(SHT1xSCK);
delay_us(2);
output_low(SHT1xSCK);
}
output_float(SHT1xDATA); //Change DATA Line to Input
delay_us(2);
output_high(SHT1xSCK); //Clock for Acknowledge
delay_us(2);
i= input(SHT1xDATA); //Get Acknowledge
output_low(SHT1xSCK);
delay_ms(250);
return (i);
}
//Read data from SHT1x
long SHTRead(void)
{
int i;
long lTmp,lVal1,lVal2,lValue;
lVal1=0;
lVal2=0;
//get MSB from SHT1x
for (i=0; i<8; i++)
{
lVal1<<=1;
output_high(SHT1xSCK); //Send Clock Hight
lTmp = input(SHT1xDATA); //Read Data Bit
//delay_us(2);
output_low(SHT1xSCK); //Send Clock Low
//delay_us(2);
if(lTmp)
lVal1|=1; //store in lVal1
}
//Acknowledge routine for Next byte
output_low(SHT1xDATA);
output_high(SHT1xSCK);
//delay_us(2);
output_float(SHT1xDATA); //Change to Input
output_low(SHT1xSCK);
//delay_us(2);
//get LSB from SHT1x
for (i=0; i<8; i++)
{
lVal2<<=1;
output_high(SHT1xSCK); //Send Clock Hight
lTmp = input(SHT1xDATA); //Read Data Bit
//delay_us(2);
output_low(SHT1xSCK); //Send Clock Low
//delay_us(2);
if(lTmp)
lVal2|=1; //store in lVal2
}
lValue = make16(lVal1,lVal2); //Makes a 16 bit number out of two 8 bit numbers.
return(lValue);
}
// SHT1x Soft Reset
// resets the interface, clears the status register to default values
// wait minimum 11ms before next command
void SHTSoftReset()
{
SHTConReset();
SHTWrite(RESET);
}
// calculate dewpoint
float sht1x_calc_dewpoint(float fRh,float fTemp)
{
float fDewpoint;
float fLogEW;
fLogEW = ((7.5*fTemp)/(237.3+fTemp))+log10(fRh)-1.33923;
fDewpoint = ((0.66077-log10(fLogEW))*(237.3))/(log10(fLogEW)-8.16077);
return(fDewpoint);
}
void InitialChip(void)
{
setup_comparator(NC_NC_NC_NC); //Input Digital
set_tris_b(0B00000001);
}
// Main Program
main()
{
float fRh_lin;
float fRh_true;
float fTemp_true;
float fDew_point;
long lValue_rh;
long lValue_temp;
int R;
InitialChip();
delay_ms(200);
SHTConReset();
while (TRUE)
{
// delay >11ms before next command
delay_ms(12);
SHTStart(); //@1 start transmission
R=SHTWrite(MEASURE_TEMP); //@2 measure temperature
if(R==1)
{
printf("Sensor Error\n");
delay_ms(1000);
continue;
}
lValue_temp = SHTRead();
// temperature calculation
fTemp_true = (D1+(D2*lValue_temp));
// delay 11ms before next command
delay_ms(12);
// start transmission
SHTStart();
// measure relative humidity
SHTWrite(MEASURE_HUMI);
lValue_rh = SHTRead();
// relative humidity calculation
fRh_lin = (C1+(C2*lValue_rh)+(C3*lValue_rh*lValue_rh));
fRh_true = (((fTemp_true-25)*(T1+(T2*lValue_rh)))+fRh_lin);
// dewpoint calculation
fDew_point = sht1x_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\r\n",fDew_point);
delay_ms(1000);
}
}
|
|
|
|
|
|
|
|
|
|
การใช้งานกับ
PIC16F877 |
|
|
|
|
|
การใช้งาน
SHT15 กับ PIC16F877 |
|
|
|
|
|
ตัวอย่างโปรแกรม
PIC16F877 |
|
|
|
|
|
//SHT1x Sensor temperature & Humidity
//Read sensor and diaplay to RS232
//Compiler CCS Version 3.249
#case
#include <16F877A.h>
#use delay(clock=4000000)
#fuses XT,PUT,BROWNOUT,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#include <math.h>
#define SHT1xDATA PIN_B4
#define SHT1xSCK PIN_B5
#define noACK 0
#define ACK 1
// SHT1x address=000 is currently supported
// SHT1x command code
//adr command r/w
#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
// constant use for SHT1x Humidity Measurement
#define C1 -4.0
#define C2 0.0405
#define C3 -0.0000028
// constant use for SHT1x Temperature Measurement
#define D1 -40.0
#define D2 0.01
// constant use for SHT1x True Humidity Measurement
#define T1 0.01
#define T2 0.00008
//PIC16F6X8
#define CMCON 0x1F
#define PORTA 0x05
#define PORTB 0x06
void InitialChip(void);
//SHT1x Transmission Start condition
void SHTStart()
{
output_high(SHT1xDATA);
output_low(SHT1xSCK);
output_high(SHT1xSCK);
output_low(SHT1xDATA);
output_low(SHT1xSCK);
output_high(SHT1xSCK);
output_high(SHT1xDATA);
output_low(SHT1xSCK);
}
// SHT1x Connection Reset:
void SHTConReset()
{
int i;
output_high(SHT1xDATA);
for (i=0; i<9; i++)
{
output_high(SHT1xSCK);
delay_us(2);
output_low(SHT1xSCK);
delay_us(2);
}
SHTStart();
}
// SHT1x Address & Command Mode with address=000
int SHTWrite(int Data)
{
int i;
for (i=0x80;i>0;i/=2) //shift bit for masking data
{
if(i&Data)
output_high(SHT1xDATA);
else
output_low(SHT1xDATA);
delay_us(2); //Snend Clock each bit
output_high(SHT1xSCK);
delay_us(2);
output_low(SHT1xSCK);
}
output_float(SHT1xDATA); //Change DATA Line to Input
delay_us(2);
output_high(SHT1xSCK); //Clock for Acknowledge
delay_us(2);
i= input(SHT1xDATA); //Get Acknowledge
output_low(SHT1xSCK);
delay_ms(250);
return (i);
}
//Read data from SHT1x
long SHTRead(void)
{
int i;
long lTmp,lVal1,lVal2,lValue;
lVal1=0;
lVal2=0;
//get MSB from SHT1x
for (i=0; i<8; i++)
{
lVal1<<=1;
output_high(SHT1xSCK); //Send Clock Hight
lTmp = input(SHT1xDATA); //Read Data Bit
//delay_us(2);
output_low(SHT1xSCK); //Send Clock Low
//delay_us(2);
if(lTmp)
lVal1|=1; //store in lVal1
}
//Acknowledge routine for Next byte
output_low(SHT1xDATA);
output_high(SHT1xSCK);
//delay_us(2);
output_float(SHT1xDATA); //Change to Input
output_low(SHT1xSCK);
//delay_us(2);
//get LSB from SHT1x
for (i=0; i<8; i++)
{
lVal2<<=1;
output_high(SHT1xSCK); //Send Clock Hight
lTmp = input(SHT1xDATA); //Read Data Bit
//delay_us(2);
output_low(SHT1xSCK); //Send Clock Low
//delay_us(2);
if(lTmp)
lVal2|=1; //store in lVal2
}
lValue = make16(lVal1,lVal2); //Makes a 16 bit number out of two 8 bit numbers.
return(lValue);
}
// SHT1x Soft Reset
// resets the interface, clears the status register to default values
// wait minimum 11ms before next command
void SHTSoftReset()
{
SHTConReset();
SHTWrite(RESET);
}
// calculate dewpoint
float sht1x_calc_dewpoint(float fRh,float fTemp)
{
float fDewpoint;
float fLogEW;
fLogEW = ((7.5*fTemp)/(237.3+fTemp))+log10(fRh)-1.33923;
fDewpoint = ((0.66077-log10(fLogEW))*(237.3))/(log10(fLogEW)-8.16077);
return(fDewpoint);
}
void InitialChip(void)
{
setup_comparator(NC_NC_NC_NC); //Input Digital
set_tris_b(0B00000000);
set_tris_c(0B10000000);
}
// Main Program
main()
{
float fRh_lin;
float fRh_true;
float fTemp_true;
float fDew_point;
long lValue_rh;
long lValue_temp;
int R;
InitialChip();
delay_ms(200);
SHTConReset();
while (TRUE)
{
// delay >11ms before next command
delay_ms(12);
SHTStart(); //@1 start transmission
R=SHTWrite(MEASURE_TEMP); //@2 measure temperature
if(R==1)
{
printf("Sensor Error\n");
delay_ms(1000);
continue;
}
lValue_temp = SHTRead();
// temperature calculation
fTemp_true = (D1+(D2*lValue_temp));
// delay 11ms before next command
delay_ms(12);
// start transmission
SHTStart();
// measure relative humidity
SHTWrite(MEASURE_HUMI);
lValue_rh = SHTRead();
// relative humidity calculation
fRh_lin = (C1+(C2*lValue_rh)+(C3*lValue_rh*lValue_rh));
fRh_true = (((fTemp_true-25)*(T1+(T2*lValue_rh)))+fRh_lin);
// dewpoint calculation
fDew_point = sht1x_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\r\n",fDew_point);
delay_ms(1000);
}
}
|
|
|
|
|
|
|
|
|
|
ผลการทดลองของทั้งสองโปรแกรม
ผ่าน Hyperterminal Baudrate 9600,n,8,1 |
|
|
T=23.98 C H=59.39% Dew Point: -17.857664C
T=23.98 C
H=59.39%
Dew Point: -17.857664C
T=23.98 C
H=59.39%
Dew Point: -17.857664C |
|
|
|
|
|
จะแสดงผลทุกๆ 1 วินาที |
|
|
หากไม่ได้ต่อ sensor จะแสดงว่า"Sensor Error" |
|
|
ศมิทธิ์
เอมสมบัติ |
|
|