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 full 8-bit i2c slave address (leave bit0 as 0, do not use 7 bit 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.
The slave address is the address that is used by the PICAXE chip for identification. The 7 bit address 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 must be set to 0. 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.
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.
The slave address varies for different i2c devices (see table below). For the popular 24LCxx series serial EEPROMs the address is commonly %1010xxx0.
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 should always be 0, as it is overwritten as appropriate when the slave address is used within the hi2cin and hi2cout commands.
Most datasheets give the slave address in 8 bit format e.g. 1010000x. 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 (which should be set to 0).
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).
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)
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.
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:
hi2csetup master, %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 hi2cout 0,(seconds,mins,hour,day,date,month,year,control) endCopy Code Submit an Example
This program configures the PICAXE as an I2C slave device, waits for data to be sent to it via I2C and presents the last data received to the output pins.
Curing a locked i2c bus at power up.
If you have ever had the problem of i2c commands not working when starting a program (particularly after a hardware reset without a power on/off) the solution is below. The issue arises if an i2c slave is writing at the time of the master reset and happens to be holding SDA low at the instant the master (picaxe) is reset. In this case the normal i2c command for resetting the internal state of the i2c slave (master issuing a start command which includes pulling SDA low) may not work. The solution is as follows: Before initialising the PICAXE i2c hardware set SDA as an input and test for SDA low. If it is low then set the SCL (SCK) pin as an output Using a frequency not more that the i2c bus speed the slowest peripheral can handle, toggle the SCK pin low and then high until SDA goes high (i.e. clock out any remaining bits of the write from the peripheral). This cannot take more than 8 clock cycles - if it does then there is some other issue. Once SDA is high then issue the hi2csetup command and all should work perfectly.
- Code Example:
;Example for 20X2/20M2 - adjust pins as necessary for other parts. Init_SDA: pause 1000 input b.5 if pinb.5=1 then sertxd("SDA is high",13,10) b0=8 do pulsout B.7,150 pause 20 loop while pinb.5=1 sertxd("Done..!",13,10) pause 500 'check again... if pinb.5=0 then sertxd("SDA is now low",13,10) endif else sound c.1,(116,5) sertxd ("SDA is low",13,10) endif RETURNCopy Code Submit an Example
Submit Your Own Code!
You must be logged in to submit code examples. Login now.