<<Previous

Next>>

 I2CREAD

I2CREAD DataPin,ClockPin,Control,{Address,}[Var{,Var...}]{,Label}

Send Control and optional Address bytes out the ClockPin and DataPin and store the byte(s) received into Var. ClockPin and DataPin may be a constant, 0-15, or a variable that contains a number 0-15 (e.g. B0) or a pin name (e.g. PORTA.0).

I2CREAD and I2CWRITE can be used to read and write data to a serial EEPROM with a 2-wire I2C interface such as the Microchip 24LC01B and similar devices. This allows data to be stored in external non-volatile memory so that it can be maintained even after the power is turned off. These commands operate in the I2C master mode and may also be used to talk to other devices with an I2C interface like temperature sensors and A/D converters.

For 12-bit core PICmicro MCUs only, the I2C clock and data pins are fixed at compile time by DEFINEs. They still must be specified in the I2CREAD statements, though this information is ignored by the compiler.

	DEFINE I2C_SCL PORTA,1 	'For 12-bit core only
	DEFINE I2C_SDA PORTA,0 	'For 12-bit core only

The upper 7 bits of the Control byte contain the control code along with chip select or additional address information, depending on the particular device. The low order bit is an internal flag indicating whether it is a read or write command and should be kept clear.

This format for the Control byte is different than the format used by the original PicBasic Compiler. Be sure to use this format with PBP I2C operations.

For example, when communicating with a 24LC01B, the control code is %1010 and the chip selects are unused so the Control byte would be %10100000 or $A0. Formats of Control bytes for some of the different parts follows:

Device

Capacity

Control

Address size

24LC01B

128 bytes

%1010xxx0

1 byte

24LC02B

256 bytes

%1010xxx0

1 byte

24LC04B

512 bytes

%1010xxb0

1 byte

24LC08B

1K bytes

%1010xbb0

1 byte

24LC16B

2K bytes

%1010bbb0

1 byte

24LC32B

4K bytes

%1010ddd0

2 bytes

24LC65

8K bytes

%1010ddd0

2 bytes

bbb = block select (high order address) bits

ddd = device select bits

xxx = don=t care

The Address size sent (byte or word) is determined by the size of the variable that is used. If a byte-sized variable is used for the Address, an 8-bit address is sent. If a word-sized variable is used, a 16-bit address is sent. Be sure to use the proper sized variable for the device you wish to communicate with. Constants should not be used for the address as the size can vary dependent on the size of the constant.  Also, expressions should not be used as they can cause an improper Address size to be sent.

If a word-sized Var is specified, 2 bytes are read and stored into the Var high byte first, followed by the low byte. This order is different than the way variables are normally stored, low byte first.

A modifier, STR, may be included before the variable name. This can load an entire array (string) at once. If STR is specified, the following variable must be the name of a word or byte array, followed by a backslash (\) and a count:

a var byte[8]

I2CREAD PORTC.4,PORTC.3,$a0,0,[STR a\8]

If a word-sized array is specified, the 2 bytes that comprise each element are read low byte first. This is the opposite of how simple words are read and is consistent with the way the compiler normally stores word-sized variables.

If the optional Label is included, this label will be jumped to if an acknowledge is not received from the I2C device.

The I2C instructions can be used to access the on-chip serial EEPROM on the 12CExxx and 16CExxx devices. Simply specify the pin names for the appropriate internal lines as part of the I2C command and place the following DEFINE at the top of the program:

DEFINE I2C_INTERNAL 1

For the 12CE67x devices, the data line is GPIO.6 and the clock line is GPIO.7. For the 16CE62x devices, the data line is EEINTF.1 and the clock line is EEINTF.2. See the Microchip data sheets for these devices for more information.

The timing of the I2C instructions is set so than standard speed devices (100KHz) will be accessible at clock speeds up to 8MHz. Fast mode devices (400KHz) may be used up to 20MHz. If it is desired to access a standard speed device at above 8MHz, the following DEFINE should be added to the program:

DEFINE I2C_SLOW 1

Because of memory and stack constraints, this DEFINE for 12-bit core PICmicro MCUs does not do anything. Low-speed (100 kHz) I2C devices may be used up to 4MHz. Above 4MHz, high-speed (400kHz) devices should be used.

Transfer on the I2C bus can be paused by the receiving device by its holding the clock line low (not supported on 12-bit core PICmicro MCUs). To enable this the following DEFINE should be added to the program:

DEFINE I2C_HOLD 1

The I2C clock and data lines should be pulled up to Vcc with a 4.7K resistor per the following schematic as they are both run in a bi-directional open-collector manner.

To make the I2C clock line bipolar instead of open-collector the following DEFINE may be added to the program:

DEFINE I2C_SCLOUT 1

 

addr	var	byte
cont	con	%10100000
	addr = 17		' Set address to 17
	' Read data at address 17 into B2
	I2CREAD PORTA.0,PORTA.1,cont,addr,[B2]

See the Microchip ANon-Volatile Memory Products Data Book@ for more information on these and other devices that may be used with the I2CREAD and I2CWRITE commands.

 I2CWRITE

I2CWRITE DataPin,ClockPin,Control,{Address,}
[Value{,Value...}]{,Label}

I2CWRITE sends Control and optional Address out the I2C ClockPin and DataPin followed by Value. ClockPin and DataPin may be a constant, 0-15, or a variable that contains a number 0-15 (e.g. B0) or a pin name (e.g. PORTA.0).

For 12-bit core PICmicro MCUs only, the I2C clock and data pins are fixed at compile time by DEFINEs. They still must be specified in the I2CREAD statements, though this information is ignored by the compiler.

	DEFINE I2C_SCL PORTA,1 	'For 12-bit core only
	DEFINE I2C_SDA PORTA,0 	'For 12-bit core only

The Address size sent (byte or word) is determined by the size of the variable that is used. If a byte-sized variable is used for the Address, an 8-bit address is sent. If a word-sized variable is used, a 16-bit address is sent. Be sure to use the proper sized variable for the device you wish to communicate with. Constants should not be used for the address as the size can vary dependent on the size of the constant.  Also, expressions should not be used as they can cause an improper Address size to be sent.

When writing to a serial EEPROM it is necessary to wait 10ms (device dependent) for the write to complete before attempting communication with the device again. If a subsequent I2CREAD or I2CWRITE is attempted before the write is complete, the access will be ignored.

While a single I2CWRITE statement may be used to write multiple bytes at once, doing so may violate the above write timing requirement for serial EEPROMs. Some serial EEPROMS let you write multiple bytes into a single page before necessitating the wait. Check the data sheet for the specific device you are using for these details. The multiple byte write feature may also be useful with I2C devices other than serial EEPROMs that don=t have to wait between writes.

If a word-sized Value is specified, 2 bytes are sent, high byte first, followed by the low byte. This order is different than the way variables are normally stored, low byte first.

A modifier, STR, may be included before the variable name. This can be used to write an entire array (string) at once and take advantage of a serial EEPROM=s page mode. The data must fit into a single SEEPROM page. The page size is dependent on the particular SEEPROM device. If STR is specified, the following variable must be the name of a word or byte array, followed by a backslash (\) and a count:

a var byte[8]

I2CWRITE PORTC.4,PORTC.3,$a0,0,[STR a\8]

If a word-sized array is specified, the 2 bytes that comprise each element are written low byte first. This is the opposite of how simple words are written and is consistent with the way the compiler normally stores word-sized variables.

If the optional Label is included, this label will be jumped to if an acknowledge is not received from the I2C device.

The I2C instructions can be used to access the on-chip serial EEPROM on the 12CExxx and 16CExxx devices. Simply specify the pin names for the appropriate internal lines as part of the I2C command and place the following DEFINE at the top of the program:

DEFINE I2C_INTERNAL 1

For the 12CE67x devices, the data line is GPIO.6 and the clock line is GPIO.7. For the 16CE62x devices, the data line is EEINTF.1 and the clock line is EEINTF.2. See the Microchip data sheets for these devices for more information.

The timing of the I2C instructions is set so than standard speed devices (100KHz) will be accessible at clock speeds up to 8MHz. Fast mode devices (400KHz) may be used up to 20MHz. If it is desired to access a standard speed device at above 8MHz, the following DEFINE should be added to the program:

DEFINE I2C_SLOW 1

Because of memory and stack constraints, this DEFINE for 12-bit core PICmicro MCUs does not do anything. Low-speed (100 kHz) I2C devices may be used up to 4MHz. Above 4MHz, high-speed (400kHz) devices should be used.

Transfer on the I2C bus can be paused by the receiving device by its holding the clock line low (not supported on 12-bit core PICmicro MCUs). To enable this the following DEFINE should be added to the program:

DEFINE I2C_HOLD 1

To make the I2C clock line bipolar instead of open-collector the following DEFINE may be added to the program:

DEFINE I2C_SCLOUT 1

See the I2CREAD command above for the rest of the story.

addr	var	byte
cont	con	%10100000

	addr = 17		' Set address to 17
	' Send the byte 6 to address 17
	I2CWRITE PORTA.0,PORTA.1,cont,addr,[6]
	Pause 10		' Wait 10ms for write to complete
	addr = 1		' Set address to 1
	' Send the byte in B2 to address 1
	I2CWRITE PORTA.0,PORTA.1,cont,addr,[B2]
	Pause 10		' Wait 10ms for write to complete
<<Previous

Next>>