Shop OBEX P1 Docs P2 Docs Learn Events
Using Sensirion SHT-11 — Parallax Forums

Using Sensirion SHT-11

RsadeikaRsadeika Posts: 3,837
edited 2012-10-08 00:18 in Propeller 1
I am trying to create a program/driver to read and display the data from the SHT-11 chip. I ran into a problem, I am not sure how to do the conversion of the raw data to a readable Celsius value. The program below compiles, and I get some values, but it is not showing the correct Celsius value. Has anybody done this already, and can show me the way?

Thanks

Ray

/**
 * @file Sensi_sht11.c
 * This is the main Sensi_sht11 program start point.
 */
#include <propeller.h>


#define Dpin 0
#define Cpin 1


int Sendcommand(Cmd)
{
  int32_t result = 0;
  DIRA &= ~(1<<Dpin);
  OUTA &= ~(1<<Cpin);
  OUTA = ((OUTA & (~(1 << Cpin))) | (1 << Cpin));
  OUTA &= ~(1<<Dpin);
  DIRA = ((DIRA & (~(1 << Dpin))) | (1 << Dpin));
  OUTA &= ~(1<<Cpin);
  OUTA = ((OUTA & (~(1 << Cpin))) | (1 << Cpin));
  DIRA &= ~(1<<Dpin);
  OUTA &= ~(1<<Cpin);
  return Writebyte(Cmd);
}

int Writebyte(Value)
{
  int result = 0;
  DIRA = ((DIRA & (~(1 << Dpin))) | (1 << Dpin));
  {
    int _idx__0044;
    _idx__0044 = 8;
    do {
      OUTA = ((OUTA & (~(1 << Dpin)) | ((Value >> 7) & 0x1) << Dpin));
      Value = (Value << 1);
      OUTA ^= (1<<Cpin);
      OUTA ^= (1<<Cpin);
      _idx__0044 = (_idx__0044 + -1);
    } while (_idx__0044 >= 1);
  }
  DIRA &= ~(1<<Dpin);
  result = ((INA >> Dpin) & 0x1);
  OUTA ^= (1<<Cpin);
  OUTA ^= (1<<Cpin);
  return result;
}

int Readtemperature(void)
{
  int	Ack;
  int result = 0;
  Ack = Sendcommand(3);
//  SensirionSpin_Wait();
  return Readword();
}

int Readword(void)
{
  int result = 0;
  return ((Readbyte(0) << 8) + Readbyte(1));
}

int Readbyte( Ack)
{
  int result = 0;
  DIRA &= ~(1<<Dpin);
  {
    int _idx__0043;
    _idx__0043 = 8;
    do {
      result = ((result << 1) | ((INA >> Dpin) & 0x1));
      OUTA ^= (1<<Cpin);
      OUTA ^= (1<<Cpin);
      _idx__0043 = (_idx__0043 + -1);
    } while (_idx__0043 >= 1);
  }
  DIRA = ((DIRA & (~(1 << Dpin))) | (1 << Dpin));
  OUTA = ((OUTA & (~(1 << Dpin))) | ((Ack & 0x1) << Dpin));
  OUTA ^= (1<<Cpin);
  OUTA ^= (1<<Cpin);
  DIRA &= ~(1<<Dpin);
  return result;
}

int celsius(t)
{
    int ctemp = -39.66 + (0.01 * t);
    return ctemp;
}

/**
 * Main program function.
 */
int main(void)
{
    float rawTemp;
    float tempC;

    waitcnt(CLKFREQ + CNT);
       
    rawTemp = Readtemperature();
    tempC = celsius(rawTemp);    
    printf("%f", rawTemp);
    printf("  %f", tempC);
    
    return 0;
}

Comments

  • ersmithersmith Posts: 6,054
    edited 2012-10-06 19:39
    You should declare the types of all parameters. It looks to me like the program is mixing up floats and ints, which can go very wrong. Unlike Spin, C actually distinguishes between these types and automatically converts between them, which can change bit pattern representations.
  • WBA ConsultingWBA Consulting Posts: 2,934
    edited 2012-10-06 22:55
    Don't know if it will help, but I have links to Arduino C code examples for the Sensirion SHT11 on my module's website: http://www.aestheticacoustics.com/SHT11_05.htm
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-10-07 03:11
    I looked at the Arduino examples, my impression is, for me, it is not helping.

    I made a Spin version using the Sensirion, and XBee modules, which runs as expected. I also did a spin2cpp, of the Spin versions, both C and C++ versions, the C version would not compile, using SimpleIDE, was complaining about "_main". The C++ version compiled, but was producing the wrong values for the readings.

    So, I decided to use the individual functions that got produced in the spin2cpp conversion which is what is shown in the OP. At this point I am not sure what the next step should be.

    After looking at the Sendcommand() and Writebyte() functions, I do not know how to verify that they are working correctly, I am at a loss on this one. I think some of the C experts might have to come up with a driver for the Sensirion module, but I guess everybody is very busy doing more important things.

    Ray
  • rosco_pcrosco_pc Posts: 464
    edited 2012-10-07 19:35
    There is a DHT21 object in the OBEX (http://obex.parallax.com/objects/887/) with C code included in the documentation. From what I can tell the 2 sensors (DHT21 and SHT11) are quite similar, so it might give you some clues?
  • max72max72 Posts: 1,155
    edited 2012-10-08 00:18
    Did you try with the working spin object and spin2cpp?
    Massimo
Sign In or Register to comment.