HI2CSETUP OFF
HI2CSETUP I2CSLAVE, slaveaddress
HI2CSETUP I2CMASTER, slaveaddress, mode, addresslen
Master mode is when the PICAXE controls the i2c bus. It controls other 'slave' devices like memory EEPROMS and can 'talk' to any device on the i2c bus. Slave mode is when the PICAXE is controlled by a different master device (e.g. another microcontroller). It cannot talk to other devices on the i2c bus.
SlaveAddress is the i2c slave address
Mode is the keyword i2cfast (400kHz) or i2cslow (100kHz). Note that these keywords must change to i2cfast_8, i2cslow_8 at 8MHz, etc.
Addresslen - is the keyword i2cbyte or i2cword. Note that this is the 'addressing method' used by the i2c device (i.e. some EEPROMs use a byte address, some use a word address). It is NOT the length of data returned by the hi2cin command, which is always a byte.
The hi2csetup command is used to configure the PICAXE pins for i2c use and to define the type of i2c device to be addressed.
hi2csetup - slave mode (X1 and X2 parts only)
In slave mode the PICAXE becomes an i2c slave device which other i2c masters can communicate with.
Slave Address
The slave address is the address that is used by the PICAXE chip for identification. It can be a number between 1 and 127, but must be held in bits 7 to 1 of the address (not bits 6 - 0) e.g. %1010000x. Bit0 is the read/write bit and so ignored. If you are not sure which address to use we recommend the 'standard i2c EEPROM' address which is %10100000. Some special i2c addresses (0, %1111xxx, %0000xxxx) have special meanings under the i2c protocol and so are not recommended as they may cause unexpected behaviour on third party devices. Address 0 should not be used.
Description
When in slave mode all i2c functions of the slave PICAXE chip are completely automatic. An i2c master can read or write to the slave PICAXE chip as if it was a 128 (X1, 20X2) or 256 (X2) byte 24LCxx series EEPROM, with the scratchpad area acting as the memory transfer area. The master can read the slave PICAXE chip at any time. This does not have any noticeable effect on the slave PICAXE program, however commands that disable internal hardware interrupts (e.g. serout etc) may affect operation. See appendix 2 in section 2 of the manual for more detail on possible conflicts.
However when the master writes to the slave PICAXE memory the 'hi2cflag' is set and the last address written to is saved in the 'hi2clast' variable. Therefore by polling the 'hi2cflag' bit (or using setintflags to cause an interrupt) the PICAXE program can take action when a write has occurred. The 'hi2cflag' must be cleared by the user program after use.
hi2csetup - master mode
In master mode the PICAXE communicates with i2c slave devices; i2c EEPROM, PICAXEs operating as i2c slave devices and other i2c devices. The i2c slave to be accessed is specified by using its i2c slave address.
When only one slave i2c device is connected to your PICAXE you generally only need one hi2csetup command within a program. After the hi2csetup has been issued, hi2cin and hi2cout can be used to access the slave i2c device. When using multiple devices you can change the default slave address within the hi2cin or hi2cout command using the newslave keyword.
Due to limitations with the PIC silicon slave addresses of values 0, 1, 2 are not supported.
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.
Most datasheets give the slave address in 8 bit format e.g. 1010000x - where x is don't care (the read/write bit, PICAXE controlled). However some datasheets use a 7 bit format. In this case the bits must be shifted left to take account for the read/write bit.
Speed
Speed of the i2c bus can be selected by using one of the keywords i2cfast or i2cslow (400kHz or 100kHz). The internal slew rate control of the microcontroller is automatically enabled when required. Always use the SLOWEST speed of the devices on a bus - do not use i2cfast if any part is a 100KHz part (e.g. DS1307).
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 hi2cin and hi2cout commands is a word variable.
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)
hi2csetup off
This will turn off i2c and return the i2c related pins to normal general purpose input and output use.
If a hi2csetup command has been issued it is likely that i2c devices are connected to the PICAXE and it would be extremely rare that a 'hi2csetup off' would be issued.
Effect of Increased Clock Speed:
Ensure you modify the mode keyword (i2cfast_8, i2cslow_8) at 8MHz or (i2cfast_16, i2cslow_16) at 16MHz for correct operation.