โพสต์โดย SM » 14/11/2006 6:46 am
อันนี้ผ่านครับ Test แล้ว แต่เป็น PIC นะครับ ลองดู
#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>
#case
#define SHT1xDATA PIN_A0
#define SHT1xSCK PIN_A1
#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
long CRC;
void InitialChip(void);
//SHT1x Transmission Start condition
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
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:
// DATA=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
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
}
//Acknowledge routine for CRC
output_low(SHT1xDATA);
// delay_us(2);
output_high(SHT1xSCK);
//delay_us(2);
//output_low(SHT1xSCK);
//output_float(SHT1xDATA); //Change to Input
//delay_us(2);
CRC=0;
//get CRC from SHT1x
for (i=0; i<8; i++)
{
CRC<<=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)
CRC|=1; //store in CRC
}
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)
{
#asm
//Disable Comparator features
//CLRF PORTA //Clear Port A
CLRF PORTB //Clear Port B
MOVLW 0x07 //Turn Comparator off and
MOVWF CMCON //Enable pins for I/O
#endasm
}
// 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);
//setup_adc_ports(NO_ANALOGS);
//setup_adc(ADC_OFF);
//setup_comparator(FALSE);
//setup_vref(FALSE);
//printf("Hello\r\n");
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.2fC %lX\n",fTemp_true,CRC);
printf("H=%3.2f%%\n",fRh_true);
printf("\nDew Point: %3.6fC",fDew_point);
printf("\n");
delay_ms(1000);
}
}