HCRTC - Arduino library for use with DS1307 based RTC module

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 726
Joined: Sun Aug 05, 2012 4:02 pm

HCRTC - Arduino library for use with DS1307 based RTC module

Post by admin » Sun May 05, 2013 12:07 pm

Image
Real Time Clock module HCMODU0011


Description:

This Arduino library has been written to allow easy use DS1307 based real time clock modules such as our RTC module HCMODU0011. The library also contains functions to read and write to EEPROM memory which is also included in this RTC module. Communication with the module is via the standard Arduino I2C interface on pins A4 (SDA) & A5 (SCL) for an Uno, or 20 (SDA) & 21 (SCL) for a Mega.

You will need to download (please log in to download the library) and unzip this library to the Aduino development environments library area

On Windows:
My Documents\Arduino\libraries\
My Documents\Arduino\libraries\
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/
Documents/Arduino/libraries/
Documents/Arduino/libraries/
or similarly for Linux.


To use the library just include the Wire.h and HCRTC.h libraries and create an instance of the HCRTC library. E.g:
HCRTC HCRTC;
You can set the date and time with the following library function:
HCRTC.RTCWrite(I2C address, Year, Month, Day, Hour, Minuite, Second, Day of week);
And read the current date and time with the following library function:
HCRTC.RTCRead(I2C address);
Once the data has been read from the RTC module it can be accessed with the following library functions which return one byte of data of type byte:
HCRTC.GetDay()
HCRTC.GetMonth()
HCRTC.GetYear()
HCRTC.GetHour()
HCRTC.GetMinute()
HCRTC.GetSecond()
HCRTC.GetWeekday()
Or using the following string functions which return the time and date as a formatted string:
HCRTC.GetTimeString()
HCRTC.GetDateString()
To write data (of type byte) to the modules EEPROM memory you can use the following sequence of library functions:
HCRTC.EEStartWrite(EEPROM I2C address, EEPROM memory start location);

HCRTC.EEWriteByte(Data byte 0);
HCRTC.EEWriteByte(Data byte 1);
.
.
HCRTC.EEWriteByte(Data byte n);

HCRTC.EEEndWrite();
Please note that when writing to the EEPROM, the data is temporarily stored in the EEPROM's cache memory until the HCRTC.EEEndWrite() function is executed. The maximum amount of data that this cache can hold in one write sequence is 64 bytes. You should therefore not attempt to write more than this ammount in one write sequence.


To read data (of type byte) from the modules EEPROM memory you can use the following read sequence:
HCRTC.EEStartRead(EEPROM I2C address, EEPROM memory start location);

Data_1 = HCRTC.EEReadByte(Data byte 0);
Data_2 = HCRTC.EEReadByte(Data byte 1);
.
.
Data_n = HCRTC.EEReadByte(Data byte n);


Example Code:

Code: Select all

/* FILE     HCRTC_Example.cpp
   DATE:    23/12/13
   VERSION: 0.2
   AUTHOR:  Andrew Davies

This is an example of how to use the Hobby Components RTC library to read and
write to and from the DS1307 real time clock 24C32 EEPROM. The library is intended
to be used with our RTC clock module (HCMODU0011), but should work fine with any 
module that uses a DS1307 device.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Include the wire library */
#include "Wire.h"
/* Include the Hobby Components RTC library */
#include <HCRTC.h>

/* Define the I2C addresses for the RTC and EEPROM */
#define I2CDS1307Add 0x68
#define I2C24C32Add  0x50

/* Example string to write to EEPROM */
char ExampleString[] = "Hobby Components";

/* Used in example as an index pointer */
byte index;

/* Create an instance of HCRTC library */
HCRTC HCRTC;


void setup()
{
  Serial.begin(9600);
  
  /* Use the RTCWrite library function to set the time and date. 
     Parameters are: I2C address, year, month, date, hour, minute, second, 
     day of week */
  HCRTC.RTCWrite(I2CDS1307Add, 13, 4, 24, 14, 21, 0, 3);
}

void loop()
{
  /************* Example write to EEPROM ***************/  
  
  /* Initiate a write sequence. 
     Parameters are: I2C address, EEPROM start location */
  HCRTC.EEStartWrite(I2C24C32Add, 32000);
  
  for (index = 0; index < sizeof(ExampleString); index++)
  {  
    /* Sequentially write example data to the EEPROM */
    HCRTC.EEWriteByte(ExampleString[index]);
  }
  /* End the write sequence */
  HCRTC.EEEndWrite();
  
  /* Wait for cached data to finish writing */
  delay(10);  
  
  
  /************* Example read from EEPROM ***************/  
  
  /* Initiate read sequence.
     Parameters are: I2C address, EEPROM start location */
  HCRTC.EEStartRead(I2C24C32Add, 32000);
  

  for (index = 0; index < sizeof(ExampleString); index++)
  {
    /* Sequentially read data from EEPROM and output it to the UART */
    Serial.write(HCRTC.EEReadByte(I2C24C32Add));
  }    
  Serial.println();
    
 
  /* Continuously read the current time and date from the RTC */ 
  while(true)
  {
    /* Read the current time from the RTC module */
    HCRTC.RTCRead(I2CDS1307Add);
   
    /* Output the information to the UART */
    Serial.print(HCRTC.GetDay());
    Serial.print("/");
    Serial.print(HCRTC.GetMonth());
    Serial.print("/");
    Serial.print(HCRTC.GetYear());
    Serial.print(" ");
   
    Serial.print(HCRTC.GetHour());
    Serial.print(":");
    Serial.print(HCRTC.GetMinute());
    Serial.print(":");
    Serial.print(HCRTC.GetSecond());
    Serial.print(" DOW:");
    Serial.println(HCRTC.GetWeekday());
    
    /* Now output the same thing but using string functions instead: */
    Serial.print(HCRTC.GetDateString());
    Serial.print(" ");
    Serial.println(HCRTC.GetTimeString());
    
    /* Wait a second before reading again */
    delay(1000);  
  }
}
Library:
HCRTC.zip


Diagrams, libraries, and example code are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

DAJ
Posts: 5
Joined: Tue Jun 05, 2018 4:37 pm

Re: HCRTC - Arduino library for use with DS1307 based RTC mo

Post by DAJ » Wed Jul 25, 2018 8:53 pm

admin wrote:
HCRTC.zip
The header file in the library HCRTC.h explicitly includes "wire.h".

Note that the standard Arduino file is named "Wire.h" with an upper-case 'W'. There is not "wire.h".

If you're building on Windows that won't matter at all because the filesystems on Windows are not case-sensitive. If you're building on Linux (or, I think, MacOS) filenames are case-sensitive, the compiler won't find the file, and the build will fail.

admin
Site Admin
Posts: 726
Joined: Sun Aug 05, 2012 4:02 pm

Re: HCRTC - Arduino library for use with DS1307 based RTC mo

Post by admin » Thu Jul 26, 2018 8:29 am

Thanks, I've made the correction and attached the new version to the first post.

ftlpilot
Posts: 1
Joined: Mon Nov 12, 2018 4:14 am

Re: HCRTC - Arduino library for use with DS1307 based RTC module

Post by ftlpilot » Mon Nov 12, 2018 4:25 am

HCRTC.zip link has a virus/is unavailable - NOT VERY IMPRESSIVE :-(

andrew
Site Admin
Posts: 804
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCRTC - Arduino library for use with DS1307 based RTC module

Post by andrew » Mon Nov 12, 2018 9:37 am

The library is just a zip file containing plain text files. THERE IS NO EXECUTABLE CODE contained within the library to infect - or for it to infect your system. Just to be sure I've just downloaded a copy, checked the contents is correct, then scanned it with windows defender and it's not showing any infections. To be doubly sure I've also uploaded it to an online virus scanner (virus total) which scans it though multiple anti-virus engines and that is also showing the files as completely clean.

It is very likely that your virus scanner is is showing a false positive.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply