Revision 1.7 Page 8 of 27
09/12/1999
{int base, slv, setnack;
base = 0x310; /* Adapter’s bas address = 0x310 (hex) */
slv = 0xa0; /* I2C Address of slave, this is a typical */
/* write for an I2C static RAM or EEPROM */
setnack = 0; /* Enable Acknowledge (see parameter */
/* descriptions) */
sendaddress(base, slv, setnack);
printf(“Start and I2C Slave Address sent\n”);
}
/* This will send a start and the I2C Slave Address of the device */
/* to be communicated with. */
5.1.4. I2C Read Data Byte from Slave Routine
Name: readbyte(base, setnack)
Usage: data = readbyte(base, setnack);
Function: To read a byte of data from a slave device whose slave address has already
been sent by sendaddress. Can be used to disable acknowledge after reading
data if it is the last but one byte to be read.
Note: First byte read after read-address is always that address and should be discarded. This byte
should be ignored when working out when to acknowledge/not acknowledge.
Parameters are:
int base The base address of the I2C Communications Adapter set by links on circuit
board.
int setnack This controls whether the I2C Communications Adapter transmits an
Acknowledge down the I2C Bus on reception of a byte. The last byte received
during a transfer must not be acknowledged, in all other cases must be enabled.
If setnack = 0 then acknowledge is enabled, if setnack = 1 then acknowledge is
disabled. Therefore, if the LAST BUT ONE byte is to be read, setnack should be
set to = 1; in all other cases it must be set = 0. (in case of reading 1 byte only,
acknowledge will have been disabled by sendaddress and so should now be
enabled again after reading the data, hence setnack = 0 for reading a single byte
of data.)
Value Returned:
int <data> The function reads the byte of data and returns an integer equal to the value
read.
I2C status on exit from routine:
The status of the I2C Communications Adapter will be either 0x00 - bus busy and
unread data in the data register or 0x80 - bus busy and no unread data in the
data register depending on whether any more bytes of data have been
transmitted by the slave device which is being read. (See Appendix A for details
of the status Codes).
Example Usage: (see also sample programs)
#include <stdio.h>
#include <dos.h> /* Optional, but recommended if your compiler */
/* supports DOS I/O functions */
#include <i2c.h> /* This contains the I2C functions */
main
{int base, setnack, data;
base = 0x310; /* Adapter’s base address = 0x310 (hex) */
setnack = 0; /* Enable acknowledge - only one byte it to */
/* be read (see parameter descriptions) */
data = readbyte(base, setnack);
printf(“Data read was %x\n”, data);
}