Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 19:31 29 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM - replacing 1307 with 3231 RTC

     Page 3 of 3    
Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 04:34am 13 Aug 2014
Copy link to clipboard 
Print this post

Now I get it. In the routine settime, you are calculating the dow from the date$ and sending it to the ds3231 to set it initially. Sorry for my confusion. I makes perfect sense now. Real nice routine!!!!
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 06:57am 13 Aug 2014
Copy link to clipboard 
Print this post

Hi all!

I think this is could be wrong:
Dow=(day+y+Int(y/4)-Int(y/400)+Int(31*m/12)) Mod 7

I use TZAdvantage's DOW Calculation like this:
Dow=(day+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12)) Mod 7

Sunday = 0.
I use some modification of jman's code för my DS3231SN/24C32 modul. Works perfectly (nearly)! Before that I used some DS1307 and PCF8563's. I dont like them anymore.
The DS3231/24C32 modul takes less then 100nA (Battery mode), costs (on eBay from china-dealer) less then 1,4 EUR and includes a 4K eeprom + a CR2032!

'Jman's code for the DS3231
'No alarms or offset
'Calculation to set Day of the week
'SETTIME from the console will set the DS3231 with the current
'time and date
' I2C RTC based On DS1307 Secs,Mins,Hours,Day,Date,Month,Year,Control
'DayOfWeek() corrected + some small optical improvements 10-8-2014

Option base 0
Dim RTCbuff(7)
Dim TempBuff(2)
Dim Days$(7)

For I = 0 To 6
Read Days$(I)
Next I

Data "Sunday"
Data "Monday"
Data "Tuesday"
Data "Wednesday"
Data "Thursday"
Data "Friday"
Data "Saturday"

'*********************
' Start Mainloop
'*********************
Do
ReadTime 'from DS3231
ReadTemp 'from DS3231

Cls
Print "Time has been set to ";Time$
Print"Date has been set to ";Date$
Print "Today is ";Days$(DayOfWeek())
Print "Temperature is";Temperature-2.25 'brute force correction
Pause 1000
Loop

End
'*********************
' End Mainloop
'*********************



'*********************
Sub ReadTime 'from DS3231 I2C
'*********************
i2caddr = &h68 ' DS3231 I2C address
I2C open 100,100 ' Enable I2C
I2C write I2caddr, 0, 1, 0
I2C Read i2caddr, 0, 7, RTCbuff(0) ' Read Time
I2C Close

sec$ = Str$(BCDtoDec(RTCBuff(0)))
min$ = Str$(BCDtoDec(RTCBuff(1)))
hours$ = Str$(BCDtoDec(RTCBuff(2)))
DOW$ = Str$(BCDtoDec(RTCBuff(3))) 'day of week?? always=0?
day$ = Str$(BCDtoDec(RTCBuff(4)))
month$ = Str$(BCDtoDec(RTCBuff(5)))
year$ = Str$(BCDtoDec(RTCBuff(6)) + 2000 )

t$ = hours$+":"+min$+":"+sec$
D$ = day$+"/"+month$+"/"+year$
Time$ = T$
Date$ = D$
End Sub


'*********************
Sub ReadTemp ' read temperature from DS3231 I2C
'*********************
i2caddr = &h68 ' DS3231 I2C address
I2C open 100,100 ' Enable I2C
I2C write I2caddr, 0, 1, 17
I2C Read i2caddr, 0, 2, TempBuff(0) ' Read Temperature
I2C Close

If Tempbuff(0) < &h80 Then
Temperature= TempBuff(0) + (TempBuff(1) / 256)
Else
Temperature = -((TempBuff(0) Xor &HFF) + 1) + (TempBuff(1) / 256))
EndIf
End Sub


'*********************
Sub SetTime ' Get time from time$ and date$
'*********************
hours = BCDtoHex(Val(Left$(Time$, 2)))
minutes = BCDtoHex(Val(Mid$(Time$, 4, 2)))
seconds = BCDtoHex(Val(Right$(Time$, 2)))
day = BCDtoHex(Val(Left$(Date$, 2)))
month = BCDtoHex(Val(Mid$(Date$, 4, 2)))
year = BCDtoHex((Val(Right$(Date$, 4)) - 2000))

' Write Time to RTC
i2caddr = &h68 ' DS3231 I2C address
I2C Open 100,100 ' Enable I2C
I2C Write i2caddr,0,8,0,seconds,minutes,hours,dow,day,month,year
I2C Close

Print "0=ok 1=nack 2=timeout"; MM.I2C
If MM.I2C=0 Then Print "RTC has been set to ";Time$;" ";Date$ Else Print "Error!"
End Sub


'*********************
'Calculate Day Of Week TZAdvantage's DOW Calculation
'*********************
Function DayOfWeek
a = Int((14-Val(Mid$(Date$,4,2)))/12)
m = Val(Mid$(Date$,4,2)) + 12*a - 2
y = Val(Mid$(Date$,7,4)) -a
day = Val(Mid$(Date$,1,2))

DayOfWeek = (day+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12)) Mod 7
End Function


'*********************
' Convert to Hex
'*********************
Function BCDtoHex (TempDec)
BCDtoHex = Fix(tempdec / 10) * 16
BCDtoHex = BCDtoHex Or ((tempdec / 10) - (Fix(tempdec / 10))) * 10
End Function


'*********************
' Convert to Decimal
'*********************
Function BCDtoDec (BCDTEMP)
BCDtoDec = Fix(BCDTemp / 16) * 10
BCDtoDec = BCDtoDec + (BCDTEMP And &hF)
End Function


With respect to Jman. I hope, it's okay to post it here?

Michael
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 07:33am 13 Aug 2014
Copy link to clipboard 
Print this post

Hi Two Fingers,

I noticed that when you are reading the register for dow that you commented 'day of week?? always=0?. I think that once you set that register correctly, that it will change accordingly and stay correct. Have you tried that yet?
As far as your calculation for dow, I can't check right now, but I have been using this for a while and has been working properly for me. I will use yours to check when I get home.

I used to live in Hanover as a child. Not so young anymore and live in Florida USA. Where are you?
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 07:58am 13 Aug 2014
Copy link to clipboard 
Print this post

Hi viscomjim,

I don't use the DS3221 registers for 'day of week' determination. I don't see any sense in doing so because as I understand I have to set first this register and calculate the dow.
  Quote  I think that once you set that register correctly, that it will change accordingly and stay correct. Have you tried that yet?

I did not try it that way.

I live in Luebeck. Many years ago I traveled through the States. I like the Everglades, the Keys and Manatee Springs ... all american state parks!
  Quote  Not so young anymore ...

Ich auch!

Michael
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 08:25am 13 Aug 2014
Copy link to clipboard 
Print this post

If you use the for next you have...

For I = 0 To 6
Read Days$(I)
Next I

Data "Sunday"
Data "Monday"
Data "Tuesday"
Data "Wednesday"
Data "Thursday"
Data "Friday"
Data "Saturday"

When you read RTCbuff(3) you can use that for I and that will be your day of the week. Once it is set, you can just read that register and you will have your day of the week without having to calculate it. I think the calculation was there only to initially be able to program the DS3231.

I agree with you on the other RTCs, once I was able to use the DS3231, I don't like the others either. They drift way too much for me.

Luebeck, only 2.5 hours away from my old home.
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 09:45am 13 Aug 2014
Copy link to clipboard 
Print this post

Hi viscomjim,

  Quote  When you read RTCbuff(3) you can use that for I and that will be your day of the week. Once it is set, you can just read that register and you will have your day of the week without having to calculate it. I think the calculation was there only to initially be able to program the DS3231.

I'm sure this should work. I was just too lazy to write a Sub to set the DOW-register.

  Quote  They drift way too much for me.

Yes. My DS1307/PCF8563-modules drifts about 0,5sec/h. The DS1307 consumed 2,2µA, including die voltage divider.

To test the PCF8563 (and Geoffs great improvment RTC GETTIME/SETTIME) I just cutted the DS1307 out and replaced it with a PCF8563 (10 pieces for 1,7 EUR at ebay). PIN 3 = NC. PIN 8 = D1/R6. It takes about 0,5µA (without R4). I did find no way to correct the drift.




The image above shows a DS1307-module with a replaced PCF8563 (DS1307<->PCF8563).

eBay-DS1307 modules - the schematic - are made for lithium accus (LIR2032) but often sold with a CR2032 battery.

The DS3221 seems to be the able to tune its frequency by software. But 2ppm is enough for my purposes.

  Quote  Luebeck, only 2.5 hours away from my old home.

Yes. Near the baltic sea.

Michael Edited by twofingers 2014-08-14
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 11:51am 14 Aug 2014
Copy link to clipboard 
Print this post

Hi TF,

I just tried both yours and TZ's calculation for Day of Week using the following...


For D = 1 TO 7
DATEX$ = STR$(D)+"/8/2014"
print "dateX$ = ";DATEX$

a = Int((14-Val(Mid$(DateX$,4,2)))/12)
m = Val(Mid$(DateX$,4,2)) + 12*a - 2
y = Val(Mid$(DateX$,7,4)) -a
day = Val(Mid$(DateX$,1,2))

DowTZ=(day+y+Int(y/4)-Int(y/400)+Int(31*m/12)) Mod 7
dowTF=(day+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12)) Mod 7
print "tz calculation = ";dowTZ
print "tf calculation = ";dowTF
NEXT D


and the results were the same as follows...


RUN
dateX$ = 1/8/2014
tz calculation = 0
tf calculation = 0
dateX$ = 2/8/2014
tz calculation = 1
tf calculation = 1
dateX$ = 3/8/2014
tz calculation = 2
tf calculation = 2
dateX$ = 4/8/2014
tz calculation = 3
tf calculation = 3
dateX$ = 5/8/2014
tz calculation = 4
tf calculation = 4
dateX$ = 6/8/2014
tz calculation = 5
tf calculation = 5
dateX$ = 7/8/2014
tz calculation = 6
tf calculation = 6


Without racking my brain too much, they both seem to work fine.
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 12:44pm 14 Aug 2014
Copy link to clipboard 
Print this post

Hi viscomjim

  Quote  I just tried both yours and TZ's calculation


As I explained yesterday (see above at 13 August 2014 at 4:57pm) it is TZs algorithm:
(day+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12)) Mod 7

To verify try please: date$="01.01.2000" a saturday (DOW=6).

Michael
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:55pm 14 Aug 2014
Copy link to clipboard 
Print this post

Ahhhhhhh, thats interesting. Will have to explore a bit more. Thanks TF.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 11:00pm 28 Nov 2014
Copy link to clipboard 
Print this post

Back on page 1:

[Quote=jman]You can do a drop in replacement and it should go fine. [/Quote]

He was right, and I can confirm that the DS3232 as a direct replacement for the 1307 works just fine, and it is much, MUCH more accurate over several days then the drifty 1307.

The 1307 does have cost advantages over the much more expensive 3232 though, so I guess it depends on what you want.

However, I am happy to be able to report that the 3232 works perfectly right out of the box with the Maximite.
Smoke makes things work. When the smoke gets out, it stops!
 
     Page 3 of 3    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024