View Full Version : DS1307 vs DS3231?

15-06-2011, 01:48
I am trying to use the code from page 7 of the attached pub, but I have a DS3231 instead of a DS1307. I thought the code would be identical for the two RTC's, but am not sure. Any help would be appreciated.

Andrew Cowan
15-06-2011, 02:22
The data storage uses the same locations - RTC in addresses 00h to 07h.

The address of both parts is 1101000, so you shouldn't have any software differences - only the hardware is different.


15-06-2011, 06:22
The DS3231 also has a RST (reset) pin. Do you have that pulled high via say a 4k7 or 10k Ohm resistor?

EDIT: Okay not needed - "RST pin is pulled high by the internal pullup resistor."

Do you have a back-up battery connected? if not, read the datasheet about where to connect the Vbatt pin

15-06-2011, 10:40
Hi tracecom,
i use this same sample code here for my ds3231
you may also be interestested in my latest project using a lcd and the ds3231

16-06-2011, 19:35
As most of you know, I am a novice when it comes to PICAXE programming, but I am pedantic and persistent (often an annoying combination.) If I am correctly reading the Timekeeping Registers tables in the two datasheets (DS1307 and DS3231), there are some programming differences beginning at address 07h. I believe that means instructions at or above that address that were written for the DS1307 will not run correctly in the DS3231.

The specific problem that I am having is that I cannot get the 1 Hz square wave output on the DS3231 to start. It is my assumption that that instruction must be sent as part of a time setting program as shown in the attached code. However, the square wave output doesn't appear. What am I doing wrong?

'Sets time for 17:04:08 on Wednesday, 15th of June, 2011, and turns on the Square Wave output.

i2cslave %11010000, i2cslow, i2cbyte

writei2c 0, ($08, $04, $17, $04, $15, $06, $11, $00, $00, $00, $00, $00, $00, $00, $40)

16-06-2011, 20:00
Are you just looking at the pin with your scope?

The device is open drain so will need a pull up

16-06-2011, 20:46
Are you just looking at the pin with your scope?

Worse than that! I was looking for 5v with my DMM. I'll try a 4.7k pullup. Thanks.

17-06-2011, 09:09
I'm only learning too lol
you should only really need to send it once
i think pin 3 output is only rated at 3ma
ive only ever used the first 7 addresses so havent tried the others
hopefully this works lol.

i2cslave %11010000, i2cslow, i2cbyte

writei2c 14, ($0) ' DS3231 turn on $0 1hz pin3, turn off $4

17-06-2011, 12:31

looking further at the DS3231 datasheet, it is far look like the DS1337 RTC chip that others have used in the past with two alarms, the control register at location $0E (14dec) and same bit functionality.

The DS1337 does not have provision for battery backup to a specific pin.

Have a search on this forum for the DS1337 and you will likely find some past code by other members.

17-06-2011, 13:14
Hi tracecom,
i just had amoment to test that code in post 8
its works ok
i also tried $40 which you had
it also works just the difference being when supply removed it will
continue to pulse 1hz from pin3 with bat backup.
your original code in post 5 also works.

18-06-2011, 01:23

I have retrieved the datasheet for the DS1337 and will review it. Your post also reminded me that I actually have a DS1337C that I ordered without understanding that it was SMT. Thanks.


I had already reviewed your project using the 20X2 and DS3231. In fact, I have it breadboarded, but am using a different display and haven't yet made your code run. Thanks.

18-06-2011, 20:42

Your post #5 was key for me -to finally- understand how to address the registers past just the time settings.

Also the answer to your problem of no square wave output is below (so I believe).

My DS3231 is mounted on a custom SMT to TTH board called a “ChronoDot” from MaceTech. I used your code with some modifications and can consistently control the advanced registers and output.

You didn't mention (did you?) that you have 10K pullUP resistors on both DS3231 pins one and three.

I too am a novice, so being clear is best. Place a 10K resistor between pin one and 5V, and a 10K resistor between pin three and 5V.

This assuming assuming that you have two 4.7K pullUPs between the SDL to 5V and SCL to 5V.

The Maxim spec sheet talks about the 10Ks and how pins one and three are "open-drain" on page nine.

MaceTech talks about it here

Maxim also states that all N/C pins (5-12) "Must be connected to ground".

Here is what I ended up with:

i2cslave %11010000, i2cslow, i2cbyte
' secs min hrs dow date mon yr A1sec A1min A1hou A1day A2min A2hou A2daydat ctrl ctrlstat aging,TempMSB,TempLSB)
writei2c 0, ($00, $55, $12, $07, $18, $06, $11, $00, $00, $00, $00, $00, $00, $00, 208)

Note the control entry “208”
It could have been entered "$D0" instead.

Both convert to 11010000.
The Control Register settings to enable a square wave at 4.096kHz are:
(apologies for alignment)

' 11010000 Ctrl Reg :: Enable Osc / SQW Enable / No Coversion / Set BitRate 1 /SetBitRate2 / Int Enable / Alm1 / Alm2
' 1 1 0 1 0 0 0 0
' 11010000 binary converts to 208 decimal

Using a logic analyzer (LA), I can see the square wave and SWQ outputs.

Indeed, I spent close to an hour with the LA.
Using your code that "marks" confirms to be valid, I could not figure out why there was no output for either the square wave or the SQW. Worse yet, pulses would sometimes show up, then fail.

Then I re-read the MaceTech notes about the 10K pullups.

Instant success. Please see attached for views showing the 32kHz and the 4.096 readings.

For digital troubleshooting, a logic analyzer has been well worth the money to me. No way would I have solved this issue without one.

They can be found for as little as $50.

Good luck,


18-06-2011, 21:07
Well done.

I find that my £5 set of reading glasses help me with data sheets and tech notes to indicate what has to be done to make things work :D:D:D

19-06-2011, 00:11
@ TAMeyer

Thanks for the feedback. Our situations are very similar; I, too, am using the Chronodot.

After the comment by russbow, I did put a pullup resistor from SQW to vcc (I used a 4.7k) and then connected an LED to the SQW. The LED does flash at 1Hz when I enable SQW. I don't have a pullup on the 32KHz output because I am not using it, but will add a resistor there so I don't forget it if I do use the 32KHz output in the future.

I had not tried using decimal numbers to program the DS3231, but made a note to try it this afternoon. Good to know that will work as well.

I don't have a logic analyzer and have been making do with DEBUG. I program the DS3231 and then use the following code to see if my changes are in the appropriate register. I also note that the temperature sensor in the DS3231 seems to be working: see the contents of b17 and b18.

i2cslave %11010000, i2cslow, i2cbyte

readi2c 0, (b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18)
pause 1000
goto main

I want to get the time and temp to display on my LCD, but first I have to convert it from BCD to decimal. I know there are code examples here on the forum, but I haven't tackled that yet.

Good luck.

19-06-2011, 19:34
Wow thats impressive those timing diagrams @ TAMeyer
anyway here's some code to try!

i2cslave %11010000, i2cslow, i2cbyte
readi2c $11, (b1,b0) ' DS3231 read temp

sertxd (CR, LF,#b1,".",#b0," degrees Celsius", CR, LF)

W1=B1*100+B0 : W1=W1/5*4+W1+3200
b1=w1/100 : b0=w1//100
sertxd (CR, LF,#b1,".",#b0," degrees Fahrenheit", CR, LF)

pause 1000 : goto main

19-06-2011, 22:41
Here is my version of the code required to read the time from the DS3231 and display it. The code is running now on a 20X2, but I haven't yet seen it change from PM to AM or display times with double digit hours. ETA: As of 3:45 PM on 6/22/11, the code has been running for about 72 hours with no problems. It displays the time correctly as well as the AM/PM indicator.

'DS3231 RTC & PICAXE-20X2

symbol SECS = b0
symbol MINS = b1
symbol HOURS = b2
symbol AM_PM = b19
symbol HRS_TENS = b20
symbol HRS_ONES = b21
symbol MINS_TENS = b22
symbol MINS_ONES = b23
symbol SECS_TENS = b24
symbol SECS_ONES = b25

let AM_PM = "A"

i2cslave %11010000, i2cslow, i2cbyte
readi2c 0, (SECS, MINS, HOURS)
if HOURS > $60 then : let AM_PM = "P" : endif
if HOURS > $12 then : let hours = hours - $40 : endif
if HOURS > $12 then : let hours = hours - $20 : endif
if HRS_TENS = "0" then : let HRS_TENS = " " : endif
pause 1000
goto readtime

Here is the code I used to set the DS3231.

'Sets DS3231 for 12 hour operation and the time at 09:29:18 am.
'Sets the day at 01 (Sunday.)
'Sets the date as 19th of June, 2011.
'Sets no alarms.
'The $04 at the end turns off the Square Wave output.

i2cslave %11010000, i2cslow, i2cbyte

writei2c 0, ($18, $29, $49, $01, $19, $06, $11, $00, $00, $00, $00, $00, $00, $00, $04)

Here is a pic of my breadboard and display with the Chronodot in operation. Note that since the picture was taken, I have added a 4.7k pullup to the DS3231 32k output.


Comments and corrections are welcome.

20-06-2011, 11:09
I had not tried using decimal numbers to program the DS3231, but made a note to try it this afternoon. Good to know that will work as well.

Good luck.

Anywhere thAt a value is required within a program, you can use either:
1. A binary number (prefixed with %)
2. a decimay number, or
3. A hex number (prefixed with either $ or 0X)

The BCD is just a special case of hex numbers

The "trick" is to know which format/type provides the best alignment with the specific application at a given Point in each program.