c - Not getting correct values in I2C interfacing in Atmega -


i'm new concept of i2c , i've been having problems interfacing i2c between 2 atmega32(s).

i have 1 atmega32 master lcd screen connected , i2c slave lm35 has been connected , both of these atmega have been connected sda , scl lines.

so, although getting data on lcd screen attached master, i'm not getting right values. temperature 28 centigrade here lcd connected master keeps repeating 65280 reason. can tell me i'm going wrong? code both master , slave devices have been posted below.

the master code:

int main(void) {     unsigned int xx=0x00;     unsigned char yy;     ddra=0xff;     i2c_init(); //i2c initialization     lcd_init(); //lcd initialization     while(1)     {         (int i=0;i<3;i++) //getting unsigned int data slave,         {                     //broke data 4 parts , receiving              if (i==0)         //one byte @ time.              {                 i2c_start(0x01);  //i2c start along address of slave                 yy=i2c_read_ack(); //read slave data along acknowledgment                  xx |= (yy << 8);                     i2c_stop(); //i2c stop function.             }             else if (i>0)             {                 i2c_start(0x01+1); //don't know particular reason                 yy=i2c_read_ack(); //behind this, if don't 0x01+1                 xx |= (yy << 8);   //then master ends reading                 i2c_stop();        //address data packet.             }         }      lcd_num(xx);   //lcd function display unsigned int data.     } } 

the slave code:

the slave code repeating 1 function again , again i'm not posting whole code, snippets in between.

int main(void) { unsigned int byte = 1; unsigned int totransfer; unsigned int mask = 0xff; unsigned int xx; unsigned char tosend = 0; twi_init_slave(0x01); //initializing slave i2c address adc_init(); //initialize adc while(1) {     xx=adc_read(0); //reading adc0 of atmega.     totransfer = (5.0 * xx * 100.0) / 1024; //calibrating temperature      if (byte == 1) //send packet 1     {         tosend = totransfer & mask; //sending first 8 bits of data.         totransfer = totransfer >> 8;//right shift next function take 8-16 bits of data.         twi_match_write_slave(); //i2c function verify address         twi_write_slave(tosend); //i2c function write data on master         byte = 2;     }      /*repeating till byte 4*/      else if (byte == 4) //send packet 4     {         tosend = totransfer & mask;         totransfer = totransfer >> 8;         twi_match_write_slave();         twi_write_slave(tosend);         byte = 1;         //initialization next turn         mask = 0xff;         tosend = 0;     } } 

in master code, xx never set, except when declared, modified. suggest initialise xx within 1 of loops, not sure which, because lcdnum() within for loop.

edit: there several things wrong receiver loop.

  • no initialisation of accumulator
  • wrong loop control (3 instead of 4)
  • incorrect shifting
  • incorrect byte order (l.s. byte sent first)

i suggest rewriting code block as

for (int i=0; i<4; i++) {     if (i==0)                   // if first byte     {         i2c_start(0x01);         xx = i2c_read_ack();    // initialise accumulator first byte         i2c_stop();     }     else                        // no need test again     {         i2c_start(0x01+1);         yy = i2c_read_ack();         xx |= (yy << (i*8));    // update accumulator         i2c_stop();     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -