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

Driver for SHT21

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

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

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

and the code is:

///                               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){

void SoftResett(){

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

      movf lsbtemp,0
      movwf 0x00A
      movf msbtemp,0
      movwf 0x00B
   temperies1= (-46.85 + (175.72*(temperiess/65536)));
   //return temperies1;

void sht21_humid(float &humidum1){
   //float humidum1;
   int8 msbhum, lsbhum;
   int16 humidum;
   float humidumm;
   msbhum = i2c_read();
   lsbhum = i2c_read();
   checksum = i2c_read();
   humidum = make16(msbhum,lsbhum);
      movf lsbhum,0
      movwf 0x00C
      movf msbhum,0
      movwf 0x00D
   humidum1= (-6 + (125*(humidumm/65536)));
   //return humidum1;

void sht21_both(float &tmp, float &humd){
   /*float temp;
   float humedad;*/

an example of how to use:

///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
#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;
   lcd_putc("\fSensor de \nTemperatura y\nHumedad  SHT21");

      printf(lcd_putc, "\fTemp: %f %cC",tempi, signo);
      printf(lcd_putc, "\nRH: %f %%",humedadi);

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.

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:
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.

msbtemp = i2c_read();
lsbtemp = i2c_read();
checksum = i2c_read();

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

He has this mistake in these routines:
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