View previous topic :: View next topic |
Author |
Message |
Zulander
Joined: 04 Dec 2006 Posts: 14
|
i2c write more then ones |
Posted: Mon Apr 30, 2007 8:13 pm |
|
|
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
|
|
Posted: Tue May 01, 2007 5:05 am |
|
|
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
|
|
Posted: Tue May 01, 2007 8:19 am |
|
|
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 |
|
|
|