Revision 1.7 Page 10 of 27
09/12/1999
#include <dos.h> /* Optional, but recommended if your compiler */
/* supports DOS I/0 functions */
#include <i2c.h> /* This Contains the I2C routines */
main
{int base, controladdress;
base = 0x310; /* Adapter’s base address = 0x310 (hex) */
controladdress = base + 1; /* Control Register address */
sendstop(controladdress);
printf(“Stop has been transmitted\n”);
}/* This will write a Stop to the I2C Bus */
5.1.7. I2C Restart and Address Sending Routine
Name: restart(base, slv, setnack)
Usage: restart(base, slv, setnack)
Function: Procedure to send a Restart and the slave address of device that is to be
communicated with, without a Stop having been sent at the end of last
transmission/reception. This routine is particularly useful for reading memories
which must first have a data pointer written to them to select where the read is to
occur from, and must have their address sent as a Restart to proceed with the
read operation.
Parameters are:
int base the base address of the I2C Communications Adapter set by links on the circuit
board.
int slv the slave address of the device which is to be communicated with. This will be an
even number if the adapter is to write to the slave, add 1 to get an odd number if
the adapter is to read from the slave.
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 acknowledge must be enabled, if
setnack = 1 then acknowledge is disabled. Therefore, if a read (odd numbered)
address is being sent AND only 1 byte is to be read, setnack should be set to =
1; in all other cases it must be set = 0.
Value Returned:none.
I2C status on exit from routine:
The status of the I2C/ the Communications Adapter will be either 0x00 - bus the
busy and unread data in the data register or 0x80 - bus busy and no
unread data in the data register when a Restart and Slave Address have been
successfully transmitted (see Appendix A for details of 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 <12c.h> /* This contains the I2C functions */
main
{int base, slv, setnack;
base = 0x310; /* Adapter’s base address = 0x310 (hex) */
slv = 0xa1; /* I2C Address of slave, this is a typical read */
/* address for an I2C static RAM or EEPROM */
setnack = 0; /* Enable Acknowledge (see parameter descriptions)*/
restart(base, slv, setnack);
}
5.2. TurboBASIC Routines
5.2.1. I2C Setup/Initialisation Routines