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 support@ccsinfo.com

SHT21 - Wrong Humidity value [SOLVED]
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 12951

View user's profile Send private message

PostPosted: Mon Jul 04, 2016 4:02 am     Reply with quote

The humidity will decrease when the chip is heated.

The chip assumes it's temperature, is the air temperature. For a given amount of moisture in the air, the RH is lower the hotter the air is. Heat the chip, and the RH measure will fall.
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Bad Sensor.. Closing thread
PostPosted: Wed Jul 06, 2016 4:35 am     Reply with quote

After a lot of testing, I find that both my sensors are damaged and giving wrong reading.

The updated driver code that was updated here works.

I am closing this thread and marking it as solved.
_________________
Regards,
Devil
Ttelmah



Joined: 11 Mar 2010
Posts: 12951

View user's profile Send private message

PostPosted: Wed Jul 06, 2016 7:23 am     Reply with quote

Crying or Very sad

Sad. I just spent two days debugging why a unit wouldn't do what I expected to find it was a faulty moulding hidden inside a connector system...

Happens I'm afraid.
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Thanks for your time
PostPosted: Sun Jul 17, 2016 12:35 pm     Reply with quote

Thanks Mr T.

I closed the thread because I ordered new sensors. It will take 1 month to get delivered.

Its good not to keep threads open.
_________________
Regards,
Devil
gmyu



Joined: 06 Jan 2009
Posts: 3

View user's profile Send private message

Re: SHT21 - updated driver code.
PostPosted: Thu Jan 11, 2018 8:43 pm     Reply with quote

int16 humidum; should be ...
uint16 humidum;
Else, when humidum is more then 55%, this var will be a negative. Also change this --- int16 temperies;
==========================================

devilfrmheaven wrote:
Hi All,

I am placing the udpated driver file here just incase if someone needs it.


Code:


////////////////////////////////////////////////////////////////////////////////
///                               sht21.c                                    ///
///            Driver for temperature and humidity sensor                    ///
///            Realizado por PicTrance                                       ///
///            junio del 2011, Puebla - México                               ///
///            Compiler: CCS                                                 ///
///         Updated July 04, 2016 --- devilfrmheaven www.ccsinfo.com         ///
///                                                                          ///
/// 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(0);
   //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(0);

   //i2c_read(0);
   i2c_stop();
   
   lsbhum &= 0XFC; /// Thanks to Ttelmah @ www.ccsinfo.com
   humidum = make16(msbhum,lsbhum);     
   /*#ASM
      movf lsbhum,0
      movwf 0x00C
      movf msbhum,0
      movwf 0x00D
   #ENDASM*/
   
   humidum1= (-6 + (0.00190734*humidum)); ///// Thanks to Ttelmah @ www.ccsinfo.com
   SoftResett();
   //return humidum1;
}

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

Ttelmah



Joined: 11 Mar 2010
Posts: 12951

View user's profile Send private message

PostPosted: Fri Jan 12, 2018 3:14 am     Reply with quote

as a comment to the above, the reason 'int16' was used, is that on a PIC16 or 18, with CCS, a int16, by default _is_ unsigned. So the problem won't occur. It will only be a problem if the code is moved to a PIC24/30/33, where the default int16, is signed. It is better to be explicit, but this is 'why' this was not seen by some people.... Very Happy
gmyu



Joined: 06 Jan 2009
Posts: 3

View user's profile Send private message

PostPosted: Sun Jan 14, 2018 8:35 pm     Reply with quote

Thx! I just use PIC24 series.

And I have changed the calc code to int from float, it should save CPU resource.
Code:

void sht21_HT(void){ //float &temperies1,float &humidum1){
   uint8 msbX, lsbX;
   uint16 uiX,humidum;
   int16 iValue;
   int32 lValue;  //  float temperiess;
   i2c_start();   i2c_write(sht21);    i2c_write(temphold);
   i2c_start();   i2c_write(read);
   msbX = i2c_read();
   lsbX = i2c_read(0);
   lsbX &= 0XFC;    //checksum = i2c_read(0);
   i2c_stop();
   uiX = make16(msbX,lsbX);//valiable = MAKE16(varhigh, varlow) junta las
   lValue=uiX;  //   lValue+=(372.95*30);  //-25deg=373*25
   lValue=(lValue*17572)/65536-4685;
   iValue = lValue;
   //temperies1= (-46.85 + (175.72*((float)uiX/65536)));

   delay_ms(10);  //T=85mS H=29mS 
  i2c_start();   i2c_write(sht21);  i2c_write(humidhold);
   i2c_start();  i2c_write(read);
   msbX = i2c_read();
   lsbX = i2c_read(0);
   lsbX &= 0XFC;   //checksum = i2c_read(0);
   i2c_stop();
   uiX = make16(msbX,lsbX);//valiable = MAKE16(varhigh, varlow) junta las
//   humidum1= (-6 + (125.0/65536*uiX));
   uiX>>=2;   uiX = uiX/13-60;  //*10 Humidum%
   //temperiess=uiX;

}
gmyu



Joined: 06 Jan 2009
Posts: 3

View user's profile Send private message

PostPosted: Sun Jan 14, 2018 8:39 pm     Reply with quote

Ttelmah wrote:
as a comment to the above, the reason 'int16' was used, is that on a PIC16 or 18, with CCS, a int16, by default _is_ unsigned. So the problem won't occur. It will only be a problem if the code is moved to a PIC24/30/33, where the default int16, is signed. It is better to be explicit, but this is 'why' this was not seen by some people.... Very Happy


Could you give my any advice for i2c setting?
I use the I2C1 force_hw, it have 2 selection in wizard, the default is B8/B9, I want set to A0/A1 but the wizard program no any difference in generate code. What can I do? Thx!
Ttelmah



Joined: 11 Mar 2010
Posts: 12951

View user's profile Send private message

PostPosted: Mon Jan 15, 2018 1:19 am     Reply with quote

You need to tell us your chip.....

The odds are that it is a fuse (most chips offering just two addresses have these selected this way).

There would be no difference in the code, except for the fuse to change the pins used.
temtronic



Joined: 01 Jul 2010
Posts: 6020
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jan 15, 2018 6:12 am     Reply with quote

also

Do NOT rely on the'wizard' ! You can easily cut code without it AND you'll be a better programmer for doing so.
The 'wizard' is just one person's idea of how to setup a PIC,NOT yours or mine. It can easily have a 'bug' in it saying 'this' but coding 'that' and you'll waste a a lot of time trying to figure out what you did wrong when it's the 'wizards' fault. I say the same about 'Proteus' all the time..it's full of errors yet code 'compiles' that will never, ever work in the Real World.
Read the I2C section of the MSSP of the datasheet, 2, 3, 20 times whatever it takes so that you understand the peripheral, it's registers and options. Read a book like 'How I2C works'.... and of course read the CCS manual chapters for I2C functions.
The more you read, the more you'll learn. I understand English is hard and I grew up with it ! PICs by their nature are technical and new ones are very complicated BUT once you understand them it all makes sense.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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