CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

i2c write more then ones

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Zulander



Joined: 04 Dec 2006
Posts: 14

View user's profile Send private message

i2c write more then ones
PostPosted: Mon Apr 30, 2007 8:13 pm     Reply with quote

Hi need to know I if I could send/ receive more then one time and i2c_write to the master and slave. I have tired this, and it is working perfectly for the one time i2c_write however when I write more then ones it send me an 0xff.


my slave code:
Code:


# INT_SSP
void ssp_interupt ()
{
  BYTE incoming, state;
  int newpos=0;
LED_DATARC=1;
  state = i2c_isr_state();
  if (state < 0x80)
  { //Master is sending data
    incoming = i2c_read();
    if (state == 1)
    //First received byte is address{
      address = incoming;
    }

    if (state == 2)
    //Second received byte is data
      buffer[address] = incoming;
           
    switch (buffer[address]){
     
     
   case smgotopos:
        {
      newpos=i2c_read();   //HERE
          break;
        }
     
      default:
        {
        err=buffer[address];
        }

    }

  if (state == 0x80)
  { //Master is requesting data
    switch (buffer[address]){
      case getstatus:
        {
          i2c_write(createstatus1());
      i2c_write(cpos); //note that cpos is a global variable //HERE
          break;
                  }
      default:
        {
          i2c_write(buffer[address]);
        }
    }
  }
LED_DATARC=0;
}


my master functions code:
Code:

void getstatus1(int add)
{
   int data,data1;
   i2c_start();
i2c_write(add);
i2c_write(0x00);
i2c_write(getstatus);
i2c_stop();

i2c_start();
i2c_write(add);
i2c_write(0x00);
i2c_start();
i2c_write(add +1);
data = i2c_read(0); //i should read the createstatus1
data1 = i2c_read(1); // i should read cpos
i2c_stop();
printf("s1:%x|%x\r\n",data,data1);
}

void i2c_write_data(int xdata1,int add){
  i2c_start();
  i2c_write(add);
  i2c_write(0x00);
  i2c_write(smgotopos);
  i2c_write(xdata1); //i should send New pos
  i2c_stop();
  delay_ms(1000);
}


thank s
Ttelmah
Guest







PostPosted: Tue May 01, 2007 5:05 am     Reply with quote

The last I2C read at _both_ ends, needs to send a NACK. You are only doing this at the 'master' end.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue May 01, 2007 8:19 am     Reply with quote

Quote:
data = i2c_read(0); //i should read the createstatus1
data1 = i2c_read(1); // i should read cpos


You are sending a NACK on this first read and then an ACK on the second read. i2c_read() is the same as i2c_read(1) which sends an ACK. i2c_read(0) will send a NACK.

When the Slave receives a NACK it assumes that the Master is finished reading data and stops sending it. You are telling the Slave that you are finished after the first read. Switch your commands to:

Code:
data = i2c_read(1); //i should read the createstatus1
data1 = i2c_read(0); // i should read cpos


and see if that helps.

Ronald
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group