Writing TIME in RTC Module

Our range of tiny modules are designed to make it easy for you to quickly add functionality to your projects. [Module List]
andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Writing TIME in RTC Module

Post by andrew » Tue May 20, 2014 12:23 am

Well done.
The book will cover your LCD needs.

Chris
Posts: 17
Joined: Sat Aug 30, 2014 5:37 am
Location: Adelaide, South Australia

Re: Writing TIME in RTC Module

Post by Chris » Sat Aug 30, 2014 5:57 am

Hi, sorry to hijack this post again, didnt seem worth starting another. Im also having trouble uploading any time to the module. I have it wired directly board to board and checked theyre the right way around. The test through the serial monitor works as it should and thats as far as I get. I type the test time TIME 14:57:00, enter ~ nothing.
One thing Ive noticed is that if I have a simple "hello world" running and open another, it compiles then a led flashes on the Eleven and the new one flashes on the screen. If I have "hello world" running and upload the time, the led flashes and hello world freezes on the screen.
Theres no error as such because nothing happens :x

Chris

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Writing TIME in RTC Module

Post by andrew » Sat Aug 30, 2014 7:22 am

Chris wrote: Theres no error as such because nothing happens :x

Chris
Please visit http://www.freetronics.com/pages/rtc-re ... tart-guide
scroll most of the way down until you see the link for a sketch called
I2Cds3232.ino

If you upload this sketch and open the serial monitor, what happens?

Chris
Posts: 17
Joined: Sat Aug 30, 2014 5:37 am
Location: Adelaide, South Australia

Re: Writing TIME in RTC Module

Post by Chris » Sat Aug 30, 2014 9:08 am

Thanks for the reply. It scrolls along the top continuously not like the example even with the carriage return checked, but anyway it would read

Day of week:3:40:18 1/1/0
Day of week:3:40:19 1/1/0
Day of week:3:40:20 1/1/0
etc.

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Writing TIME in RTC Module

Post by andrew » Sun Aug 31, 2014 10:29 pm

Chris wrote:Thanks for the reply. It scrolls along the top continuously not like the example even with the carriage return checked, but anyway it would read

Day of week:3:40:18 1/1/0
Day of week:3:40:19 1/1/0
Day of week:3:40:20 1/1/0
etc.
In void loop(), if you uncomment the setDS3232time line, change the date/time data to something current, upload the sketch, recomment the line, upload the sketch again - does it work?

If not, let's look at your wiring. Please describe how it's connected to the board. If possible, can you attach a photo? Or email it to support@freetronics.com and we'll have a look.

Chris
Posts: 17
Joined: Sat Aug 30, 2014 5:37 am
Location: Adelaide, South Australia

Re: Writing TIME in RTC Module

Post by Chris » Mon Sep 01, 2014 9:58 am

Thanks John. Its actually displaying something different today

2:46:43 2/1/0 Day of week:Monday
2:46:44 2/1/0 Day of week:Monday
2:46:45 2/1/0 Day of week:Monday
2:46:46 2/1/0 Day of week:Monday

The only void loop() I can find is near the end, I can uncomment the setDS3232time line, but I cant figure out where to change the time

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Writing TIME in RTC Module

Post by andrew » Mon Sep 01, 2014 10:12 am

Chris wrote:Thanks John. Its actually displaying something different today

2:46:43 2/1/0 Day of week:Monday
2:46:44 2/1/0 Day of week:Monday
2:46:45 2/1/0 Day of week:Monday
2:46:46 2/1/0 Day of week:Monday

The only void loop() I can find is near the end, I can uncomment the setDS3232time line, but I cant figure out where to change the time
Change the values in the setDS3232 time function - see the comment afterwards for the values. For example to set 12:34pm Monday September 1, 2014 to the DS3232, use
setDS3232(00, 34, 12, 2, 1, 9, 14);
Make sure to put the comment "//" back in front of the function and upload the sketch again, otherwise it will keep resetting the time.

Chris
Posts: 17
Joined: Sat Aug 30, 2014 5:37 am
Location: Adelaide, South Australia

Re: Writing TIME in RTC Module

Post by Chris » Mon Sep 01, 2014 10:53 am

Again thanks John for the quick response. Works great in the Serial Monitor. Is it supposed to be showing up the same on the display or have we not got that far yet. If I have have another example running, like hello world, and I upload this sketch, it just freezes the timer under hello world.

Chris

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Writing TIME in RTC Module

Post by andrew » Mon Sep 01, 2014 10:06 pm

Chris wrote:Again thanks John for the quick response. Works great in the Serial Monitor. Is it supposed to be showing up the same on the display or have we not got that far yet. If I have have another example running, like hello world, and I upload this sketch, it just freezes the timer under hello world.

Chris
What display are you talking about?
The sketch only displays the time to the serial monitor.

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Writing TIME in RTC Module

Post by andrew » Tue Sep 02, 2014 9:38 pm

The following is a sketch to display the time and date using our LCD Keypad Shield and DS3232 real-time clock module:
http://www.freetronics.com/collections/ ... pad-shield
http://www.freetronics.com/collections/ ... rtc-module

It is also project #58 in my book "Arduino Workshop".
http://www.freetronics.com/collections/ ... o-workshop

If anyone is new to Arduino they could do a lot worse than by working through a copy of this book. It will give you the foundation knowledge of how an Arduino works, programming, electronics, and how to deal with various devices, data buses and so on that are used with popular hardware types.

Code: Select all

// Project 58 - Creating a Simple Digital Clock
#include "Wire.h"
#define DS3232_I2C_ADDRESS 0x68
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  lcd.begin(16, 2);
  // set the initial time here:
  // DS3232 seconds, minutes, hours, day, date, month, year
  //setDS3232time(0, 27, 0, 5, 15, 11, 12);
}
void setDS3232time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3232
  Wire.beginTransmission(DS3232_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3232time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3232_I2C_ADDRESS);
  Wire.write(0); // set DS3232 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3232_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3232 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3232
  readDS3232time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send the data to the LCD shield
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print(hour, DEC);
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second<10)
  {
    lcd.print("0");
  }
  lcd.print(second, DEC);
  lcd.setCursor(0,1);
  switch(dayOfWeek){
  case 1:
    lcd.print("Sun");
    break;
  case 2:
    lcd.print("Mon");
    break;
  case 3:
    lcd.print("Tue");
    break;
  case 4:
    lcd.print("Wed");
    break;
  case 5:
    lcd.print("Thu");
    break;
  case 6:
    lcd.print("Fri");
    break;
  case 7:
    lcd.print("Sat");
    break;
  }
  lcd.print(" ");
  lcd.print(dayOfMonth, DEC);
  lcd.print("/");
  lcd.print(month, DEC);
  lcd.print("/");
  lcd.print(year, DEC);
}
void loop()
{
  displayTime(); // display the real-time clock time on the LCD,
  delay(1000); // every second
}


Post Reply