I2CSLAVE slaveaddress, mode, addresslen

SLAVEI2C slaveaddress, mode, addresslen

SlaveAddress - is the i2c slave address.

Mode - is the keyword i2cfast (400kHz) or i2cslow (100kHz) at 4Mhz.

Addresslen - is the keyword i2cbyte or i2cword.


This command is deprecated, please consider using the hi2csetup command instead.

The i2cslave command (slavei2c also accepted by the compiler) is used to configure the PICAXE pins for i2c use (in MASTER mode) and to define the type of i2c device to be addressed.

Use of i2c parts is covered in more detail in the separate 'i2c Tutorial' datasheet.

If you are using a single i2c device you generally only need one i2cslave command within a program. With the PICAXE-18X device you should issue the command at the start of the program to configure the SDA and SCL pins as inputs to conserve power.

After the i2cslave has been issued, readi2c and writei2c can be used to access the i2c device.

Slave Address

The slave address varies for different i2c devices (see table below). For the popular 24LCxx series serial EEPROMs the address is commonly %1010xxxx.

Note that some devices, e.g. 24LC16B, incorporate the block address (ie the memory page) into bits 1-3 of the slave address. Other devices include the external device select pins into these bits. In this case care must be made to ensure the hardware is configured correctly for the slave address used.

Bit 0 of the slave address is always the read/write bit. However the value entered using the i2cslave command is ignored by the PICAXE, as it is overwritten as appropriate when the slave address is used within the readi2c and writei2c commands.


Speed mode of the i2c bus can be selected by using one of the two keywords i2cfast or i2cslow (400kHz or 100kHz). The internal slew rate control of the microcontroller is automatically enabled at the 400kHz speed (28X/40X). Note that the 18X internal architecture means that the slower speed is always used with the 18X, as it is not capable of processing at the faster speed.


Address Length

i2c devices commonly have a single byte (i2cbyte) or double byte (i2cword) address. This must be correctly defined for the type of i2c device being used. If you use the wrong definition erratic behaviour will be experienced. When using the i2cword address length you must also ensure the 'address' used in the readi2c and writei2c commands is a word variable. Note this is the EEPROM address length only, not the data bytes themselves. It is not possible to transmit a word value directly over i2c (e.g. word w0 must be transmitted as the two separate bytes b0 and b1).


Settings for some common parts

Device Type Slave Speed Address
24LC01B EE 128 %1010xxxx i2cfast i2cbyte
24LC02B EE 256 %1010xxxx i2cfast i2cbyte
24LC04B EE 512 %1010xxbx i2cfast i2cbyte
24LC08B EE 1kb %1010xbbx i2cfast i2cbyte
24LC16B EE 2kb %1010bbbx i2cfast i2cbyte
24LC64 EE 8kb %1010dddx i2cfast i2cword
24LC128 EE 16kb %1010dddx i2cfast i2cword
24LC256 EE32kb %1010dddx i2cfast i2cword
24LC512 EE64kb %1010dddx i2cfast i2cword
DS1307 RTC %1101000x i2cslow i2cbyte
MAX6953 5x7 LED %101ddddx i2cfast i2cbyte
AD5245 Digital Pot %010110dx i2cfast i2cbyte
SRF08 Sonar %1110000x i2cfast i2cbyte
AXE033 I2C LCD %1100011x i2cslow i2cbyte
CMPS03 Compass %1100000x i2cfast i2cbyte
SPE030 Speech %1100010x i2cfast i2cbyte

x = don't care (ignored).

b = block select (selects internal memory page within device).

d = device select (selects device via external address pin polarity).


Effect of Increased Clock Speed

Ensure you modify the speed keyword (i2cfast_8, i2cslow_8) at 8MHz or (i2cfast_16, i2cslow_16) at 16MHz for correct operation.

See readi2c or writei2c for example program for DS1307 real time clock.

Applies To:
See Also:
Related Create:

Example of how to use a DS1307 Real Time Clock

This program configures I2C operations to access a DS1307 Real Time Clock and then sets the current time and date of the DS1307. Note the data is sent/received in BCD format. Note that seconds, mins etc are variables that need defining e.g. symbol seconds = b0 etc.

Code Example:
		i2cslave %11010000, i2cslow, i2cbyte				; set PICAXE as master and DS1307 slave address

; write time and date e.g. to 11:59:00 on Thurs 25/12/03

start_clock:	let seconds = $00						; 00 Note all BCD format
		let mins = $59							; 59 Note all BCD format
		let hour = $11							; 11 Note all BCD format
		let day = $03							; 03 Note all BCD format
		let date = $25							; 25 Note all BCD format
		let month = $12							; 12 Note all BCD format
		let year = $03							; 03 Note all BCD format
		let control = %00010000						; Enable output at 1Hz
		writei2c 0,(seconds,mins,hour,day,date,month,year,control)
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.


  • Login to leave a comment.