RTC Module (Part 6)

Part 1234567

Here are some pieces of code which has been updated from my previous PlainRTC library. A new specific PlainRTC1394 has been created; this library is also compatible with the fully revamped PlainSPI.

Firstly, lets have a look to the header file:

/*

	PlainRTC1394 library, Real Time Clock driver library
	MAXIM DALLAS DS1394 driver
	Copyright (C) 2011 Didier Longueville

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef PlainRTC1394_h
#define PlainRTC1394_h
/* Include std libraires */
#include  
/* Include custom libraires */
#include 

class PlainRTC1394: protected PlainSPI 
{
public:
	/* Constants */
	#define RTC_SECOND 0x01
	#define RTC_MINUTE 0x02
	#define RTC_HOUR 0x03
	#define RTC_DAY 0x05
	#define RTC_MONTH 0x06
	#define RTC_YEAR 0x07
	/* Constructor */
	PlainRTC1394();
	PlainRTC1394(uint8_t cs_pin);
	/* Functions */
	uint8_t GetData(uint8_t dataType);
	void InitRTC(uint8_t pin_cs);
	void SetData(uint8_t dataType, uint8_t value);

private:
	/* Constants */
	#define DISABLE 0x00
	#define ENABLE 0x01	
	/* Functions */
	uint8_t BinToBcd (uint8_t binValue);
	uint8_t BcdToBin (uint8_t binValue);	
	void ChipSelect(uint8_t status);
	uint8_t ReadRegister(uint8_t address);	
	void WriteRegister(uint8_t address, uint8_t data);
	/* Variables */
	uint8_t _pin_cs;

};

#endif

At first glance, nothing really new, except few small changes which make a whole difference. These changes were brought in order to unifiy the Plain Libraries in a way that all the SPI driven modules can work all together without (too many) conflicts. This is one of the reasons why the PlainSPI object is called in the protected mode. The ChipSelect function now belongs to each SPI driver library, using a privately declared _cs_pin.

Finally, the requirements for running such a library are limited to the initialization of the driver which can be done while declaring the PlainRTC1394 object (overloaded version of the constructor), or after the initialization, thanks to the Init() function. Then, all you have to do is to read or write the time and date values using the GetData() and the SetData() functions. Here is the example which is attached to the library:

/*

	Example of use of the PlainRTC1394 library, Real Time Clock driver
	Copyright (C) 2011 Didier Longueville

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
The DS1394 RTC is hooked to PORTB: 
Typical pinout
RTC	Arduino	Function
6		11			MOSI
7		12			MISO
8		13			SCK

4		9				CS
5						GND
10					VCC	
1						X1
2						X2
3						Bat+
*/

/* Include libraires */
#include 
#include 	
/* Create objects */
PlainRTC1394 RTC; 
/* Local variables */
uint8_t const CS_PIN_RTC = PINB1;
uint8_t lastSecond;

void setup() {
	Serial.begin(115200);
	/* Set time */
	Serial.print("Initializing RTC...");
	RTC.InitRTC(CS_PIN_RTC);
	Serial.println(" done");
	/* Set time */
	Serial.println("Setting default date and time to 1/1/2000 10:10:10");
	RTC.SetData(RTC_HOUR, 10);
	RTC.SetData(RTC_MINUTE, 10);
	RTC.SetData(RTC_SECOND, 10);
	/* Set date */
	RTC.SetData(RTC_DAY, 1);
	RTC.SetData(RTC_MONTH, 1);
	RTC.SetData(RTC_YEAR, 0);
	Serial.println("Ready");
}

void loop() {
	uint8_t second = RTC.GetData(RTC_SECOND);
	if (lastSecond != second) {
		lastSecond = second;
		Serial.print(RTC.GetData(RTC_DAY), DEC);
		Serial.print("/");
		Serial.print(RTC.GetData(RTC_MONTH), DEC);
		Serial.print("/");
		Serial.print(RTC.GetData(RTC_YEAR) + 2000UL, DEC);	
		Serial.print(" ");			
		Serial.print(RTC.GetData(RTC_HOUR), DEC);
		Serial.print(":");
		Serial.print(RTC.GetData(RTC_MINUTE), DEC);
		Serial.print(":");
		Serial.print(second, DEC);				
		Serial.println();
	}
	delay(1);
}

Next post on same subject

7 Comments

  1. […] Next post on same subject Tags: Hardware, RTC Comment (RSS)  |  Trackback […]

  2. JimEli says:

    Any thoughts on how to couple an RTC (txco) to an arduino to increase the timing accuracy? Most of my arduino’s crystals are terrible. I have been playing with a DS3231 and using the square wave output to realign the arduino timer through a pin-change interrupt. However, I’m not sure this is a good way to go.

    Thnaks for all of your postings. I find them very informative.

  3. Didier says:

    Thanks for your comment.
    You may probably upgrade your crystals for better quality components, which stay reasonably cheap.
    Which is your targeted time resolution? (SQW ranges from 1 to 8kHz)

  4. JimEli says:

    My initial experiments were at 1.024Hz. Unsuccessful, I might add. I tried running timer0 at higher multiple of the SQW and looking at TCNT0 inside the interrupt has it varying wildly. I’m running some new tests to see if latency is the issue or something else…

  5. Didier says:

    Hum, interresting! Please, keep us posted with your observations

  6. rosie says:

    I’m very new to Arduino and need an RTC at millisecond resolution, noted that the Dallas DS1394 RTC supports this…noted in the driver code above it doesn’t make reference to it?

  7. Didier says:

    MAXIM DALLAS specs say: “The low-voltage serial-peripheral interface (SPI™) DS1390/DS1391/DS1394 and the low-voltage 3-wire DS1392/DS1393 real-time clocks (RTCs) are clocks/calendars that provide hundredths of a second, seconds, minutes, hours, day, date, month, and year information.”

    I did not care for sub second precision, but it looks like you should not get trouble reading them at address 00h, 4 upper bits for tenths of seconds and the 4 lower bits for hundredths of seconds.

    untested suggestion:

    uint8_t tenthsOfSeconds = (RTC.GetData(0x00) & 0x0F);
    uint8_t hundredthsOfSeconds = ((RTC.GetData(0x00) >> 4) & 0x0F);

Leave a Reply

You must be logged in to post a comment.