$60 off LOAD-n-GO Handheld Programmer
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 support@ccsinfo.com

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

View user's profile Send private message

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