Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F877 code for SHT11

Status
Not open for further replies.

phaychee

Newbie level 3
Joined
Aug 29, 2006
Messages
3
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,314
sht11 code

I'm trying to attach a Sensirion SHT11 Temperature and Humidity sensor to the PIC16F877 microcontroller, and then display the readings through LCD and PC via serial port, so i need the code for microcontroller...

Can anyone help me along here? I would really appreciate it.

thanks:)
 

sht11 driver

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
 

sht11 pic16f877

Thanks for those who had replied my post.

By the way, i already had the code for SHT11, if anyone interested, I can email to you.

Thanks:)
 

pic16f877a code

I am interested. Please send me example.
 

sht11 code for 16f877

Full project to display RH and temperature in 1 sec interval. Code in C18 version 2.40. MCU is 18LF4550. LCD is a 65k 1.8" color display.

Example here shows the SHT10 driver, color display driver, print a floating point in 1 decimal place on screen without using printf for a smaller code size.

John Leung
www.TechToys.com.hk
 

16f877a.h

For those who need the SHT11 code, here are them:

This is the one which i had received from one net friend:

Code:
#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

Added after 22 minutes:

For my project, I'm using PIC16F877A as central controller unit, interface with LCD,a real time clock, and SHT11 sensor and connect to computer through RS232 serial port.

Once the program has started, the value of date,time, temperature and humidity will show on LCD.
The data of temperature and humidity will be sent to computer through serial port.

In order to send both temperature and humidity data to computer, both data are split to higher and lower byte first, then send one by one.
These data will be combined afterward in computer depends on what kind of user interface are using.

For my case, I'm using Labview.


Those are the source code:

Main Programming:
Code:
#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);


}


}

The SHT11 code:
Code:
#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);}

In my case, I had modified the SHT11 driver that in above post as I put the read temperature and humidity data part into my main programming part.
 
hitech strcpy

I want to ask you do you have the e-mail of Barbarossa from the forum in https://www.mikroe.com
Because I read that he has written the source code for pic16f690 and sht75.If you can help me with the source code for pic16f690,sht75,LCD(2x16) and schematic sheet I will be very gratefull.The problem that I have is that I can't show nothing on the LCD. I send you proteus file and the code . Please if you can help me write back to me. Maybe I have mistake with the registers that I have to set while I start a project in Micro c Compiler. Or maybe I don't make correct frequency of the internal and external oscilator. Or maybe I don't use correctly libraries in Micro C- Please check this for me.
Thank you in advance.

Teodor


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
 

i have code for sht11 in mikroc pro.how i convert it to mikroc for pic 16f877a
/*
* Project name:
SHT11 Temperature and humidity sensor sample
* Copyright:
Noppharat Tawanron
* Description:
Send command SHT11 to measure temperature and humidity.
Calculate temperature and humidity from SHT11 digital value
and display on serial LCD module
* Test configuration:
MCU: PIC16F886
Dev.Board: -
Oscillator: HS, 16.0000 MHz
Ext. Modules: -
SW: mikroC PRO 1.65
* NOTES:
HW connection
MCU SHT11
RB0/INT <--------> DATA W/ 10K Ohm pull up
RB1 <--------> SCK
MCU LCD module
RC6 <--------> data in

*/

#define LCD_Clear_Screen Uart1_Write(0xFE); Uart1_Write(0x01);
#define LCD_Home Uart1_Write(0xFE); Uart1_Write(0x02);
#define LCD_Second_line Uart1_Write(0xFE); Uart1_Write(192);

#define CLK PORTB.F1
#define DATA PORTB.F0
#define DATA_X TRISB.F0

#define _TEMP 0x03
#define _HUMI 0x05
#define _RESET 0x1E

const float d1C = -40.1; //@ i4bit
const float d2C = 0.01; //@ i4bit
const float C1 = -4.0; //@ 12bit
const float C2 = 0.0405; //@ 12bit
const float C3 = -0.0000028; //@ 12bit
const float t1 = 0.01; //@ 12bit
const float t2 = 0.00008; //@ 12bit

float tempt, humi;
float RH_linear, RH_true;
char txt[13];
char *ptr;

//function use for measure temperature or humidiy from SHT only
unsigned SHT_Measure(char CMD){
char i;
char m = 128;
unsigned temp = 0;
DATA_X = 0;
DATA = 1;
CLK = 0;
// Connection reset sequence
for(i=0; i<10; i++){
CLK = 1;
CLK = 0;
}
// initial transmistion pluses

DATA = 1;
CLK = 1;
DATA = 0;
CLK = 0;
CLK = 1;
DATA = 1;
CLK = 0;

//Transmit command.
for(i=0; i<8; i++){
if(!(m & CMD)) DATA = 0;
else DATA = 1;
CLK = 1;
CLK = 0;
m >>= 1;
}
//Check acknowledge pulse. return 0 if no pulse.
DATA_X = 1;
CLK = 1;
if(DATA) return 0;
CLK = 0;
while(!DATA);
// wait until data is ready
while(DATA);
//delay_ms(250);
// get first byte MSB
for(i=0; i<8; i++){
temp <<= 1;
CLK = 1;
if(DATA) temp |= 1;
CLK = 0;
}
// acknowledge pluse
DATA_X = 0;
DATA = 0;
CLK = 1;
CLK = 0;
DATA_X = 1;
// get second byte LSB
for(i=0; i<8; i++){
temp <<= 1;
CLK = 1;
if(DATA) temp |= 1;
CLK = 0;
}
// return value without CRC
return temp;
}

void delay100ms(){
delay_ms(100);
}

void writef(char *str, char pt){
char i = 0;
while(*str != '.') {
TXREG = *str;
while(!TXSTA.TRMT);
str++;
}
while(i <= pt){
TXREG = *str;
while(!TXSTA.TRMT);
str++;
i++;
}
}

void main(){
ANSELH = 0;
TRISB = 0;
PORTB = 0;
Uart1_init(9600); // initail USART
//---------- Turn off cursor and clear screen -------
Uart1_Write(0xFE);
Uart1_Write(12);
delay100ms();
LCD_Clear_Screen;
delay100ms();
while(1){
//read adc temperature value from SHT
tempt = SHT_Measure(_TEMP);
//calculate temperature in Celsius from value
//formula t = d1 + d2*SOt
tempt = d1C + (tempt * d2C);
//convert to string
floatToStr(tempt,txt);
//send string to LCD display
LCD_Home;
delay100ms();
UART1_Write_Text("TEMP: ");
writef(txt,1);
Uart1_Write(0xDF);
Uart1_Write('C');
//Usart_Send(13);
//read adc humidity value from SHT
humi = SHT_Measure(_HUMI);
//calculate humidity %RH from value
//formula RHlinear = C1 + C2*SOrh + C3*SOrh*SOrh %RH
RH_linear = C1 + (C2*humi) + (C3*humi*humi);
//formula RHtrue = (Tc-25)*(t1+t2*SOrh)+RHlinear
RH_true = (tempt-25)*(t1+(t2*humi))+ RH_linear;
if(RH_true > 100.0) RH_true = 100.0;
if(RH_true < 0.1) RH_true = 0.1;
//convert to string
floatToStr(RH_true,txt);
//send string to LCD display
LCD_Second_line;
delay100ms();
UART1_Write_Text("HUMI: ");
writef(txt,1);
Uart1_Write('%');
//Usart_Send(13);
delay_ms(1000);
}
}
 

    V

    Points: 2
    Helpful Answer Positive Rating
Re: sht11 pic16f877

please send me the code code for SHT11
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top