Driver for S-35390A RTC chip
Roy Eltham
Posts: 3,000
Attached is a driver for the Real Time Clock chip on the Spinneret Web Server board.
You need to get the basic_i2c_driver file from the obex here: http://obex.parallax.com/objects/26/
Or alternatively you can use the pasm_i2c_driver from the obex here: http://obex.parallax.com/objects/611/ (you'll need to modify the OBJ section to change basic to pasm if you use this one)
What is implemented:
Setting and reading of date and time
Handling 12 hour or 24 hour mode (setting the time is always in 24 hour mode)
Setting Alarms and reading if they have occurred.
Reading and writing a user data byte that is retained across power cycles
Functions to get and set the Clock Correction register
Functions to get and set the 2 status registers (temporary)
I added a bunch of comments to explain more of what's going on
What is not implemented:
The user frequency stuff (drives the int pins)
There is a second byte of user data spread across 3 registers
Usage info:
You need to call start at the beginning of your program before calling anything else. Then call SetDateTime(...), SetTime(...), and/or Set24HourMode(...) as needed.
Use SetAlarm(...) to set either of the two alarms, and call DidAlarmOccur() to see if one of the alarms went off. You should call Update before calling DidAlarmOccur() (and at least once between calls to it also).
To read the date/time, first call Update, then call any of the GetXXXX functions.
Use SetUserData(value) to store and value := GetUserData to retrieve a byte of user data.
Note about clock correction:
You should read the datasheet for a detail description of what values to set on the clock correction register.
Note about the status registers:
You should read the datasheet and the comments in my code before you call any of the functions that deal with the status registers. In normal usage of this driver, you shouldn't need to call these functions, but I put them in for now since I haven't implemented everything yet.
You need to get the basic_i2c_driver file from the obex here: http://obex.parallax.com/objects/26/
Or alternatively you can use the pasm_i2c_driver from the obex here: http://obex.parallax.com/objects/611/ (you'll need to modify the OBJ section to change basic to pasm if you use this one)
What is implemented:
Setting and reading of date and time
Handling 12 hour or 24 hour mode (setting the time is always in 24 hour mode)
Setting Alarms and reading if they have occurred.
Reading and writing a user data byte that is retained across power cycles
Functions to get and set the Clock Correction register
Functions to get and set the 2 status registers (temporary)
I added a bunch of comments to explain more of what's going on
What is not implemented:
The user frequency stuff (drives the int pins)
There is a second byte of user data spread across 3 registers
Usage info:
You need to call start at the beginning of your program before calling anything else. Then call SetDateTime(...), SetTime(...), and/or Set24HourMode(...) as needed.
Use SetAlarm(...) to set either of the two alarms, and call DidAlarmOccur() to see if one of the alarms went off. You should call Update before calling DidAlarmOccur() (and at least once between calls to it also).
To read the date/time, first call Update, then call any of the GetXXXX functions.
Use SetUserData(value) to store and value := GetUserData to retrieve a byte of user data.
Note about clock correction:
You should read the datasheet for a detail description of what values to set on the clock correction register.
Note about the status registers:
You should read the datasheet and the comments in my code before you call any of the functions that deal with the status registers. In normal usage of this driver, you shouldn't need to call these functions, but I put them in for now since I haven't implemented everything yet.
spin
26K
Comments
Thanks for the RTC driver.
'
I just got My Spinneret, So I'm still reading, wiriing,etc.
'
This code will be very usefull.
'
Thanks Again
Have you checked the accuracy of the real time clock on your board? I set the time on mine yesterday, and it gained 18 seconds in 24 hours. That works out to 208 PPM. I set the frequency adjustment register to compensate for the fast clock. I'll check it again tomorrow to see how close the time is.
Dave
Of course that only covers when it's turned on and connected to the internet, so I may need to adjust the correction register.
Seriously, thanks for the code.
OBC
According to my reading of the datasheet, if you wrote $40 to the register then that would be a correction of +6.1. For the -195.3 you'd need to write $04.
I will test my RTC correction needs over the next few days.
Ok, yeah I am not sure why they bit reversed everything also. I get mixed up by it too (as you can see if put in $04 instead of $02. Have you tried the other option for correction that does it more often with a smaller range of adjustment?
I am still curious whether anybody else has looked at the accuracy of the RTC frequency, and whether their's is off by as much as mine. Is anybody working on getting the time from a time server? It would be nice to have the RTC automatically set and adjusted based on a time server.
Also, you have probably already seen it since this thread is over 6 months old, but Beau Schwabe posted some working code to set the RTC from internet SNTP time and I just added some code for the automatic adjustment for DST.
http://forums.parallax.com/showthread.php?129435-DEMO-Using-SNTP-to-sync-the-Spinneret-s-on-board-RTC
Ray