View previous topic :: View next topic |
Author |
Message |
FMOpt2000
Joined: 10 Jan 2008 Posts: 17
|
i2C on 18f4525 and I2C_Read(0)??? |
Posted: Tue Sep 04, 2012 7:17 am |
|
|
The question is, why the zero between brackets?
I'm using 3 PIC 18LF4525 and the I2C bus.
I've no problem for the I2C write operations on the 2 "slaves" chips,
but I've an issue in the read operation.
The slave has the suggested structure (no problem, I'm sure):
Code: |
void i2c_isr() {
BYTE
state;
state = i2c_isr_state();
if(state >= 0x80)
{
i2c_write(rcv_buffer[state - 0x80]);
//
}
else if(state > 0)
{
rcv_buffer[state - 1] = i2c_read();
test++;
if(flag_i2c)
{output_low(pin_d0);
//output_low(LED_CHRG_OUT);
flag_i2c=0;
}
else
{
output_high(pin_d0);
//output_high(LED_CHRG_OUT);
flag_i2c=1;
}
}
} |
In the main if I receive a RS232 command with a specific code I'll do the
reading on the first slave chip:
Code: |
if(serial_command==49)
{
//i2c_start();
//i2c_write(0x50);
i2c_start();
i2c_write(0x51);
count=0;
buffer[count++]= i2c_read(); //Read Next
buffer[count++]= i2c_read(); //Read Next
buffer[count++]= i2c_read(); //Read Next
buffer[count++]= i2c_read(0); //Read Next
i2c_stop();
}
//**************
} |
If I put the last i2c_read without zero, after the last reading
the SDA line, remain LOW and I need I write operation to resume it.
The reading operation is ok because the buffer is filled with right 4 bytes
read by slave.
So the issue is why the SDA goes LOW and reain in this state.
Could you help me to understand?
In the examples anyone use this 0! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Tue Sep 04, 2012 7:30 am |
|
|
Basic I2C. Look at ACK/NACK.
Each 8bit transfer, actually involves nine bits. The last is the acknowledge bit.
The zero is used as a marker to say this is the last transfer, and controls the ACK bit sent after the last data bit is transferred.
Realistically you need to read the Phillips I2C reference manual.
Best Wishes |
|
|
FMOpt2000
Joined: 10 Jan 2008 Posts: 17
|
|
Posted: Tue Sep 04, 2012 8:33 am |
|
|
I didn't rember the change in the ACK and the last byte and I supposed it was more tricky...
Thank you so much!
Bye |
|
|
|