Electronic – Not getting correct values in I2C interfacing in Atmega


I'm very new to the concept of I2C and I've been having some problems interfacing I2C between 2 Atmega32(s).

  • I have one Atmega32 as a Master to which an LCD screen is connected
    and another I2C as a slave to which a LM35 has been connected and
    both of these Atmega have been connected with the SDA and SCL lines.

  • So, although I am getting data on the LCD screen attached to the
    master, I'm not getting the right values. Like the temperature is 28
    Centigrade here but the LCD that is connected to the master keeps
    repeating 65280 for some reason. Can someone tell me where I'm going
    wrong? Code for both the master and slave devices have been posted

The Master code:

int main(void)
    unsigned int xx=0x00;
    unsigned char yy;
    I2C_init(); //I2C initialization
    lcd_init(); //LCD initialization
        for (int i=0;i<4;i++) //Getting unsigned int data from slave, so
        {                     //broke data into 4 parts and receiving 
            if (i==0)         //one byte at a time. 
                I2C_start(0x01);  //I2C start along with the address of the slave
                yy=I2C_read_ack(); //Read slave data along with an acknowledgment 
                xx |= (yy << 8);    
                I2C_stop(); //I2C stop function.
            else if (i>0)
                I2C_start(0x01+1); //don't know any particular reason
                yy=I2C_read_ack(); //behind this, but if i don't do 0x01+1
                xx |= (yy << 8);   //then the master ends up reading the
                I2C_stop();        //address as a data packet.
     lcd_num(xx);   //lcd function to display unsigned int data.

The Slave code:

The slave code is just repeating one function again and again so I'm not posting the whole code, just the snippets in between.

int main(void)
    xx=adc_read(0); //reading from ADC0 of the Atmega.
    toTransfer = (5.0 * xx * 100.0) / 1024; //calibrating the temperature

    if (byte == 1) //send packet 1
        toSend = toTransfer & mask; //sending the first 8 bits of data.
        toTransfer = toTransfer >> 8;//right shift so that the next function will take the 8-16 bits of data.
        TWI_match_write_slave(); //I2C function to verify address
        TWI_write_slave(toSend); //I2C function to write data on to master
        byte = 2;

    /*Repeating this till byte 4*/

    else if (byte == 4) //send packet 4
        toSend = toTransfer & mask;
        toTransfer = toTransfer >> 8;
        byte = 1;
        //initialization for next turn
        mask = 0xFF;
        toSend = 0;

update: I've tried this with no luck:

unsigned int xx;   //I just wrote it here so that you don't have to refer the code again-
unsigned char yy;  //-not present in the main loop.
I2C_start(0x01);   //yy is receiving data one byte at a time from the slave
yy = I2C_read_ack();
xx = (xx|yy);

Similarly for the else statement in the code posted by me originally.

Whatever value I send, the LCD on the receiving end is printing 255.

Best Answer

IT WORKS!! So, after I noticed that the LCD is printing only 255 and nothing else, I sat and thought about it. Then I realized that 255 is 0xff in hexadecimal so that meant the SCL line wasn't being held long enough for the slave to transmit data. So I simply put a small delay of 100 ms after the read function and it started working. :D

 yy = I2C_read_ack();    
 getdata=yy; //getdata is just another unsigned char variable. I thought that maybe yy 
 yy=0;       //isn't getting refreshed or something, so i made a small reset sort of thing here.
 xx = getdata&zz;