The documentation in the Picbasic manual isn't so good for I2C commands. I've been screwing around with LOTS of I2c for the past 5 weeks, and I can tell you that all the commands that are shown in the green Picbasic book are for Eprom, and are confusing.

Here's my version...


I2CWrite Datapin, Clockpin, Deviceaddress, data,data, data,data, [data with stop bit, optional]

Now here's the deal...

The chip address is whatever you set, or whatever the chip is default set to. The first 7 bits are the address. The last bit is whether you're reading or writing.

A chip with an address of 1100111 would have to have it's address followed by a 0 if you were using an I2CWrite, and a 1 if you were reading. Hence

I2CWrite Dpin,Cpin, %11001110, [single byte of data]
I2CRead Dpin,Cpin, %11001111, [single byte of data]

This MIGHT be dependant on the device, so check your datasheet. I'm using a Phillips chip, and since they invented the protocol, I'll guess that it's relatively standard.

OK.. Now about those brackets... Those denote the Acknowledge bit. In sending, say your chip wants to get 8 bytes in sequence...


I2CWrite Dpin,Cpin, %11001110, Byte1, Byte2,Byte3,Byte4,Byte5,Byte6,Byte7,[Byte8]

OR (this would work but it's stupid and would eat up code space)

I2CWrite Dpin,Cpin, %11001110, Byte1, Byte2,Byte3,Byte4,
I2CWrite Dpin,Cpin, %11001110, Byte5,Byte6,Byte7
I2CWrite Dpin,Cpin, %11001110, [Byte8]

The bracket sends the Acknowledge bit. It's kinda like the "End Of Transmission" signal.

I've found that if you're doing a lot of I2C, it's best to make a subroutine with ONE I2C command in it that sends from variables. Set the variables, then call your sub. In this one program I'm working on, with individual I2CWrite lines, sending a single I2C byte took 17 words of data. Sending 14 bytes of data ate up 56 words. Probably not a bad figure considering that it's bit banging, but still, worth looking in to doing in hardware, plus the fact that it'll cut back my CPU time.

I'm off to learn assembly.

Andy