bongabobo1
Newbie level 4
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
</stdlib.h></stdio.h>
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);
}
}
Last edited by a moderator: