OK, you've posed a whole heap of questions... so let's take this apart...

1. Yes, all the variables were prevoiuly decalred as bytes. You can send words, they are sent highbyte first which can conflict with the usual default memory storage arrangements. If in doubt use the highbyte or lowbyte modifier and send bytes to ensure your target device gets data in the arrangement it expects.

2. $D0 (always do Hex in capitals as some assemblers will turn around and bite you if you use lower-case). Yes, it's the device address. I didn't do it in this example, but ideally you will have previously defined this as either an 8-bit or 16-bit variable (to suit your device) and preloaded ($D0 in my case) into it. No, don't worry about changing bit 0 of the address to a '1' or a '0' (depending on Read or Write) as the I2C commands will automatically handle that bit for you. I usually always leave it as '0'.

3. $00. Yes this is the Register address. My example showed me reading eight bytes starting with address $00. If I only wanted to read the Hours Register, I could have executed...

I2CRead SDApin,SCLpin,$D0,$02,[RTCHour]

4. Writing would be the opposite of reading (including my new example above). The Data bytes would have been preloaded with the information targeting the registers I wanted loaded with that data. As I said before, don't worry about bit 0 of the Address, it's handled for you. If you do include it, no harm done as the I2C commands will ignore it and do their own thing anyway.

5. Strip a byte apart using the bit manipulation facilities of PICBasic.

Example 1. Let me use the Seconds Byte RTCSec in my example. When you first power-up a DS1307 (you just got it fresh from Dallas), and you apply power to it (either by slotting-in a Battery or from it's main +5v Supply, or both, or if it's been powered off without Battery Backup), then the chip basically is brainless. It has no Date, no Time, and everything (including it's NVRam which is only NV if there's a battery connected - kinda like my Laptop!) is in an unpredicatable state. Worse, the chip isn't even ticking. The only thing that you DO know is that bit 7 of the Seconds Register is set to '1'. So, you read the Seconds Register and do this...

If RTCSec.7=1 goto SetDateandTime

Example 2. You want the DS1307 to output a 1 second tick on it's SQW/OUT pin. This is done by setting bit 4 to 1, and bits 1 and 0 of the Control Register to zero. Two ways to do this... either...

(a) Read the Control Register Byte first, then do this...

RTCCtrl.4=1
RTCCtrl.1=0
RTCCtrl.0=0

and then write the Byte back out again into the Control Register (which will not affect the setting of the other five bits), or,

(b) Preset the entire Control Register BYTE (all eight bits of it)...

RTCCtrl=%00010011

and again write the whole byte out...

You will notice we can only do BYTE I2CREAD/WRITE operations, and cannot manipulate a single individual bit. The method you chose is up to you. This chip requires method (b) to initialise because you can't rely on Dallas's engineers to have built it properly to initialise itself into any predetermined state - therefore you have to do it. The Datasheet of your chosen I2C device should tell you the state of all it's internals at Power-Up.

The I2C bit is really easy - but study the Datasheet of your chosen device. See what state it's in and what it needs to initialise it from Power Up. Once you've done that, everything is as sweet as Mom's Apple Pie...

Melanie