Please assist with d code below (I2C writing but not reading)...Urgent !!!!

Status
Not open for further replies.

Yamber

Newbie level 5
Joined
May 6, 2013
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,330
I'm trying to read Temperature and Humidity with SHT11 Sensor. I need someone help to check why am I not getting the correct reading on the LCD, please? I'm using MplabX, proteus : PIC16f2620, LCD Lm032, 10k resistors...Thanks in advance:roll:


Code:
    OpenI2C(MASTER, SLEW_OFF);
    SSPADD = 45;
    IdleI2C();
    StartI2C();

    while (SSPCON2bits.SEN);
    __delay_ms(80);

    while (1) {
        RestartI2C();
        IdleI2C();
        WriteI2C(0x07);
        IdleI2C();
        WriteI2C(0x06);
        IdleI2C();
        WriteI2C(_TEMP);
        IdleI2C();
        temperature = ReadI2C();
        tempt = SHT_Measure(temperature);
        IdleI2C();
        AckI2C();
        __delay_ms(80);

        WriteI2C(0x07);
        IdleI2C();
        WriteI2C(0x06);
        IdleI2C();
        WriteI2C(_HUMI);
        IdleI2C();
        humidity = ReadI2C();
        getcI2C();
        IdleI2C ();
        NotAckI2C();
        humidity = ReadI2C();
     //   StopI2C();
        humi = SHT_Measure(humidity);
        IdleI2C();
     //   NotAckI2C();
        __delay_ms(80);
     //   StopI2C();

        tempt = d1C + (tempt * d2C);
        sprintf(txt, "Temp= %.3g", tempt);
        putrsXLCD(txt);
        putcXLCD(223);
        putcXLCD(c);
        putrsXLCD("    ");

        SetDDRamAddr(0x40);
        //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;
        if (RH_true > 100.0) RH_true = 100.0;
        if (RH_true < 0.1) RH_true = 0.1;

        sprintf(txt, "RH= %.3g", RH_true);
        putrsXLCD(txt);
        putcXLCD('%');
        putrsXLCD("    ");

        WriteCmdXLCD(0x02);
    }
}

unsigned SHT_Measure(char CMD) {
    char i;
    char m = 128;
    unsigned temp = 0;
    DATA_X = 0;
    DATA = 1;
    CLK = 0;
    // Connection reset sequence
    for (i = 0; i < 10; i++) {
        CLK = 1;
        CLK = 0;
    }
    // initial transmistion pluses
    DATA = 1;
    CLK = 1;
    DATA = 0;
    CLK = 0;
    CLK = 1;
    DATA = 1;
    CLK = 0;

    //Transmit command.
    for (i = 0; i < 8; i++) {
        if (!(m & CMD)) DATA = 0;
        else DATA = 1;
        CLK = 1;
        CLK = 0;
        m >>= 1;
    }
    //Check acknowledge pulse. return 0 if no pulse.
    DATA_X = 1;
    CLK = 1;
    if (DATA) return 0;
    CLK = 0;
    while (!DATA);
    // wait until data is ready
    while (DATA);
    //delay_ms(250);
    // get first byte MSB
    for (i = 0; i < 8; i++) {
        temp <<= 1;
        CLK = 1;
        if (DATA) temp |= 1;
        CLK = 0;
    }
    // acknowledge pluse
    DATA_X = 0;
    DATA = 0;
    CLK = 1;
    CLK = 0;
    DATA_X = 1;
    // get second byte  LSB
    for (i = 0; i < 8; i++) {
        temp <<= 1;
        CLK = 1;
        if (DATA) temp |= 1;
        CLK = 0;
    }
    // return value without CRC
    return temp;
}

}
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…