# Two wire vs. three wire serial interface for RTC

I'm looking to choose an RTC for a project I have in mind.

I have the following options (linked to datasheet):

The main difference seems to be the communication protocol. The DS1307 seems to use something called "two-wire serial interface" whereas the DS1302 uses something called the "3-wire interface".

I would like to use one of these chips to keep time on my ATMega microcontroller (probably ATmega8).

So how are these two different, and how would I hook them up?

The main difference between SPI (3-wire interface) and I2C (2-wire interface) is, that you have to send an address when using I2C. So it won't be as fast as SPI because you have to send more data.

When using SPI you will need an extra pin for the chip enable signal. This will be required for each slave. So it's faster, but you need more output pins.

I would prefer I2C because speed doesn't matter in this scenario.

The ATMega microcontroller has hardware peripherals for both methods so it's up to you

• Thanks, answered my question perfectly, with a concise recommendation. – capcom Jan 20 '13 at 20:08
• With only one slave, you can wire the enable signal to the power rail and use it as a 2-wire interface. SPI is then much easier to debug because you don't need to deal with a protocol. – Potatoswatter Jan 21 '13 at 3:19
• @Potatoswatter: that's not correct. First, SS should be connected to ground, not power. Second, you still need 3 lines: clock, data out and data in (SCK, MOSI, MISO). – Federico Russo Jan 21 '13 at 9:09
• @Potatoswatter: Some SPI devices may be usable with SS forced enabled, but many devices use it not only to identify which communications are intended for them, but also to mark the start of each command. – supercat Nov 19 '13 at 18:09

The first runs off I2C. Timing information is in Figure 5 on page 7 of the datasheet.

The second runs off SPI. Timing information is in Figure 4 on page 8 of the datasheet.

It looks like the ATmega8 has TWI and SPI just like all the other ATmega parts, you can use both with it, simultaneously if you choose. My preference would be SPI. It's a bit easier to work with, and as a protocol tends to be a bit less temperamental.

• Thanks a lot. You and Hardwarefreak responded simultaneously, but I found his answer to be a bit more complete. Many thanks again. – capcom Jan 20 '13 at 20:10

It depends on a few factors:

1. Hardware design: If you are using only this chip with your microcontroller, anyone of them will do. But if your design consists of two or more devices that use I2C interface then it makes sense to use I2C RTC so that you don't have to do additional interfacing, just add the RTC to existing bus. Same applies to SPI but keep in mind when you add more devices to the SPI bus, you need one extra SS signal per device. It also depends on the hardware peripherals that you have in your microcontroller. If your micro has hardware SPI peripheral but not I2C then it make sense to use SPI device over an I2C device. Same goes with I2C peripheral.

2. Software design: Another deciding factor is whether you are implementing either protocol in software. I2C involves a bit more work than SPI. You can also find some code implementations over the web. If you already have a I2C code with high confidence, use it. Same applies if you are pretty confident with a piece of code for SPI that you found over the web.

3. Your understanding: The choice also depends upon your understanding of either protocols and how well-versed you are with these. If you have used anyone of them before, it makes sense to use prior experience.

• Thanks Chetan. Although you didn't answer my question directly, your answer is an excellent supplement to the above answers. Extremely helpful. – capcom Jan 20 '13 at 20:11

The DS1302 does not truly use standard SPI because the single "I/O" data signal is time-multiplexed during communication (driven by the chip during a read between the falling-edge of "CLK" to just after the rising-edge of "CLK", high-z at all other times).

Standard SPI has separate MOSI (Master-Out/Slave-In) and MISO (Master-In/Slave-Out) pins for each peripheral (although the MISO can be three-stated by the peripheral if it is multiplexed with multiple slaves).

The DS1302 "CE" (previously referred to as "/RST") is absolutely essential in communication to start/stop each transmission, plus store the RTC data after a "Burst" (you can NOT simply connect it to a rail). Also, the first bit sent after CE is asserted indicates if you are doing a Read (1) or a Write (0). That being said, there's still less protocol overhead than using I2C, because you don't need to worry about I2C-specific addressing and timing.

Even at the 2V specification (the datasheet gives timing for 2V and 5V), the DS1302 is still faster than using I2C, and WAY faster when using 5V. Be aware that each of the 3 communications pins for the DS1302 have a (nominal) 40k pull-down resistor, to avoid allowing the pins to float.