[En-Nut-Discussion] i2c ds1388

myrteza kertusha myrkertu at gmail.com
Wed Feb 21 13:24:09 CET 2007


Hi everyone,
this is a piece of code trying to communicate with ds1388. As far as I can
see, seems well-written, as the atmega-two wire serial interface explains.
If there's someone that sees smth wrong , please let me know.
Any help appreciated, thamnx in advance
Best regards
Zac



    TWCR = 0xA4;// start condition


    //define scl clock frequency
    TWBR = 0xFF;// setting scl freq. : the lowest value
    TWSR |= 0x03;


    // activating pull-ups on both pins, SCL and SDA
    DDRD &= (~0x03);
    PORTD |= 0x03;

    unsigned long count = 100000;

    while(!(TWCR & 0x80) && (count-- > 1) && (retCode != 0x08))
    {
        retCode = (TWSR & 0xF8);
    }
    retCode = (TWSR & 0xF8);// ottengo gli ultimi 5 bit
    UartWriteString(SER_PORT_COM1,"\n\n\n start byte sent... retCode = %x,
TWCR = %x, count = %lu \n", retCode, TWCR, count );

// i can get an ack after sending start bit

    TWDR = 0xD0;// write op byte

    TWCR = 0x84;// set bit TWINT, TWEN

    count = 10000000;

    while( !(TWCR & 0x80) && (count-- > 1) && (retCode != 0x18))
    {
        retCode = (TWSR & 0xF8);// se lo start non è stato mandato
    }
    retCode = (TWSR & 0xF8);// ottengo gli ultimi 5 bit
    UartWriteString(SER_PORT_COM1,"\n address+R/W byte sent... retCode = %x,
TWCR = %x, count = %lu\n", retCode, TWCR, count);

 // from now and on no ack is ever sent

    TWDR = 0x88;// byte data to sent
    TWCR = 0x84;// set bit TWINT, TWEN

    count = 100000000;
    while(!(TWCR & 0x80) && (count-- > 1) && (retCode != 0x28))
        retCode = (TWSR & 0xF8);// se lo start non è stato mandato
    retCode = (TWSR & 0xF8);// ottengo gli ultimi 5 bit
    UartWriteString(SER_PORT_COM1,"\n data inviato... retCode = %x, TWCR =
%x, count = %lu \n", retCode, TWCR, count );


   TWCR =  0x94;



More information about the En-Nut-Discussion mailing list