CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Driver for SHT21

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
pictrance



Joined: 18 Jul 2010
Posts: 14
Location: Puebla, Mexico

View user's profile Send private message Visit poster's website

Driver for SHT21
PostPosted: Tue Jun 28, 2011 2:52 pm     Reply with quote

hi!
I want to share with you the driver for the sht21 (temperature and humidity sensor)

and the code is:

Code:
////////////////////////////////////////////////////////////////////////////////
///                               sht21.c                                    ///
///            Driver for temperature and humidity sensor                    ///
///            Realizado por PicTrance                                       ///
///            junio del 2011, Puebla - México                               ///
///            Compiler: CCS                                                 ///
///                                                                          ///
/// sht21_init()  - Iicializa el sensor sht21 -                              ///
///                                                                          ///
///                                                                          ///
/// sht21_temp(temperatura)      Get the temperature, lee tempeartura        ///
///                                                                          ///
/// sht21_humid(humedad)         Get the humidity, lee humedad               ///
///                                                                          ///
/// sht21_both(temp,humd)        Get the temperature and humidity            ///
///                              Lee temperatura y humedad                   ///
////////////////////////////////////////////////////////////////////////////////

#define sht21     0b10000000
#define writeuser 0b11100110
#define readuser  0b11100111
#define reset     0b11111110
#define temphold  0b11100011
#define humidhold 0b11100101
#define read      0b10000001
#define softreset 0b11111110

/*int8 msbhum, lsbhum, msbtemp, lsbtemp, checksum;
int16 temperies, humidum;
float temperiess, humidumm;*/

int8 checksum;

void sht21_init(void){
   i2c_start();
   i2c_write(sht21);
   i2c_write(softreset);
   i2c_stop();
   delay_ms(15);
}

void SoftResett(){
   i2c_start();
   i2c_write(sht21);
   i2c_write(softreset);
   i2c_stop();
   delay_ms(15);
}

void sht21_temp(float &temperies1){
   //float temperies1;
   int8 msbtemp, lsbtemp;
   int16 temperies;
   float temperiess;
   i2c_start();
   i2c_write(sht21);
   i2c_write(temphold);
   i2c_start();
   i2c_write(read);
   msbtemp = i2c_read();
   lsbtemp = i2c_read();
   checksum = i2c_read();
   //i2c_read(0);
   i2c_stop();
   temperies = make16(msbtemp,lsbtemp);//valiable = MAKE16(varhigh, varlow) junta las
                                  //2 variables in8 en una in16

   /*#ASM
      movf lsbtemp,0
      movwf 0x00A
      movf msbtemp,0
      movwf 0x00B
   #ENDASM*/
   
   temperiess=temperies;
   temperies1= (-46.85 + (175.72*(temperiess/65536)));
   SoftResett();
   //return temperies1;
}

void sht21_humid(float &humidum1){
   //float humidum1;
   int8 msbhum, lsbhum;
   int16 humidum;
   float humidumm;
   i2c_start();
   i2c_write(sht21);
   i2c_write(humidhold);
   i2c_start();
   i2c_write(read);
   //delay_ms(25);
   msbhum = i2c_read();
   lsbhum = i2c_read();
   checksum = i2c_read();
   //i2c_read(0);
   i2c_stop();
   
   humidum = make16(msbhum,lsbhum);
   
   /*#ASM
      movf lsbhum,0
      movwf 0x00C
      movf msbhum,0
      movwf 0x00D
   #ENDASM*/
   
   humidumm=humidum;
   humidum1= (-6 + (125*(humidumm/65536)));
   SoftResett();
   //return humidum1;
}

void sht21_both(float &tmp, float &humd){
   /*float temp;
   float humedad;*/
   sht21_temp(tmp);
   delay_us(800);
   sht21_humid(humd);
}


an example of how to use:

Code:
///Ejemplo de como usar el driver
///sht21.c, creado por PicTrance
///para el foro de Neoteo y el foro de CCS
///funciona para el compilador CCS

#include <18f4550.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20M)
//Pal bootloader
#build (reset=0x1000,interrupt=0x1008)
#org 0x0000,0x0FFF{}

//#use i2c(Master,fast,sda=PIN_C7,scl=PIN_C6)
#use i2c(Master,fast,sda=PIN_A4,scl=PIN_A5)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include "lcd4x20.c"
#include "sht21.c"
int signo = 0xDF;//simbolo "°"

void main(){
   float tempi,humedadi;
   SETUP_ADC(ADC_OFF);
   lcd_init();
   set_tris_b(0x00);
   set_tris_c(0b00000000);
   lcd_putc("\fSensor de \nTemperatura y\nHumedad  SHT21");
   delay_ms(500);
   output_c(0b11111000);
   sht21_init();
   
 
   delay_ms(300);

   while(true){
     
      sht21_temp(tempi);
      delay_ms(100);
      sht21_humid(humedadi);
      //sht21_both(tempi,humedadi);
      printf(lcd_putc, "\fTemp: %f %cC",tempi, signo);
      printf(lcd_putc, "\nRH: %f %%",humedadi);
      delay_ms(1000);
   }
}


I hope that this driver helps someone
_________________
Si el proyecto tiene mal olor es de Química, Si echa humo negro es de Mecánica, Si es verde o se retuerce es de Bioingenieria Y si no funciona es de Electrónica.. :p ... no es mia la frase, pero me gusto.
Pichuqy_1



Joined: 03 Aug 2010
Posts: 38

View user's profile Send private message MSN Messenger

PostPosted: Tue Jul 28, 2015 8:46 pm     Reply with quote

I tried and not worked.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 29, 2015 3:14 pm     Reply with quote

Here's the SHT21 data sheet:
http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_SHT21_Datasheet_V4.pdf
Look at the i2c protocol bit diagrams on page 8. Notice how they read the
data (msb), data (lsb), and checksum bytes, and then the PIC issues a
NACK. In his code below, he doesn't do the NACK. This is a violation of
the SHT21 data sheet. If you skip the NACK on the last read, I know for
a fact that you will get bad data on subsequent reads on many i2c chips.
I have seen this happen while testing i2c eeprom chips to study the effect
of a missing NACK.
Quote:

i2c_write(read);
msbtemp = i2c_read();
lsbtemp = i2c_read();
checksum = i2c_read();
//i2c_read(0);
i2c_stop();

The line in bold should be:
Code:
checksum = i2c_read(0);


He has this mistake in these routines:
sht21_temp()
sht21_humid()
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group