View previous topic :: View next topic |
Author |
Message |
angel
Joined: 19 Oct 2004 Posts: 40
|
i2c and ACK |
Posted: Thu Aug 12, 2010 3:03 am |
|
|
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 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Thu Aug 12, 2010 7:58 am |
|
|
I2C_WRITE, _returns_ the ACK bit.....
Best Wishes |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Aug 12, 2010 8:21 am |
|
|
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
|
hi |
Posted: Thu Aug 12, 2010 10:21 am |
|
|
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
|
|
Posted: Thu Aug 12, 2010 11:38 am |
|
|
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
|
|
Posted: Fri Aug 13, 2010 1:18 am |
|
|
OK, thanks Ronald |
|
|
|