Dallas Real-Time Clock Interface

Here you can share completed projects or parts of projects that may be useful to others. You may post files relevant to ZBasic - source code, schematics, etc.
Post Reply
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Dallas Real-Time Clock Interface

Post by spamiam »

Here is API code to utilise the hardware I2C functions of the ZX-24 hardware and interface with the DS3231 precision real-time clock. THis is a nice piece of hardware which combines an extremely precise industrial-range temperature compensated oscillator with a real-time clock.

This code will also work with the more simple DS1337 RTC. The commands for the 1337 are a subset of those for the 3231.

Not all 3231 functions are fully supported in this API, just those I needed when I wrote the code. I will be writing some code to read the temperature of the oscillator, but this is not complete yet....

-Tony
Attachments
DS3231_API.bas
(23.02 KiB) Downloaded 4015 times
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

Here is a program to use the DS3231. It was assembled just now from a more complex program with cuts and pastes. It might need a little tuning.





Private Const RTC_I2C_Channel as Byte = 0 'hardware!

Public Sub main()
Dim year as Byte
Dim month as Byte
Dim Mday as Byte
Dim hour as Byte
Dim minute as Byte
Dim second as Byte
Dim Success as Integer

Dim C_hour as Byte
Dim C_Minute as Byte
Dim C_second as Single
Dim C_Tick as Integer

Dim last_second as Byte


year = 05
month = 09
MDay = 17
hour = 20
minute = 21
second = 22
Success = 1
Last_second = 60

'Call LCDInitialize(LCDPort, 9600, LCDInputPin, LCDOutputPin)

Call OpenI2C(RTC_I2C_Channel,0,0,I2CMAX) 'open hardware I2C port at 409K bps

'otherwise use I2C400K


Call RTC_Set_Channel (RTC_I2C_Channel)
'Call FRAM_Set_Channel (RTC_I2C_Channel) 'They can be on the same channel



If (RTC_Osc_Stopped) Then 'the clock is wrong
Call RTC_Initialize ( RTC_I2C_Channel, _
RTC_Bat_Clk_On, _
RTC_Bat_SQW_Off, _
RTC_SQW_1KHz, _
RTC_SQW_Enable, _
RTC_Alm1_Disable, _
RTC_Alm2_Disable, _
RTC_32KHz_Enable)
Call RTC_PutTimeStamp (5,9,18,1,2,58, Success)
Call PutTimestamp(5,9,18,1,2,58.0)
Call LCDmovecursor(4,1)
Call LCDPutStr("Time lost")
Else 'the clock has not stopped
Call RTC_GetTime (hour, minute, second, Success)
last_second = second
'set all the settings to a known state (may have been different even with "good time")
Call RTC_Initialize ( RTC_I2C_Channel, _
RTC_Bat_Clk_On, _
RTC_Bat_SQW_Off, _
RTC_SQW_1KHz, _
RTC_SQW_Enable, _
RTC_Alm1_Disable, _
RTC_Alm2_Disable, _
RTC_32KHz_Enable)
Call LCDmovecursor(4,1)
Call LCDPutStr("No time lost")
'wait For the top of a second
do
Call RTC_GetTime (hour, minute, second, Success)
loop while (second = last_second)
'now set the cpu time
Call PutTime(hour, minute, CSng(second))
End If

End Sub
Post Reply