i would like to interface PIC16f887 with SH11 Humidity& Temperature sensor and output the temperature reading on 2 X 16 LCD Display.
I wrote a source code and it displays the value "-37.44999" always.
Can anyone please check the code attached and advise.
Regards
Ananth
Code: Select all
#define SHT_READ 0b11111011;
#define SHT_WRITE 0b11111001;
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
#define ACK 1
#define N0_ACK 0
const float C1= -4.0 ; // constant use for SHT1x Humidity Measurement
const float C2 = 0.0405 ;
const float C3 = -0.0000028 ;
const float d1 = -40.00; // constant use for SHT1x Temperature Measurement
const float d2 = 0.01;
const float t1 = 0.01 ; // constant use for SHT1x True Humidity Measurement
const float t2= 0.00008 ;
float tempt, humi;
float RH_linear, RH_true;
//calculate temperature in Celsius from value
//formula t = d1 + d2*SOt
// tempt = d1C + (tempt * d2C);
//calculate humidity %RH from value
//formula RHlinear = C1 + C2*SOrh + C3*SOrh*SOrh %RH
// RH_linear = C1 + (C2*humi) + (C3*humi*humi);
//formula RHtrue = (Tc-25)*(t1+t2*SOrh)+RHlinear
// RH_true = (tempt-25)*(t1+(t2*humi))+ RH_linear;
//SHT11 connections
sbit SDA at RC4_bit; // Serial data pin
sbit SCL at RC3_bit; // Serial clock pin
sbit SDA_Direction at TRISC4_bit; // Serial data direction pin
sbit SCL_Direction at TRISC3_bit; // Serial clock direction pin
// LCD module 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;
// End LCD module connections
char *text; //
char temp[10];
void temp_read() {
I2C1_Start();
I2C1_Wr(0x03);
delay_ms(350);
tempt = I2C1_Rd(1);
delay_ms(10);
I2C1_Stop();
}
void temp_meas() {
tempt = d1 + (tempt * d2);
FloatToStr(tempt, temp);
}
void temp_disp() {
Lcd_Out(2,1,temp); // Define the first message
Lcd_Out(2, 11, "C");
}
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
I2C1_Init(100000); // initialize I2C communication
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)
text = "Initializing..."; // Define the first message
Lcd_Out(1,1,text);
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
text = "Temp&Humidity"; // Define the first message
Lcd_Out(1,1,text); // Write the first message in the first line
Delay_ms(1000);
text = "Proj by ANANTH"; // Define the second message
Lcd_Out(2,1,text); // Define the first message
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
while (1) {
temp_read();
temp_meas();
temp_disp();
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
}