Discussion Forum

I2C procedure questions

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
kannan kariraman
Posted
13-Jul-2009 12:30 GMT
Toolset
C51
New! 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.
      }
}

Read-Only
Author
erik malund
Posted
13-Jul-2009 12:52 GMT
Toolset
C51
New! use CodeArchitect

free from http://www.esacademy.com the code for the LPC935 is identical (except possibly for clocking). The code will work, maybe you will want to tweak it to fully fit your purpose.

DO NOT try to do IIC without an ISR, you willl run into all kinds of problems with the rest of your code when the IIC put everything else to a stop.

Erik

Read-Only
Author
kannan kariraman
Posted
17-Jul-2009 11:49 GMT
Toolset
C51
New! did it

Among other tasks.... finally i was able to interface DS1631, with interrupts and with out them to the p89V66X.... just had to read the Datasheet carefully ;)

and yes, as Erik as mentioned, it makes a lot of sense to handle I2C using interrupts.

Read-Only
Author
Andy Neil
Posted
17-Jul-2009 13:46 GMT
Toolset
None
New! just had to read the Datasheet carefully

Yes, that is always a necessity!

Next Thread | Thread List | Previous Thread Start a Thread | Settings