#include <16F877A.h>
#include <string.h>
// #device *=16
#fuses HS,NOLVP,WDT,NOPUT,NOBROWNOUT
#use delay (clock=10000000,RESTART_WDT)
#USE RS232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7,STREAM=COM_A)
#USE RS232(BAUD=9600,XMIT=PIN_D2,RCV=PIN_D3,STREAM=COM_B)
#include <math.h>
#include <string.h>
// Definiciones Sensores Temp y HR
#define DATA_SHT PIN_C0 // sht75 1
#define SCK_SHT PIN_C1
#define DATA_SHT2 PIN_C2 // sht75 2
#define SCK_SHT2 PIN_C3
#define noACK 0
#define ACK 1
#define STATUS_REG_W 0x06 //000 0011 0 //adr|command|r/w command code
#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
#define C1 -4.0 // constant use for SHT1x Humidity Measurement
#define C2 0.0405
#define C3 -0.0000028
#define D1 -40.00 // constant use for SHT1x Temperature Measurement
#define D2 0.01
#define T1 0.01 // constant use for SHT1x True Humidity Measurement
#define T2_ 0.00008
///////////////////////////////////////////////////////////////sensor1
// SHT1x Transmission Start Sequence
void sht1x_xmission_start()
{ output_high(DATA_SHT);output_low(SCK_SHT);delay_us(2);output_high(SCK_SHT);
delay_us(2);output_low(DATA_SHT);delay_us(2);output_low(SCK_SHT);delay_us(2);
delay_us(2);delay_us(2);output_high(SCK_SHT);delay_us(2);
output_high(DATA_SHT);delay_us(2);output_low(SCK_SHT);delay_us(2);
}
// SHT1x Connection Reset Sequence
void sht1x_connection_reset()
{ int i;
output_high(DATA_SHT);
for (i=0; i<9; i++)
{output_high(SCK_SHT);delay_us(2);output_low(SCK_SHT);delay_us(2);}
sht1x_xmission_start();
}
// SHT1x Address & Command Mode with address=000
void sht1x_command_mode(int iMode)
{ int i;
for (i=128; i>0; i/=2)
{ if (i & iMode) output_high(DATA_SHT);
else output_low(DATA_SHT);
delay_us(2); output_high(SCK_SHT);
delay_us(2); output_low(SCK_SHT);
}
output_float(DATA_SHT); delay_us(2);
output_high(SCK_SHT); delay_us(2);
output_low(SCK_SHT); delay_ms(250);
}
// SHT1x Soft Reset
// resets the interface, clears the status register to default values
// wait minimum 11ms before next command
void sht1x_soft_reset()
{ sht1x_connection_reset(); sht1x_command_mode(RESET); }
// read DATA_SHT from SHT1x and store
long sht1x_read_DATA_SHT()
{ int i;
long lTmp;
long lVal1=0;
long lVal2=0;
long lValue;
// get MSB from SHT1x
for (i=0; i<8; i++)
{
lVal1<<=1;
output_high(SCK_SHT);
lTmp = input(DATA_SHT);
delay_us(2);
output_low(SCK_SHT);
delay_us(2);
if (lTmp) lVal1|=1;
}
// acknowledge routine
output_low(DATA_SHT); delay_us(2);
output_high(SCK_SHT); delay_us(2);
output_low(SCK_SHT);
output_float(DATA_SHT);
delay_us(2);
// get LSB from SHT1x
for (i=0; i<8; i++)
{
lVal2<<=1;
output_high(SCK_SHT);
lTmp = input(DATA_SHT);
delay_us(2);
output_low(SCK_SHT);
delay_us(2);
if (lTmp) lVal2|=1;
}
lValue = make16(lVal1,lVal2);
return(lValue);
}
// calculate dewpoint
float sht1x_calc_dewpoint(float h,float t)
{
float logEx,dew_point;
logEx=-2+0.66077+((7.5*t)/(237.3+t))+log10(h);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
///////////////////////////////////////////////////////////////sensor1
///////////////////////////////////////////////////////////////sensor2
// SHT1x Transmission Start Sequence
void sht1x_xmission_start2()
{ output_high(DATA_SHT2);
output_low(SCK_SHT2); delay_us(2);
output_high(SCK_SHT2); delay_us(2);
output_low(DATA_SHT2); delay_us(2);
output_low(SCK_SHT2); delay_us(2); delay_us(2); delay_us(2);
output_high(SCK_SHT2); delay_us(2);
output_high(DATA_SHT2); delay_us(2);
output_low(SCK_SHT2); delay_us(2);
}
// SHT1x Connection Reset Sequence
void sht1x_connection_reset2()
{ int i;
output_high(DATA_SHT2);
for (i=0; i<9; i++)
{ output_high(SCK_SHT2); delay_us(2);
output_low(SCK_SHT2); delay_us(2);
}
sht1x_xmission_start2();
}
// SHT1x Address & Command Mode with address=000
void sht1x_command_mode2(int iMode)
{ int i;
for (i=128; i>0; i/=2)
{ if (i & iMode) output_high(DATA_SHT2);
else output_low(DATA_SHT2);
delay_us(2); output_high(SCK_SHT2);
delay_us(2); output_low(SCK_SHT2);
}
output_float(DATA_SHT2); delay_us(2);
output_high(SCK_SHT2); delay_us(2);
output_low(SCK_SHT2); delay_ms(250);
}
// SHT1x Soft Reset
// resets the interface, clears the status register to default values
// wait minimum 11ms before next command
void sht1x_soft_reset2()
{ sht1x_connection_reset2(); sht1x_command_mode2(RESET); }
// read DATA_SHT from SHT1x and store
long sht1x_read_DATA_SHT2()
{ int i;
long lTmp;
long lVal1=0;
long lVal2=0;
long lValue;
// get MSB from SHT1x
for (i=0; i<8; i++)
{
lVal1<<=1;
output_high(SCK_SHT2);
lTmp = input(DATA_SHT2);
delay_us(2);
output_low(SCK_SHT2);
delay_us(2);
if (lTmp) lVal1|=1;
}
// acknowledge routine
output_low(DATA_SHT2); delay_us(2);
output_high(SCK_SHT2); delay_us(2);
output_low(SCK_SHT2);
output_float(DATA_SHT2);
delay_us(2);
// get LSB from SHT1x
for (i=0; i<8; i++)
{
lVal2<<=1;
output_high(SCK_SHT2);
lTmp = input(DATA_SHT2);
delay_us(2);
output_low(SCK_SHT2);
delay_us(2);
if (lTmp) lVal2|=1;
}
lValue = make16(lVal1,lVal2);
return(lValue);
}
// calculate dewpoint
float sht1x_calc_dewpoint2(float h,float t)
{
float logEx,dew_point;
logEx=-2+0.66077+((7.5*t)/(237.3+t))+log10(h);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
///////////////////////////////////////////////////////////////sensor2
// Main Program
void main(void)
{ float fRh_lin, fRh_true, fTemp_true;
float fRh_lin2, fRh_true2, fTemp_true2;
long lValue_rh,lValue_temp;
long lValue_rh2,lValue_temp2;
char temp_cadena[6];
char humi_cadena[6];
char cadena_rs232[25];
char espacio[2];
char transmite[42];
char transmite2[6];
char transmite3[6];
strcpy(espacio," ");
strcpy(transmite,"");
strcpy(transmite2,"");
strcpy(transmite3,"");
setup_wdt(WDT_2304MS);
while(TRUE)
{ restart_wdt();
sht1x_connection_reset(); delay_ms(500);
sht1x_connection_reset2(); delay_ms(500);
strcpy(transmite,"");
strcpy(temp_cadena,"");
strcpy(humi_cadena,"");
delay_ms(12); sht1x_xmission_start(); sht1x_command_mode(MEASURE_TEMP);
lValue_temp = sht1x_read_DATA_SHT(); fTemp_true = (D1+(D2*lValue_temp)); // calcula la medida fisica de temp1
delay_ms(12); sht1x_xmission_start(); sht1x_command_mode(MEASURE_HUMI);
lValue_rh = sht1x_read_DATA_SHT(); // calcula la humedad relativa
if((lValue_temp==65535)||(lValue_rh==65535)) {sht1x_connection_reset();
}
else
{fRh_lin = (C1+(C2*lValue_rh)+(C3*lValue_rh*lValue_rh));
fRh_true = (((fTemp_true-25)*(T1+(T2_*lValue_rh)))+fRh_lin);
strcpy(temp_cadena,"");
sprintf(temp_cadena,"%02.0f",fTemp_true); // convierte a cadena el valor de Temp ##
sprintf(humi_cadena,"%02.0f",fRh_true);
strcpy(transmite,"");
strcat(transmite,temp_cadena); // añade este valor a cadena tranmite
strcat(transmite,espacio);
strcat(transmite,humi_cadena);
strcpy(transmite2,"");
strcpy(transmite2,transmite);
}
strcpy(transmite,"");
strcpy(temp_cadena,"");
strcpy(humi_cadena,"");
delay_ms(12);sht1x_xmission_start2();sht1x_command_mode2(MEASURE_TEMP);
lValue_temp2 = sht1x_read_DATA_SHT2(); fTemp_true2 = (D1+(D2*lValue_temp2)); // calcula la medida fisica de temp2
delay_ms(12); sht1x_xmission_start2(); sht1x_command_mode2(MEASURE_HUMI);
lValue_rh2 = sht1x_read_DATA_SHT2();
if((lValue_temp2==65535)||(lValue_rh2==65535)) {sht1x_connection_reset2();
}
else
{fRh_lin2 = (C1+(C2*lValue_rh2)+(C3*lValue_rh2*lValue_rh2));
fRh_true2 = (((fTemp_true2-25)*(T1+(T2_*lValue_rh2)))+fRh_lin2);
strcpy(temp_cadena,"");
sprintf(temp_cadena,"%02.0f",fTemp_true2); // convierte a cadena el valor de Temp ##
sprintf(humi_cadena,"%02.0f",fRh_true2);
strcpy(transmite,"");
strcat(transmite,temp_cadena); // añade este valor a cadena tranmite
strcat(transmite,espacio);
strcat(transmite,humi_cadena);
strcpy(transmite3,"");
strcpy(transmite3,transmite);
}
if ((strlen(transmite2))!=0&&(strlen(transmite3))!=0)
{ strcpy(transmite,"");
strcat(transmite,transmite2);
strcat(transmite, espacio);
strcat(transmite,transmite3);
fgets(cadena_rs232,COM_B); // cual=1; esto lo puse pal tec
strcat(transmite,espacio);
strcat(transmite,cadena_rs232);
fprintf(COM_A,"%s",transmite);
printf("\r");
}
} // while
} // main
#include <16F877A.h> // MCU specific library
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay (clock=20000000) // 20MHz clock
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#include<lcd.c>
#include <math.h>
#include <ds.1307.c>
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL, FORCE_HW)
void read_sht11(void);
float fRh_lin,fRh_true,fTemp_true,fDew_point;
int8 counter;
long lValue_rh,lValue_temp;
int lValue_tempL,lValue_tempH,lValue_rhL,lValue_rhH;
char j,k;
#include <sht11.c>
void main()
{
BYTE sec;
BYTE min;
BYTE hr;
BYTE day;
BYTE mth;
BYTE yr;
BYTE dow;
lcd_init();
ds1307_init();
printf(lcd_putc,"\f Welcome! ");
ds1307_set_date_time(01,03,07,0,16,15,0);
while(1)
{
read_sht11();
delay_ms(1000);
ds1307_get_date(day,mth,yr,dow);
ds1307_get_time(hr,min,sec);
printf(lcd_putc,"\f%02u-%02u-%02u %2.2fC",day,mth,yr,fTemp_true);
printf(lcd_putc,"\n%02u:%02u:%02u %2.2f%%",hr,min,sec,fRh_true);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void read_sht11()
{
sht1x_connection_reset();
delay_ms(12);
sht1x_xmission_start();
sht1x_command_mode(MEASURE_TEMP);
lValue_temp = sht1x_read_data();
fTemp_true = (-1975+(0.529161*lValue_temp)-(0.000034804*lValue_temp*lValue_temp));
delay_ms(12);
sht1x_xmission_start();
sht1x_command_mode(MEASURE_HUMI);
lValue_rh = sht1x_read_data();
if((lValue_temp==65535)||(lValue_rh==65535))
{sht1x_connection_reset();}
else
{
lValue_tempL=make8(lValue_temp,0); /*split the 14-bit temperature data into higher and lower byte in order to send to pc through RS232 serial port*/
delay_us(20);
lValue_tempH=make8(lValue_temp,1);
delay_ms(50);
lValue_rhL=make8(lValue_rh,0); /*split the 12-bit humidity data into higher and lower byte in order to send to pc through RS232 serial port*/
delay_us(20);
lValue_rhH=make8(lValue_rh,1);
/* 2 bytes for temperature */
delay_ms(20);
putc(lValue_tempH); /* Send higher byte */
delay_ms(20);
putc(lValue_tempL); /* Send lower byte */
delay_ms(200);
/* 2 bytes for humidity */
putc(lValue_rhH);
delay_ms(20);
putc(lValue_rhL);
fRh_lin = (C1+(C2*lValue_rh)+(C3*lValue_rh*lValue_rh));
fRh_true = (((fTemp_true-25)*(T1+(T2*lValue_rh)))+fRh_lin);
fDew_point = sht1x_calc_dewpoint(fRh_true,fTemp_true);
delay_ms(1000);
}
}
#define DATA PIN_B6 /*set DATA to PortB6 and SCK to portB5*/
#define SCK 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 SHT11 Humidity Measurement*/
#define C1 -4.0
#define C2 0.0405
#define C3 -0.0000028
/*constant use for SHT11 Temperature Measurement*/
#define D1 -40.0
#define D2 0.01
/*constant use for SHT11 True Humidity Measurement*/
#define T1 0.01
#define T2 0.00008
/*SHT11 Transmission Start Sequence*/
void sht11_xmission_start()
{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(2);delay_us(2);delay_us(2);
output_high(SCK);delay_us(2);
output_high(DATA);delay_us(2);
output_low(SCK);delay_us(2);}
/*SHT11 Connection Reset Sequence*/
void sht11_connection_reset()
{ int i;
output_high(DATA);
for (i=0; i<9; i++)
{output_high(SCK);
delay_us(2);
output_low(SCK);
delay_us(2);}
sht1x_xmission_start();}
/*SHT11 Address & Command Mode with address=000*/
void sht11_command_mode(int iMode)
{
int i;
for (i=128; i>0; i/=2)
{if (i & iMode) 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);
/*delay >55ms for 12-bit measurement*/
/*delay >210ms for 14-bit measurement*/
delay_ms(250); /*250ms is chosen as delay time*/
}
/*SHT11Soft Reset - resets the interface, clears the status register to default values*/
/*wait minimum 11ms before next command*/
void sht11_soft_reset()
{
sht11_connection_reset();
sht11_command_mode(RESET);
}
// read data from SHT11 and store
long sht11_read_data()
{ int i;
long lTmp,lVal1=0,lVal2=0,lValue;
for (i=0; i<8; i++) /*get MSB from SHT11*/
{lVal1<<=1;
output_high(SCK);
lTmp = input(DATA);
delay_us(2);
output_low(SCK);
delay_us(2);
if (lTmp) lVal1|=1; }
output_low(DATA); /*acknowledge routine*/
delay_us(2);
output_high(SCK);delay_us(2);
output_low(SCK);output_float(DATA);delay_us(2);
for (i=0; i<8; i++) /*get LSB from SHT11*/
{lVal2<<=1;
output_high(SCK);
lTmp = input(DATA);
delay_us(2);
output_low(SCK);
delay_us(2);
if (lTmp) lVal2|=1;}
lValue = make16(lVal1,lVal2);
return(lValue);}
TechToys said:Have got a sample program working before with 16F877A and SHT10 (compatible with SHT11). under hitech C. Could send it to you for reference if you are interested.
The major modification from the original Sensirion's Keil C example is on : s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
The original one is
*(p_value) = s_read_byte(ACK);
*(p_value+1)=s_read_byte(ACK);
the change is:
*(p_value+1)=s_read_byte(ACK);
*(p_value) = s_read_byte(ACK);
Just swap the two statements above, and the rest of the sample program the same for Keil C and hi-tech C.
John
please send me the code code for SHT11
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?