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 and ACK

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



Joined: 19 Oct 2004
Posts: 40

View user's profile Send private message

i2c and ACK
PostPosted: Thu Aug 12, 2010 3:03 am     Reply with quote

Hi
I would like to communicate a PIC with a chip using a I2C communication. I checked the CCS commands. It seems it is easy. But I don't find the way to check the ACK answer from the slave. Maybe using an interruption? Does anyone know the way to read the ACK answer using a I2C communication?
Thanks for your answer Surprised
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 7:58 am     Reply with quote

I2C_WRITE, _returns_ the ACK bit.....

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 8:21 am     Reply with quote

You can check the ACK bit in the following way and if the device does ACK back then you can proceed with the rest of your code. Something like this:
Code:
i2c_start();
if(!i2c_write(0xA0))// send device address, if it responds let's write some data
(
  i2c_write(data);

}
i2c_stop();// if it doesn't respond we'll just stop talking

This is just a quick short sample but hopefully it shows you how to check the ACK bit so you know the device is responding properly. Then, you can add your code to write/read data to/from the device.

Now, you can put the initial i2c_write in a while() loop to keep sending the device's address until it does respond. This is used to check to see if an eeprom is busy while it's writing data it received into the eeprom's memory. The program can get hung up in this while loop though if you don't place a counter in it as well to escape out, just in case the i2c device never responds.

Also, when reading data from the device, i2c_read(), you need to send a zero in the last read statement, i2c_read(0), which tells the device that you're done retrieving data from it.

Clear as mud?

Ronald
angel



Joined: 19 Oct 2004
Posts: 40

View user's profile Send private message

hi
PostPosted: Thu Aug 12, 2010 10:21 am     Reply with quote

Thanks, the first part is clear...
But, Why is it important to tell the slave device " that you're done retrieving data from it"...?
In the CCS manual i2c_read(0) indicates do not ack. I dont see it means the same...

The manual of my slave device says -> after reading a value: "Not acknowledge bit sent by host controller to device". So no ACK will be sent to the master device. Do I need the i2c_read(0)?

If I read 1 data from the slave device, is it necessary the i2c_read(0)?
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 11:38 am     Reply with quote

Quoted from the Philips i2c specification manual:
Quote:
If a master-receiver is involved in a transfer, it must signal
the end of data to the slave- transmitter by not generating
an acknowledge on the last byte that was clocked out of
the slave. The slave-transmitter must release the data line
to allow the master to generate a STOP or repeated
START condition.


When you are writing data to a slave device the slave will generate the ACK bit, telling the master that it received the data. When the slave is sending data to the master the master will generate the ACK bit telling the slave that it received the data. The ACK bit tells the slave that it should be ready to send another byte of data to the master.

By default, when the master is reading data from a device the command i2c_read() sends a 1 in the command, i2c_read(1), which tells the master to send an ACK. You should always send a NOACK when you have read the last byte. This NOACK tells the device that you're finished.

Ronald
angel



Joined: 19 Oct 2004
Posts: 40

View user's profile Send private message

PostPosted: Fri Aug 13, 2010 1:18 am     Reply with quote

OK, thanks 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