'PICAXE BASIC 'Master sample code for I2C-IT sensor 'Created on 2/6/2008 'By Dan Gates / Dave Hrynkiw 'Solarbotics / HVW Technologies Ltd. 'http://www.hvwtech.com 'The I2C can report back the distance measured as a raw analog 0-255 number, metric (cm) or in whole inches. 'This is set by the parameter in the "hi2cin" command, where 1=inches 2=cm, 3=raw, i.e.: ' hi2cin 1,(bo) (reports back inches) ' hi2cin 2,(bo) (reports back centimeters) ' hi2cin 3,(bo) (reports back raw) 'This code assumes a base address of 0x20. Since the I2C protocol uses a 7-bit address, plus a operation bit on 'the least significant bit (LSB, or right-most position), you have to be careful on how to "call" the device. 'This is often a source of confusion for somebody new to I2C, as a 7-bit address shifted one place to add the 'read/write bit at the end results in a different hex number than expected. It's generally easier to think of 'I2C addresses as _8_ bit, with even addresses being "write", and odd addresses as the "read" address for the 'same device. But that doesn't apply in this specific case, as the PICAxe software automatically manages the 'LSB for you, so we recommend you set the address in binary. ' 'If you want to use hexadecimal, add 20 to the source address. 0x20 become 0x40; '0x22 becomes 0x42, etc. ' 'Here are the binary equivalents of the hex address to use with the PICAxe, which leaves room for it to add 'the necessary read/write bit: ' 'I2C-IT | PICAXE ' 0x20 = 01000000 ' 0x22 = 01000100 ' 0x24 = 01001000 ' 0x26 = 01001100 ' 0x28 = 01001000 ' 0x2A = 01010100 ' 0x2C = 01010100 ' 0x2E = 01011100 'set I2C-IT slave address 'hi2csetup (set PICAxe master), (device address-don't forget "%" prefix!), (100kHz network speed), (address method) hi2csetup i2cmaster, %01000000, i2cslow, i2cbyte main: 'main program loop b0 = 0 'set variable b0 to 0 to clear any unwanted data hi2cin 1,(b0) 'This command actually sets the I2C-IT to return inches and stores that data in b0 'if you look at the datasheet, you will see that the hi2cin command allows for a write 'normally used to select what register you want to read from. However, the I2C-IT does 'not work like a memory chip and sees the write as a command to set its mode (1=inches, '2=cm, or 3=raw) so we take advantage of that and eliminate the need for another line of code. debug b0 'echo the results out goto main 'rinse, and repeat! end