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 CCS Technical Support

i2C on 18f4525 and I2C_Read(0)???

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



Joined: 10 Jan 2008
Posts: 17

View user's profile Send private message

i2C on 18f4525 and I2C_Read(0)???
PostPosted: Tue Sep 04, 2012 7:17 am     Reply with quote

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: 19539

View user's profile Send private message

PostPosted: Tue Sep 04, 2012 7:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 04, 2012 8:33 am     Reply with quote

Very Happy Very Happy Very Happy
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
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