Are SHT11 & 18S20 sensors able to work together ?

General discussion on mikroC.
Post Reply
Author
Message
crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Are SHT11 & 18S20 sensors able to work together ?

#1 Post by crocu » 13 Feb 2010 10:39

Hello,

Has someone already be able to have Sensirion sensor SHTxx + Dallas 18S20 displaying temperatures on a LCD ?

I have 2 working codes for :

SHT11
and
18S20

They are both working well separatly but when i try to combine the codes into ONE project, i can't get it working.
In fact, I would like to have SHT11 temp & humidity datas displayed on a LCD and also 18S20 temp displayed on another LCD row.

It is possible ?

If someone can post a working example i will really appreciate it.
Many thanks for your help,

Code: Select all

/*******************************************************************************
      CompiLED by: Mikro C v8.0.0.0

      PIC 16F876A @ 4MHz.
      Description: SHT11 + DS18S20 :
 
      Pins configuration :

      - SHT11 SCK on RC4
      - SHT11 Data on RC5

      - 18S20 on RC0

      LCD on port B :
      - RS on RB2
      - E on RB3
      - D4 on RB4
      - D5 on RB5
      - D6 on RB6
      - D7 on RB7

*******************************************************************************/

// Adding a Custom LCD character : °C  

const char degre[] = {8,20,8,0,3,4,4,3};

void symbol_degre(char pos_row, char pos_char) {
  char i;
    LCD_Cmd(64);
    for (i = 0; i<=7; i++) LCD_Chr_Cp(degre[i]);
    LCD_Cmd(LCD_RETURN_HOME);
    LCD_Chr(pos_row, pos_char, 0);}

typedef union
{ unsigned int i;
  float f;
} value;

//////////////////////////
// Adding 18S20 code :

// Set temp_dallas_RESOLUTION to the corresponding resolution of your DS18x20 sensor:
//  18S20: 9
//  18B20: 12 (default setting; can be 9,10,11,or 12)
const unsigned short temp_dallas_RESOLUTION = 9;

const int RES_FACTOR_1[4] = {5000, 2500, 1250, 625};
const unsigned int RES_FACTOR_2[4] = {0x0001, 0x0003, 0x0007, 0x000F};
const unsigned int RES_FACTOR_3[4] = {0x8000, 0xC000, 0xE000, 0xF000};

float alarma;

unsigned temp_dallas,temp_dallas2,new_temp_dallas;
unsigned short  j, RES_SHIFT,j2;


///////////////////DISPLAY FOR ONE DEVICE//////////////////
  void Display_temp_dallas(unsigned int temp_dallas) {
  const unsigned short RES_SHIFT = temp_dallas_RESOLUTION - 8;
  unsigned int temp_dallas_whole, temp_dallas_fraction;
  unsigned short i;
  char text[8];

  // Isolate the fraction and make it a 4-digit decimal integer (for display)
  temp_dallas_fraction = temp_dallas & RES_FACTOR_2[RES_SHIFT - 1];
  temp_dallas_fraction = temp_dallas_fraction * RES_FACTOR_1[RES_SHIFT - 1];
  //portc = temp_dallas_fraction;
  // Handle the whole part of temp_dallaserature value
  temp_dallas_whole = temp_dallas;

  // Is temp negative?
  if ((temp_dallas_whole & 0x8000) != 0u) i = 1;  // Yes, i = 1
  else i = 0;                              // No,  i = 0
  PORTC = i;
  // Remove the fractional part
  temp_dallas_whole >>= RES_SHIFT;

  // Correct the sign if necessary
  if (i) temp_dallas_whole |= RES_FACTOR_3[RES_SHIFT - 1];

  //portd = temp_whole;
  IntToStr(temp_dallas_whole, text);  // Convert whole part to string
  Lcd_Out(4, 10, text);         // Print whole part on LCD
  Lcd_Chr_Cp('.');             // Print dot to separate fractional part


  IntToStr(temp_dallas_fraction, text); // Convert fractional part to string

  // Add leading zeroes (we display 4 digits fractional part)
  if (temp_dallas_fraction < 1000u) Lcd_Chr_Cp('0');
  if (temp_dallas_fraction < 100u)  Lcd_Chr_Cp('0');
  if (temp_dallas_fraction < 10u)   Lcd_Chr_Cp('0');

  Lcd_Out_Cp(text);            // Print fractional part on LCD

  Lcd_Chr_Cp(223);             // Print degree character
  Lcd_Chr_Cp('C');             // Print 'C' for Centigrades
}//~

// End code 18S20

//////////////////////////
// Added for AN reading :
unsigned int hold;
char txt[7];
//////////////////////////

// Init SHT 11 :
//------------------------------------------------------------------------------
// modul-var
//------------------------------------------------------------------------------
//enum {TEMP,HUMI};

#define TEMP 0
#define HUMI 1

// SHT sensor : RC4:data - RC5:SCK
#define PORT_COND TRISC.F4    //data port condition: input/output
#define DATA PORTC.F4         //SHT11 DATA pin
#define SCK PORTC.F5          //STH11 SCK pin

#define noACK 0
#define ACK 1
//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
#define MEMORY_ADDRESS  0xA0

// Definition for Inputs & Outputs :
#define BP PORTC.F6      // BP on RC6 ( input )
#define LED1 PORTC.F1    // LED1 on RC1 ( output )
#define LED2 PORTC.F2    // LED2 on RC2 ( output )
#define LED3 PORTC.F3    // LED3 on RC3 ( output )
//#define BP1 PORTA.F0     // BP1 on RA0 ( input )

/************************************************************
                     Initializations
************************************************************/

void Init(void)
 {


 TRISA = 0xFF; //PORTA as input
 ADCON1 = 0x0E; // Set AN0 channel pin as analog
 CMCON |= 7; // Disable comparators

  PORTB = 0x00;     //initialize PORTB
  TRISB = 0;

  Lcd_Init(&PORTB);
  Lcd_Cmd(LCD_CLEAR);       // Clear display
  Lcd_Cmd(LCD_CURSOR_OFF);  // Turn cursor off

  PORTC = 0x00;
  TRISC = 0B00000001; // RC0 = input - RC1 - RC7 = outputs

 
  }


//------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowLED1ge
{
   unsigned char i,error=0;

   PORT_COND = 0;        //SHT11 WRITE


   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     Delay_us(2);
     if (i & value)
         DATA=1; //masking value with i , write to SENSI-BUS
     else
         DATA=0;
     SCK=1; //clk for SENSI-BUS

     Delay_us(2);   //pulswith approx. 5 us

     SCK=0;
   }

   DATA=1; //release DATA-line

   PORT_COND = 1;    //SHT11 READ

   Delay_us(2);

   SCK=1; //clk #9 for ack

   Delay_us(2);
   error = DATA; //check ack (DATA will be pulLED1 down by SHT11)

   SCK=0;

   return error; //error=1 in case of no acknowLED1ge
}


//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowLED1ge in case of "ack=1"
{
   unsigned char i,val=0;

   PORT_COND = 1;        //SHT11 READ


   DATA=1; //release DATA-line

   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     Delay_us(2);
     SCK=1; //clk for SENSI-BUS

     if (DATA) val=(val | i); //read bit
     Delay_us(2);

     SCK=0;
   }

   Delay_us(2);
   PORT_COND = 0;        //SHT11 WRITE
   Delay_us(2);

   DATA=!ack; //in case of "ack==1" pull down DATA-Line
   SCK=1; //clk #9 for ack
   Delay_us(2); //pulswith approx. 5 us
   SCK=0;
   Delay_us(2);
   DATA=1; //release DATA-line

   return val;
}


//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
//       _____         ________
// DATA:      |_______|
//           ___     ___
// SCK : ___|   |___|   |______
{
   PORT_COND = 0;   //SHT11 WRITE

   DATA=1;
   SCK=0;   //Initial state
   Delay_us(2);
   SCK=1;
   Delay_us(2);
   DATA=0;
   Delay_us(2);
   SCK=0;
   Delay_us(2);
   SCK=1;
   Delay_us(2);
   DATA=1;
   Delay_us(2);
   SCK=0;
}


//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//       _____________________________________________________         ________
// DATA:                                                      |_______|
//          _    _    _    _    _    _    _    _    _        ___     ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
{
   unsigned char i;

   PORT_COND = 0;  //SHT11 WRITE


   DATA=1; SCK=0;  //Initial state

   for(i=0;i<9;i++) //9 SCK cycles
   {
     Delay_us(2);
     SCK=1;
     Delay_us(2);
     SCK=0;
   }

   Delay_us(2);

   s_transstart(); //transmission start
}


//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
   unsigned char error=0;

   s_connectionreset(); //reset communication

   error+=s_write_byte(RESET); //send RESET-command to sensor
   return error; //error=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
   unsigned char error=0;

   s_transstart(); //transmission start

   error=s_write_byte(STATUS_REG_R); //send command to sensor

   //Delay_us(100);

   *p_value=s_read_byte(ACK); //read status register (8-bit)
   *p_checksum=s_read_byte(noACK); //read checksum (8-bit)

   return error; //error=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
   unsigned char error=0;

   s_transstart(); //transmission start

   error+=s_write_byte(STATUS_REG_W);//send command to sensor
   error+=s_write_byte(*p_value); //send value of status register

   return error; //error>=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
   unsigned error=0;
   unsigned int i;

   s_transstart(); //transmission start

   switch(mode)
   { //send command to sensor
   case TEMP : error+=s_write_byte(MEASURE_TEMP); break;   //temp
   case HUMI : error+=s_write_byte(MEASURE_HUMI); break;   //humidity
   default : break;
   }

   PORT_COND = 1;  //SHT11 READ


   for (i = 0; i < 65535 && DATA; i++) Delay_us(10); //wait until sensor has finished the measurement

   //while (DATA == 1) Delay_us(1);

   if(DATA) error+=1; // or timeout (~2 sec.) is reached

   *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
   *(p_value) =s_read_byte(ACK); //read the first byte (MSB)



   *p_checksum =s_read_byte(noACK); //read checksum

   return error;
}

/*
//----------------------------------------------------------------------------------
void init_uart()
//----------------------------------------------------------------------------------
// Initializes the UART so the final data can be sent away, e.g. to a PC
//9600 bps @ 11.059 MHz
{     SCON = 0x52;
      TMOD = 0x20;
      TCON = 0x69;
      TH1 = 0xfd;
}
*/

//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [C]
{  const float C1=-4.0; // for 12 Bit
   const float C2= 0.0405; // for 12 Bit
   const float C3=-0.0000028; // for 12 Bit
   const float T1=0.01; // for 14 Bit @ 5V
   const float T2=0.00008; // for 14 Bit @ 5V
   float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
   float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
   float rh_lin; // rh_lin: Humidity linear
   float rh_true; // rh_true: Temperature compensated humidity
   float t_C; // t_C : Temperature [C]

   t_C=t*0.01-40; //calc. Temperature from ticks to [C]

   rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]

   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]

   if(rh_true>100)rh_true=100; //cut if the value is outside of

   if(rh_true<0.1)rh_true=0.1; //the physical possible range

   *p_temperature=t_C; //return temperature [C]
   *p_humidity=rh_true; //return humidity[%RH]
}


//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [C]
// output: dew point [C]
{  float k,dew_point ;

   k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);

   dew_point = 243.12*k/(17.62-k);

   return dew_point;
}

//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
{

// SHT 11 management :

// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point

   value humi_val,temp_val;
   float dew_point;
   unsigned char error,checksum;
   //unsigned int i;

   char temp[13];
   char humi[13];
   char dew[13];

   Init();    //Initialize LCD & Pins
   s_connectionreset();
   Delay_ms(20);







   while(1)
   {

     error=0;

     error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
     //measure humidity

     error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
     //measure temperature

     if(error!=0) s_connectionreset(); //in case of an error: connection reset
     else
     {
       humi_val.f=(float)humi_val.i; //converts integer to float
       temp_val.f=(float)temp_val.i; //converts integer to float


       calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature

       dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point

       //send final data to serial interface (UART)

       //Lcd_Out(1,1,Out_temp());
       Lcd_Out(1,1,("Temp ext"));
       FloatToStr(temp_val.f, temp);
       Lcd_Out(1,11,temp);
       symbol_degre(1,16);

       Lcd_Out(2,1,("H"));
       FloatToStr(humi_val.f, humi);
       Lcd_Out(2,3,humi);
       Lcd_Out(2,8,("%"));

       // Ajout semble fonctionner :
       Lcd_Out(2,9,(" Dp "));
       FloatToStr(dew_point, dew);
       Lcd_Out(2,13,dew);

     }

   // start Code 18S20
    ////////////////// RESET DEVICE 1///////////////////
    Ow_Reset(&PORTC,0);        // Onewire reset signal
    Ow_Write(&PORTC,0,0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTC,0,0x44);   // Issue command CONVERT_T
    Delay_us(120);

    Ow_Reset(&PORTC,0);
    Ow_Write(&PORTC,0,0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTC,0,0xBE);   // Issue command READ_SCRATCHPAD
    Delay_ms(400);

    /*******************************************************/
    /////////////            18S20 TEMPERATURE DISPLAY          ///////////////////

    j = Ow_Read(&PORTC,0);     // Get temperature LSB
    temp_dallas = Ow_Read(&PORTC,0);  // Get temperature MSB
    temp_dallas <<= 8; temp_dallas += j;     // Form the result
    Lcd_Out(4,8,"Cabin Temp");
    Display_temp_dallas(temp_dallas); // Format and display result on LCD

   //Delay_ms(500);

  // end CODE 18S20


//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
     Delay_ms(1500);

   }

}

dariush_abbasi
Posts: 120
Joined: 11 May 2008 18:47

Re: Are SHT11 & 18S20 sensors able to work together ?

#2 Post by dariush_abbasi » 13 Feb 2010 19:07

Bonjour.
You should add the 18s20 code carefully ,for example it won't work without

Code: Select all

ADCON1 = 7;
.
Try to add one of the code to the other step by step in order to get the problem.

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Are SHT11 & 18S20 sensors able to work together ?

#3 Post by crocu » 14 Feb 2010 08:30

Hello,

Do you mean 18S20 will not work if AN pins are used in PORT A ? ( i'm trying to use 18S20 on RC0 , not from PORT A pins)
I need to have AN converters on port A for my project, do you confirm that 18S20 will not work properly if AN is enabled ?

I tried to add 18S20 code to SHT11 code, all works fine until i place the following part of code at the end of infinite loop :
If i comment it, SHT11 sensor works fine and displayed values are correct :

Code: Select all

   // start Code 18S20
    ////////////////// RESET DEVICE 1///////////////////
    Ow_Reset(&PORTC,0);        // Onewire reset signal
    Ow_Write(&PORTC,0,0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTC,0,0x44);   // Issue command CONVERT_T
    Delay_us(120);

    Ow_Reset(&PORTC,0);
    Ow_Write(&PORTC,0,0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTC,0,0xBE);   // Issue command READ_SCRATCHPAD
    Delay_ms(400);

    /*******************************************************/
    /////////////  TEMPERATURE 1 DISPLAY ///////////////////

    j = Ow_Read(&PORTC,0);     // Get temperature LSB
    temp_dallas = Ow_Read(&PORTC,0);  // Get temperature MSB
    temp_dallas <<= 8; temp_dallas += j;     // Form the result
    Lcd_Out(4,8,"Cabin Temp");
    Display_temp_dallas(temp_dallas); // Format and display result on LCD

   //Delay_ms(500);

  // end CODE 18S20
When inserted, this part of code generates compilation errors ( i do not get them if comment it out ) especially these one : CONST TRUNCATED

Code: Select all

error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);

Code: Select all

error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
Do you have any idea ?
Many thanks,

dariush_abbasi
Posts: 120
Joined: 11 May 2008 18:47

Re: Are SHT11 & 18S20 sensors able to work together ?

#4 Post by dariush_abbasi » 14 Feb 2010 19:02

Hi.
Give me your working SHT code.
I'll add the DS1820 to it for you.
Bye.

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Are SHT11 & 18S20 sensors able to work together ?

#5 Post by crocu » 14 Feb 2010 19:43

Hello

Here it is, please add DS18S20 on RC0 pin.

http://www.filefactory.com/file/b019b5f ... 6F876A.rar
( go at end of the page and click : " Download now with File Factory Basic "

dariush_abbasi
Posts: 120
Joined: 11 May 2008 18:47

Re: Are SHT11 & 18S20 sensors able to work together ?

#6 Post by dariush_abbasi » 14 Feb 2010 21:29

Bon nuit,ici est nuit.
Below is a fully functional code,but there are some differences in my hardware,change them according to your own.
1-I've used PIC18F877A with 8M.
2-I've used PORTD for both SHT sensor & LCD and RE2 for DS1820 sensor.

Code: Select all


/*******************************************************************************
      Filename   : SHT11

      CompiLED1 by: Mikro C v8.0.0.0

      Description: main program of SHT11 humidity & temperature sensor
                        modified from Keil's codes.

      PIC 16F877A~4MHz.
*******************************************************************************/

//------------------------------------------------------------------------------
// Custom character :
// in order to display the following °c as one character.

const unsigned short TEMP_RESOLUTION = 9;

char *text = "000.00";
unsigned temp1;


   unsigned char error,checksum;
   //unsigned int i;

   char temp[13];
   char humi[13];
   char dew[13];


const char degre[] = {8,20,8,0,3,4,4,3};

void symbol_degre(char pos_row, char pos_char) {
  char i;
    LCD_Cmd(64);
    for (i = 0; i<=7; i++) LCD_Chr_Cp(degre[i]);
    LCD_Cmd(LCD_RETURN_HOME);
    LCD_Chr(pos_row, pos_char, 0);}
//------------------------------------------------------------------------------

typedef union
{ unsigned int i;
  float f;
} value;
value humi_val,temp_val;

//------------------------------------------------------------------------------
// modul-var
//------------------------------------------------------------------------------
//enum {TEMP,HUMI};

#define TEMP 0
#define HUMI 1

// SHT sensor : RC4:data - RC5:SCK
#define PORT_COND TRISD.F1    //data port condition: input/output
#define DATA PORTD.F1         //SHT11 DATA pin
#define SCK PORTD.F0          //STH11 SCK pin

#define noACK 0
#define ACK 1
//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
#define MEMORY_ADDRESS  0xA0

/***********************************************************/

void Display_Temperature(unsigned int temp2write) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  char temp_whole;
  unsigned int temp_fraction;

  // check if temperature is negative
  if (temp2write & 0x8000) {
     text[0] = '-';
     temp2write = ~temp2write + 1;
     }

  // extract temp_whole
  temp_whole = temp2write >> RES_SHIFT ;

  // convert temp_whole to characters
  if (temp_whole/100)
     text[0] = temp_whole/100  + 48;

  text[1] = (temp_whole/10)%10 + 48;             // extract tens digit
  text[2] =  temp_whole%10     + 48;             // extract ones digit

  // extract temp_fraction and convert it to unsigned int
  temp_fraction  = temp2write << (4-RES_SHIFT);
  temp_fraction &= 0x000F;
  temp_fraction *= 625;

  // convert temp_fraction to characters
  text[4] =  temp_fraction/1000    + 48;         // extract thousands digit
  text[5] = (temp_fraction/100)%10 + 48;         // extract hundreds digit
  text[6] = (temp_fraction/10)%10  + 48;         // extract tens digit
  text[7] =  temp_fraction%10      + 48;         // extract ones digit

  // print temperature on LCD
  Lcd_Out(2, 12, text);
}//~
///////////////////////////////////////////////////////////////////////////////
void Initlcd(void)
 {


TRISA = 0xFF; //PORTA as input
ADCON1 = 0x0E; // Set AN0 channel pin as analog
CMCON |= 7; // Disable comparators


  //PORTA = 0x00;
  //ADCON1 = 0xff;
  //TRISA = 0B00000001; // RA0 = input - RA1 - RA7 = outputs

  PORTB = 0x00;     //initialize PORTB
  TRISB = 0;

  PORTC = 0x00;
  TRISC = 0B00000001; // RC0 = input - RC1 - RC7 = outputs



  //Lcd_Config(&PORTB, 0, 1, 7, 5, 4, 3, 2);
  Lcd_Init(&PORTD);
  Lcd_Cmd(LCD_CLEAR);       // Clear display
  Lcd_Cmd(LCD_CURSOR_OFF);  // Turn cursor off
  }


//------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowLED1ge
{
   unsigned char i,error=0;

   PORT_COND = 0;        //SHT11 WRITE


   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     Delay_us(2);
     if (i & value)
         DATA=1; //masking value with i , write to SENSI-BUS
     else
         DATA=0;
     SCK=1; //clk for SENSI-BUS

     Delay_us(2);   //pulswith approx. 5 us

     SCK=0;
   }

   DATA=1; //release DATA-line

   PORT_COND = 1;    //SHT11 READ

   Delay_us(2);

   SCK=1; //clk #9 for ack

   Delay_us(2);
   error = DATA; //check ack (DATA will be pulLED1 down by SHT11)

   SCK=0;

   return error; //error=1 in case of no acknowLED1ge
}


//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowLED1ge in case of "ack=1"
{
   unsigned char i,val=0;

   PORT_COND = 1;        //SHT11 READ


   DATA=1; //release DATA-line

   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     Delay_us(2);
     SCK=1; //clk for SENSI-BUS

     if (DATA) val=(val | i); //read bit
     Delay_us(2);

     SCK=0;
   }

   Delay_us(2);
   PORT_COND = 0;        //SHT11 WRITE
   Delay_us(2);

   DATA=!ack; //in case of "ack==1" pull down DATA-Line
   SCK=1; //clk #9 for ack
   Delay_us(2); //pulswith approx. 5 us
   SCK=0;
   Delay_us(2);
   DATA=1; //release DATA-line

   return val;
}


//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
//       _____         ________
// DATA:      |_______|
//           ___     ___
// SCK : ___|   |___|   |______
{
   PORT_COND = 0;   //SHT11 WRITE

   DATA=1;
   SCK=0;   //Initial state
   Delay_us(2);
   SCK=1;
   Delay_us(2);
   DATA=0;
   Delay_us(2);
   SCK=0;
   Delay_us(2);
   SCK=1;
   Delay_us(2);
   DATA=1;
   Delay_us(2);
   SCK=0;
}


//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//       _____________________________________________________         ________
// DATA:                                                      |_______|
//          _    _    _    _    _    _    _    _    _        ___     ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
{
   unsigned char i;

   PORT_COND = 0;  //SHT11 WRITE


   DATA=1; SCK=0;  //Initial state

   for(i=0;i<9;i++) //9 SCK cycles
   {
     Delay_us(2);
     SCK=1;
     Delay_us(2);
     SCK=0;
   }

   Delay_us(2);

   s_transstart(); //transmission start
}


//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
   unsigned char error=0;

   s_connectionreset(); //reset communication

   error+=s_write_byte(RESET); //send RESET-command to sensor
   return error; //error=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
   unsigned char error=0;

   s_transstart(); //transmission start

   error=s_write_byte(STATUS_REG_R); //send command to sensor

   //Delay_us(100);

   *p_value=s_read_byte(ACK); //read status register (8-bit)
   *p_checksum=s_read_byte(noACK); //read checksum (8-bit)

   return error; //error=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
   unsigned char error=0;

   s_transstart(); //transmission start

   error+=s_write_byte(STATUS_REG_W);//send command to sensor
   error+=s_write_byte(*p_value); //send value of status register

   return error; //error>=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
   unsigned error=0;
   unsigned int i;

   s_transstart(); //transmission start

   switch(mode)
   { //send command to sensor
   case TEMP : error+=s_write_byte(MEASURE_TEMP); break;   //temp
   case HUMI : error+=s_write_byte(MEASURE_HUMI); break;   //humidity
   default : break;
   }

   PORT_COND = 1;  //SHT11 READ


   for (i = 0; i < 65535 && DATA; i++) Delay_us(10); //wait until sensor has finished the measurement

   //while (DATA == 1) Delay_us(1);

   if(DATA) error+=1; // or timeout (~2 sec.) is reached

   *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
   *(p_value) =s_read_byte(ACK); //read the first byte (MSB)



   *p_checksum =s_read_byte(noACK); //read checksum

   return error;
}


//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [C]
{  const float C1=-4.0; // for 12 Bit
   const float C2= 0.0405; // for 12 Bit
   const float C3=-0.0000028; // for 12 Bit
   const float T1=0.01; // for 14 Bit @ 5V
   const float T2=0.00008; // for 14 Bit @ 5V
   float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
   float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
   float rh_lin; // rh_lin: Humidity linear
   float rh_true; // rh_true: Temperature compensated humidity
   float t_C; // t_C : Temperature [C]

   t_C=t*0.01-40; //calc. Temperature from ticks to [C]

   rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]

   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]

   if(rh_true>100)rh_true=100; //cut if the value is outside of

   if(rh_true<0.1)rh_true=0.1; //the physical possible range

   *p_temperature=t_C; //return temperature [C]
   *p_humidity=rh_true; //return humidity[%RH]
}



//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point


{
   ADCON1 = 7;



   Initlcd();    //Initialize LCD

   s_connectionreset();
   Delay_ms(20);

   while(1)
   {
    Ow_Reset(&PORTE,2);            // Onewire reset signal
    Ow_Write(&PORTE,2,0xCC);       // Issue command SKIP_ROM
    Ow_Write(&PORTE,2,0x44);       // Issue command CONVERT_T
    Delay_us(120);

    Ow_Reset(&PORTE,2);
    Ow_Write(&PORTE,2,0xCC);       // Issue command SKIP_ROM
    Ow_Write(&PORTE,2,0xBE);       // Issue command READ_SCRATCHPAD

    temp1 =  Ow_Read(&PORTE,2);
    temp1 = (Ow_Read(&PORTE,2) << 8) + temp1;

    //--- Format and display result on Lcd
    Display_Temperature(temp1);
     error=0;

     error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
     //measure humidity

     error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
     //measure temperature

     if(error!=0) s_connectionreset(); //in case of an error: connection reset
     else
     {
       humi_val.f=(float)humi_val.i; //converts integer to float
       temp_val.f=(float)temp_val.i; //converts integer to float


       calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature


       Lcd_Out(1,1,("Temp ext"));
       FloatToStr(temp_val.f, temp);
       Lcd_Out(1,11,temp);
       symbol_degre(1,16);

       Lcd_Out(2,1,("H"));
       FloatToStr(humi_val.f, humi);
       Lcd_Out(2,3,humi);
       Lcd_Out(2,8,("%"));

     }


     
//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
     Delay_ms(1500);

   }

}
By the way your program is so untidy.
être couronné de succès.
excuse moi pour mon francais.
J'aime votre langue mais...

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Are SHT11 & 18S20 sensors able to work together ?

#7 Post by crocu » 14 Feb 2010 22:32

Thanks, i tried you code but i get 085.0000 displayed for 18S20 value.

The value is static and stays the same even i heat the sensor.
85.0000 is also too much , i was expecting something arround 21.5°C ...

Normaly the displayed value for 18S20 should change every program loop, but it does not ...

By the way, are you sure that resolution has to be set to "9" for 18S20 ?
Shouldn't be resolution = 11 ?

Do you use pull-up resistor with 18S20, for my test i do .

Bonne soirée,

dariush_abbasi
Posts: 120
Joined: 11 May 2008 18:47

Re: Are SHT11 & 18S20 sensors able to work together ?

#8 Post by dariush_abbasi » 15 Feb 2010 09:01

Hi,
I think there is something wrong with your hardware.
I think the resolution for DS18S20 is 9 and you can't use 11.
If you need more,you can use DS18B20 with the resolution of 12.
Also,the pull up is needed.

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Are SHT11 & 18S20 sensors able to work together ?

#9 Post by crocu » 15 Feb 2010 15:40

What Pic clock fuses did you set in Mikro C to get that code working well ?

Did you set internal rc clock or external clock ?


Many thanks,

dariush_abbasi
Posts: 120
Joined: 11 May 2008 18:47

Re: Are SHT11 & 18S20 sensors able to work together ?

#10 Post by dariush_abbasi » 16 Feb 2010 16:48

Hi,
I've used the default.
Bye.

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Are SHT11 & 18S20 sensors able to work together ?

#11 Post by crocu » 16 Feb 2010 18:56

Here are some news :

I replaced my 18S20 by a 1820 and now i get the code working well with resolution "9"

it seems that 18S20 is not accepted by the code, i don't know why because it should be fully compatible with 1820.

Has someone already experienced this issue ?


I ordered a 18B20 because i need more occurate results, i will test it with resolution "11".

NUFAN
Posts: 65
Joined: 08 Dec 2009 20:15

Re: Are SHT11 & 18S20 sensors able to work together ?

#12 Post by NUFAN » 16 Feb 2010 20:00

If you use 18B20, example program will not work with 11 bit resolution. It work only with 12 bit resolution. Why? I don't know :?

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Are SHT11 & 18S20 sensors able to work together ?

#13 Post by crocu » 04 Jun 2010 07:50

Hello

I'm trying to port my working code from 16F876A to 18F2620 ( i need more RAM to store LCD messages )
These chips are pins compatibles.

However, since i changed for 18F2620 my project still compile properly but the LCD on my board won't initialise.

Can you advise me regarding fuse configuration with 18F2620 ?
I kept the same clock configuration that was working fine with my 16F876A :
---> 4Mhz Crystal + 2 x 18pF ceramics cap

I built my new 18F2620 project with default fuses settings , did i done something wrong ?
Do i have to go with 10Mhz Crystal with 18F2620, if so are SHT11 and 18S20 will work fine at this speed ?

Many thanks for your help,

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re: Are SHT11 & 18S20 sensors able to work together ?

#14 Post by piort » 05 Jun 2010 21:56

hi,
if your LCD are connected on portb B , chk if you have it configure as digital (RB4 is special in the 2620 familly) in the edit projet windows...

hth a bit ;-)

Post Reply

Return to “mikroC General”