This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C procedure questions

Hi,
i am interfacing DS1631 temperature sensor to P89V664. And i am trying out using the in built I2C peripheral.
i have read through both the datasheets and have written the program.
However i am not able to get the reading from the temperature registers of the DS1631. the value read seems to be static and incorrect.

i have listed the code that i developed below. can people who have worked on I2C or DS1631 or P89V664 help me out as to what I am doing wrong?
If necessary I can post the entire program.

Thanks
kannan.k

void main()
{
    unsigned char TempH;
    unsigned char TempL;

    //address is set as 000
    A0 = 0;    //initalize
    A1 = 0;
    A2 = 0;.

    SI = 0;
    STO = 0;
    STA = 0;

   LCD_init();
   lcd_clear();

    //CR0 = 0;
    //CR1 = 0;
    //CR2 = 0;

    ENS1 = 1; //enable I2C-0

//procedure described in ds1631 datasheet.
// 1. send Start
// 2. Send control byte with write bit (addresss + 1)
// 3. send 0x51 (start conversion command)
// 4. send stop
// 5. send Start
// 6. Send control byte with write bit (addresss + 1)
// 7. send 0xAA (read temperature register)
// 8. send Start
// 9. send control byte with write bit (addresss + 0)
//10. Read MSB
//11. send ACK
//12. read LSB
//13. send NACK
//14. send stop
while(1)
    {
       lcd_write_cmd(0x02);

        // 1. send Start
        STA = 1;
        // 2. Send control byte with write bit (addresss + 1)
        S1DAT = 0x91;
        while(!SI);
        SI = 0;
        // 3. send 0x51 (start conversion command)
        S1DAT = 0x51;
        while(!SI);
        SI = 0;
        // 4. send stop
        STO = 1;

        delay();
        // 5. send Start
        STA = 1;
        // 6. Send control byte with write bit (addresss + 1)
        S1DAT = 0x91;
        while(!SI);
        SI = 0;
        // 7. send 0xAA (read temperature register)
        S1DAT = 0xAA;
        while(!SI);
        SI = 0;
        // 8. send Start
        STA = 1;
        // 9. send control byte with write bit (addresss + 0)
        S1DAT = 0x90;
        while(!SI);
        SI = 0;

        LED = 0;     //12 bit conversion takes about 750ms.
        hugedelay(); //wait for conversion to complete ~1 sec.
        hugedelay(); //should I wait????
        LED = 1;
        hugedelay();
        hugedelay();

        while(!SI);      //read when S1DAT is stable
        AA = 1;          //11. send ACK -????
        TempH = S1DAT;   //10. Read MSB
        SI = 0;

        lcd_write_data((TempH/100)+0x30);
        lcd_write_data(((TempH%100)/10)+0x30);
        lcd_write_data(((TempH%100)%10)+0x30);

        while(!SI);      //read when S1DAT is stable
        AA = 0;          //13. send NACK -?????
        TempL = S1DAT;   //12. read LSB
        SI = 0;

        lcd_write_data((TempL/100)+0x30);
        lcd_write_data(((TempL%100)/10)+0x30);
        lcd_write_data(((TempL%100)%10)+0x30);

        STO = 1;      //14. send stop

        hugedelay();   // wait for few ms before we start again.
      }
}

0