Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SHT31D calculation problem

Status
Not open for further replies.

bongabobo1

Newbie level 4
Newbie level 4
Joined
Mar 6, 2020
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
91
Good day this is my code for sht31d but mm failing to do the right calculation in order to display the humidity and temperature value please help
Code:
#include <stdio.h>
#include <stdlib.h>

//helpful types
#define bool unsigned char
#define false 0
#define true  1
#define byte unsigned char

//I2C definitons
#define I2C_WRITE         0
#define I2C_READ          1

// Assuming default I2C address (ADDR pin 2 connected to VSS)
#define SHT3X    (0x44 << 1)

// Assume clock stretching disabled (not sure what this I2C library can manage!)
#define SHT3X_CMD_MSB_START_MEAS   0x24

// Assume medium repeatability (measurement time, current consumption, etc.)
#define SHT3X_CMD_LSB_START_MEAS   0x0B

// Assuming CPU clock speed 8MHz, 1 CPU clock cycle per loop (will be more!),
// this will give a delay > 20ms. Note: worst case conversion time is 15ms.
#define SHT3X_MEAS_DELAY_LOOP_VAL  160000

  // Lcd connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
Lcd_Init();



// Start temperature and humidity measurement
bool Sht3xStartMeasurement()
{
  bool error = false;
  I2C1_Start();
  I2C1_Wr(SHT3X + I2C_WRITE);
  I2C1_Wr(SHT3X_CMD_MSB_START_MEAS);
  I2C1_Wr(SHT3X_CMD_LSB_START_MEAS);
  I2C1_Stop();                                            // issue I2C stop signal

  return error;
}



bool Sht3xReadMeasurementResult(unsigned short* pTemperature, unsigned short* pHumidity)
{
  bool error   = false;
  byte tempMsb = 0;
  byte tempLsb = 0;
  byte tempCrc = 0;
  byte humMsb  = 0;
  byte humLsb  = 0;
  byte humCrc  = 0;

  I2C1_Start();                                         // issue I2C start signal
  I2C1_Wr(SHT3X + I2C_READ);                             // send byte via I2C  (device address + R)
  I2C1_Repeated_Start();                                  // issue I2C repeated start signal

  tempMsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  tempLsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  tempCrc = I2C1_Rd(1);                                   // Read the data (acknowledge)
  humMsb  = I2C1_Rd(1);                                   // Read the data (acknowledge)
  humLsb  = I2C1_Rd(1);                                   // Read the data (acknowledge)
  humCrc  = I2C1_Rd(0);                                   // Read the data (NO acknowledge)

  I2C1_Stop();                                            // issue I2C stop signal


  *pTemperature = (tempMsb << 8) + tempLsb;
  *pHumidity    = (humMsb  << 8) + humLsb;

  return error;
}



bool Sht3xStatusRegRead_temperature(unsigned short* pStatus)
{
  bool error  = false;
  byte regMsb = 0;
  byte regLsb = 0;
  byte crc    = 0;

  I2C1_Start();                                // issue I2C start signal
  I2C1_Wr(SHT3X + I2C_WRITE);         // send byte via I2C  (device address + W)
  I2C1_Wr(0xF3);                              // send byte (command msb)
  I2C1_Wr(0x2D);                               // send byte (command lsb)

  I2C1_Repeated_Start();                                  // issue I2C repeated start signal
  I2C1_Wr(SHT3X + I2C_READ);         // send byte via I2C  (device address + R)

  regMsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  regLsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  crc    = I2C1_Rd(0);                                   // Read the data (NO acknowledge)

  I2C1_Stop();

  *pStatus = (regMsb << 8) + regLsb;

  return error;
}



bool Sht3xStatusRegClear()
{
  bool error = false;

  I2C1_Start();                                // issue I2C start signal
  I2C1_Wr(SHT3X + I2C_WRITE);        // send byte via I2C  (device address + W)
  I2C1_Wr(0x30);                             // send byte (command msb)
  I2C1_Wr(0x41);                               // send byte (command lsb)

  I2C1_Stop();

  return error;
}

void check_device(unsigned short dev_address){
 I2C1_Start();
 if (I2C1_Wr(dev_address)){
  Lcd_Out(1,1,"Device not found");
 }
 else Lcd_Out(1,1,"SHT31D");
  I2C1_Stop();
}



unsigned long i = 0;
    unsigned short temperature;
    unsigned short humidity;
    char temp[20];
    char hum[20];
    unsigned short value;
    char snum[20];
    
void main()
{
OSCCON=0x00;
TRISC3_BIT=1;   //CLK
TRISC4_BIT=1;  //SDA
ADCON1 = 15;           //All PINS TO digital
SSPCON1=0x28;       // SDA & SCL port serial &clock = fosc/(4*(sspadd+1))
SSPCON2=0x00;
SSPSTAT=0x00;
SSPADD=0x27;        //baud rate 39 in decimal

  Lcd_Init();          // Initialize LCD
  delay_ms(50);
  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off

    while(1){
    check_device(SHT3X + I2C_WRITE);
    Sht3xStatusRegClear();
    Sht3xStartMeasurement();
    for (i = 0; i < SHT3X_MEAS_DELAY_LOOP_VAL; i++); // delay
    Sht3xReadMeasurementResult(&temperature, &humidity);
    Sht3xStatusRegRead_temperature(&value);
    
    if (value >= 125) {
    temp[0] = '-';
    value = ~value +1;
   }
   else temp[0] = ' ';

   temp[1] = value/1000 + 48;
   temp[2] = (value/100)%10 + 48;
   temp[3] = value%10 + 48;
   //temperature[4] = num%10
   temp[5] = 223;

   // eliminate 0s at beginning
   if (temp[1] == '0') {
      temp[1] = ' ';
      if (temp[2] == '0') temp[2] = ' ';
   }

   Lcd_Out(2,4,temp);
   Delay_ms(250);


    
    }

}
</stdlib.h></stdio.h>
 
Last edited by a moderator:

Please add code tags to program listings so the original format is retained. I have done it for you in the listing above.

What is actually wrong, do you get anything displayed at all and if so, what is it.

Brian.
 

Code:
*pTemperature = (tempMsb << 8) + tempLsb;
According to C language rules, the result of (tempMsb << 8) has the same type as tempMsb. Need a type cast:
Code:
*pTemperature = ((unsigned short)tempMsb << 8) + tempLsb;
 

Please add code tags to program listings so the original format is retained. I have done it for you in the listing above.

What is actually wrong, do you get anything displayed at all and if so, what is it.

Brian.

It does not display anything on the lcd screen which I don't know why since I did the calculation for the temperature ,I don't know if its the right way or I am wrong .
 

Do I have to change from pTemperature = (tempMsb << 8) + tempLsb; to pTemperature = ((unsigned short)tempMsb << 8) + tempLsb; on my code??
 

Thanks for your reply, the code does not display anything on the Lcd screen which I don't know my calculation is wrong or what ..i need help on the calculation part so I can display it on the screen since I am new to C language I tried my best to reach this level as for I2C sensor but now I am stuck in a corner which I need help to get out
 

Good day all
This is the calculation I tried so far from the datasheet but it gives me only one value and it doesnot change even if I put heat on the sensor, I am almost done please help.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <stdlib.h>
 
//helpful types
#define bool unsigned char
#define false 0
#define true  1
#define byte unsigned char
 
//I2C definitons
#define I2C_WRITE         0
#define I2C_READ          1
 
// Assuming default I2C address (ADDR pin 2 connected to VSS)
#define SHT3X    (0x44 << 1)
 
// Assume clock stretching disabled (not sure what this I2C library can manage!)
#define SHT3X_CMD_MSB_START_MEAS   0x24
 
// Assume medium repeatability (measurement time, current consumption, etc.)
#define SHT3X_CMD_LSB_START_MEAS   0x0B
 
// Assuming CPU clock speed 8MHz, 1 CPU clock cycle per loop (will be more!),
// this will give a delay > 20ms. Note: worst case conversion time is 15ms.
#define SHT3X_MEAS_DELAY_LOOP_VAL  160000
 
  // Lcd connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
Lcd_Init();
 
 
 
// Start temperature and humidity measurement
bool Sht3xStartMeasurement()
{
  bool error = false;
  I2C1_Start();
  I2C1_Wr(SHT3X + I2C_WRITE);
  I2C1_Wr(SHT3X_CMD_MSB_START_MEAS);
  I2C1_Wr(SHT3X_CMD_LSB_START_MEAS);
  I2C1_Stop();                                            // issue I2C stop signal
 
  return error;
}
 
 
 
bool Sht3xReadMeasurementResult(unsigned short* pTemperature, unsigned short* pHumidity)
{
  bool error   = false;
  byte tempMsb = 0;
  byte tempLsb = 0;
  byte tempCrc = 0;
  byte humMsb  = 0;
  byte humLsb  = 0;
  byte humCrc  = 0;
 
  I2C1_Start();                                         // issue I2C start signal
  I2C1_Wr(SHT3X + I2C_READ);                             // send byte via I2C  (device address + R)
  I2C1_Repeated_Start();                                  // issue I2C repeated start signal
 
  tempMsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  tempLsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  tempCrc = I2C1_Rd(1);                                   // Read the data (acknowledge)
  humMsb  = I2C1_Rd(1);                                   // Read the data (acknowledge)
  humLsb  = I2C1_Rd(1);                                   // Read the data (acknowledge)
  humCrc  = I2C1_Rd(0);                                   // Read the data (NO acknowledge)
 
  I2C1_Stop();                                            // issue I2C stop signal
 
 
  *pTemperature = (tempMsb << 8) + tempLsb;
  *pHumidity    = (humMsb  << 8) + humLsb;
 
  return error;
}
 
 
 
bool Sht3xStatusRegRead_temperature(unsigned short* pStatus)
{
  bool error  = false;
  byte regMsb = 0;
  byte regLsb = 0;
  byte crc    = 0;
 
  I2C1_Start();                                // issue I2C start signal
  I2C1_Wr(SHT3X + I2C_WRITE);         // send byte via I2C  (device address + W)
  I2C1_Wr(0xF3);                              // send byte (command msb)
  I2C1_Wr(0x2D);                               // send byte (command lsb)
 
  I2C1_Repeated_Start();                                  // issue I2C repeated start signal
  I2C1_Wr(SHT3X + I2C_READ);         // send byte via I2C  (device address + R)
 
  regMsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  regLsb = I2C1_Rd(1);                                   // Read the data (acknowledge)
  crc    = I2C1_Rd(0);                                   // Read the data (NO acknowledge)
 
  I2C1_Stop();
 
  *pStatus = (regMsb << 8) + regLsb;
 
  return error;
}
 
 
 
bool Sht3xStatusRegClear()
{
  bool error = false;
 
  I2C1_Start();                                // issue I2C start signal
  I2C1_Wr(SHT3X + I2C_WRITE);        // send byte via I2C  (device address + W)
  I2C1_Wr(0x30);                             // send byte (command msb)
  I2C1_Wr(0x41);                               // send byte (command lsb)
 
  I2C1_Stop();
 
  return error;
}
 
void check_device(unsigned short dev_address){
 I2C1_Start();
 if (I2C1_Wr(dev_address)){
  Lcd_Out(1,1,"Device not found");
 }
 else Lcd_Out(1,1,"SHT31D");
  I2C1_Stop();
}
 
    unsigned long i = 0;
    unsigned short temperature;
    unsigned short humidity;
    char temp[8];
    char hum[8];
    unsigned short value;
    //char snum[6];
 
void main()
{
OSCCON=0x00;
TRISC3_BIT=1;   //CLK
TRISC4_BIT=1;  //SDA
ADCON1 = 15;           //All PINS TO digital
SSPCON1=0x28;       // SDA & SCL port serial &clock = fosc/(4*(sspadd+1))
SSPCON2=0x00;
SSPSTAT=0x00;
SSPADD=0x27;        //baud rate 39 in decimal
 
        // Cursor off
     Lcd_Init();          // Initialize LCD
  delay_ms(50);
  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);
    ShortToStr(temperature, temp);
    ShortToStr(humidity, hum);
    Lcd_Out(1,2, temp);
    Lcd_Out(2,2, hum);
    Delay_ms(200);
  
 
    for(;;){
    //check_device(SHT3X + I2C_WRITE);
    Sht3xStatusRegClear();
    Sht3xStartMeasurement();
    for (i = 0; i < SHT3X_MEAS_DELAY_LOOP_VAL; i++); // delay
    Sht3xReadMeasurementResult(&temperature, &humidity);
    Sht3xStatusRegRead_temperature(&value);
 
    temperature = (-45 + (175*256))/65535;
    humidity = (100*(256))/65535;
 
    }
}

 

You need to learn about debugging techniques, check the data that is actually read from the sensor, trace the calculation step by step.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top