Trouble with DS3231 12/24hour formating

I am having trouble with getting the ds3231 to return time in the 12hour format. I am using arduino Uno in the IDE 1.6. I am using the Eric Ayars library. I started by using his example “ds3231 set” sketch. I changed this part of the code> Clock.setClockMode(false);< to this > Clock.setClockMode(true);< and from what I think I understand is this is supposed to set the clock mode in 12 hour format.

 /*
DS3231_set.pde
Eric Ayars
4/11

Test of set-time routines for a DS3231 RTC

*/

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;

byte Year;
byte Month;
byte Date;
byte DoW;
byte Hour;
byte Minute;
byte Second;

void GetDateStuff(byte& Year, byte& Month, byte& Day, byte& DoW, 
		byte& Hour, byte& Minute, byte& Second) {
	// Call this if you notice something coming in on 
	// the serial port. The stuff coming in should be in 
	// the order YYMMDDwHHMMSS, with an 'x' at the end.
	boolean GotString = false;
	char InChar;
	byte Temp1, Temp2;
	char InString[20];

	byte j=0;
	while (!GotString) {
		if (Serial.available()) {
			InChar = Serial.read();
			InString[j] = InChar;
			j += 1;
			if (InChar == 'x') {
				GotString = true;
			}
		}
	}
	Serial.println(InString);
	// Read Year first
	Temp1 = (byte)InString[0] -48;
	Temp2 = (byte)InString[1] -48;
	Year = Temp1*10 + Temp2;
	// now month
	Temp1 = (byte)InString[2] -48;
	Temp2 = (byte)InString[3] -48;
	Month = Temp1*10 + Temp2;
	// now date
	Temp1 = (byte)InString[4] -48;
	Temp2 = (byte)InString[5] -48;
	Day = Temp1*10 + Temp2;
	// now Day of Week
	DoW = (byte)InString[6] - 48;		
	// now Hour
	Temp1 = (byte)InString[7] -48;
	Temp2 = (byte)InString[8] -48;
	Hour = Temp1*10 + Temp2;
	// now Minute
	Temp1 = (byte)InString[9] -48;
	Temp2 = (byte)InString[10] -48;
	Minute = Temp1*10 + Temp2;
	// now Second
	Temp1 = (byte)InString[11] -48;
	Temp2 = (byte)InString[12] -48;
	Second = Temp1*10 + Temp2;
}

void setup() {
	// Start the serial port
	Serial.begin(9600);

	// Start the I2C interface
	Wire.begin();
}

void loop() {

	// If something is coming in on the serial line, it's
	// a time correction so set the clock accordingly.
	if (Serial.available()) {
		GetDateStuff(Year, Month, Date, DoW, Hour, Minute, Second);

		Clock.setClockMode(true);	
                //setClockMode(false);  // set to 24h
		//setClockMode(true);	// set to 12h

		Clock.setYear(Year);
		Clock.setMonth(Month);
		Clock.setDate(Date);
		Clock.setDoW(DoW);
		Clock.setHour(Hour);
		Clock.setMinute(Minute);
		Clock.setSecond(Second);

		// Test of alarm functions
		// set A1 to one minute past the time we just set the clock
		// on current day of week.
		//Clock.setA1Time(DoW, Hour, Minute+1, Second, 0x0, true, 
			//false, false);
		// set A2 to two minutes past, on current day of month.
		//Clock.setA2Time(Date, Hour, Minute+2, 0x0, false, false, 
			//false);
		// Turn off both alarms
		Clock.turnOffAlarm(1);
		Clock.turnOffAlarm(2);

	}
	delay(1000);
}

I formated the date and time in the serial monitor followed by the x. So yes I set the time which also allowed the code to jump into the if statement that contains the 12/24hr formater.

Then I used his next example DS3231_test.

/*
DS3231_test.pde
Eric Ayars
4/11

Test/demo of read routines for a DS3231 RTC.

Turn on the serial monitor after loading this to check if things are
working as they should.

*/

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

void setup() {
	// Start the I2C interface
	Wire.begin();
	// Start the serial interface
	Serial.begin(9600);
}

void loop() {
	// send what's going on to the serial monitor.
	// Start with the year
	Serial.print("2");
	if (Century) {			// Won't need this for 89 years.
		Serial.print("1");
	} else {
		Serial.print("0");
	}
	Serial.print(Clock.getYear(), DEC);
	Serial.print(' ');
	// then the month
	Serial.print(Clock.getMonth(Century), DEC);
	Serial.print(' ');
	// then the date
	Serial.print(Clock.getDate(), DEC);
	Serial.print(' ');
	// and the day of the week
	Serial.print(Clock.getDoW(), DEC);
	Serial.print(' ');
	// Finally the hour, minute, and second
	Serial.print(Clock.getHour(h12, PM), DEC);
	Serial.print(' ');
	Serial.print(Clock.getMinute(), DEC);
	Serial.print(' ');
	Serial.print(Clock.getSecond(), DEC);
	// Add AM/PM indicator
	if (h12) {
		if (PM) {
			Serial.print(" PM ");
		} else {
			Serial.print(" AM ");
		}
	} else {
		Serial.print(" 24h ");
	}
	// Display the temperature
	Serial.print("T=");
	Serial.print(Clock.getTemperature(), 2);
	// Tell whether the time is (likely to be) valid
	if (Clock.oscillatorCheck()) {
		Serial.print(" O+");
	} else {
		Serial.print(" O-");
	}
	// Indicate whether an alarm went off
	if (Clock.checkIfAlarm(1)) {
		Serial.print(" A1!");
	}
	if (Clock.checkIfAlarm(2)) {
		Serial.print(" A2!");
	}
	// New line on display
	Serial.print('\n');
	// Display Alarm 1 information
	Serial.print("Alarm 1: ");
	Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);
	Serial.print(ADay, DEC);
	if (ADy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(AHour, DEC);
	Serial.print(' ');
	Serial.print(AMinute, DEC);
	Serial.print(' ');
	Serial.print(ASecond, DEC);
	Serial.print(' ');
	if (A12h) {
		if (Apm) {
			Serial.print('pm ');
		} else {
			Serial.print('am ');
		}
	}
	if (Clock.checkAlarmEnabled(1)) {
		Serial.print("enabled");
	}
	Serial.print('\n');
	// Display Alarm 2 information
	Serial.print("Alarm 2: ");
	Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);
	Serial.print(ADay, DEC);
	if (ADy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(AHour, DEC);
	Serial.print(' ');
	Serial.print(AMinute, DEC);
	Serial.print(' ');
	if (A12h) {
		if (Apm) {
			Serial.print('pm');
		} else {
			Serial.print('am');
		}
	}
	if (Clock.checkAlarmEnabled(2)) {
		Serial.print("enabled");
	}
	/* display alarm bits
	Serial.print('\n');
	Serial.print('Alarm bits: ');
	Serial.print(ABits, DEC);
	*/

	Serial.print('\n');
	Serial.print('\n');
	delay(1000);
}

I get the following result. On the serial monitor.

2015 3 23 1 6 13 26 24h T=24.25 0+
Alarm 1: 7 DoW 2 0 11
Alarm 2: 12 DoW 4 4

The 24h tells me that the ds3231 is still in 24hr format.
The clock is very accurate and is counting but not in the 12 hour format.
What code do I need to modify to change this to 12hr format?

Any help would be appreciated. Thanks in advance.

If you look at the specs for the chip:

the timekeeping register (Figure 1), says it is bit 6 of register 0x02 sets the 12/24 format. When the bit is high, the clock is in 12 hour format; low selects 24 hour format. I'd take a look at the library code to see if it is setting the bit properly.

I am not familiar with how libraries are created. In the past I have just created my own code by looking at datasheets. I don't know what file I should look in for the formating of the 12/24h format.(I am having a hard time finding what part of the code sets this.) Do I look in the .cpp.swp, .h.swp, .cpp, or .h file. I have found parts of the code that access 0x02 and look up what bit 6 is already set to. (in the .cpp file) but I need to find the part that initially sets it.

Hello and welcome,

After line:

Serial.print(Clock.getHour(h12, PM), DEC);
Serial.print(' ');

Try to add

Serial.print( h12 );

What does it print?

I can't try this library right now, I suggest you try another one. This is the one I used in the past: GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks

I don't see how you set AM/PM in the code you posted. Twelve hour mode is a pain in the a$$, especially with alarms. I'm always forgetting to check the setting of AM/PM on the alarm hour.

And, do you really need it? You can always display hours>12 as "hours-12"

It is returning a 0 for the h12 value. Yeah. You are right with it being a pain in 12 format with alarms. I will do what you suggested and do the hours -12 thing because ultimatly I am going to time events from this. Im not sure how to set AM/PM in this library either. I was just trying to get to 12hour format but im not worried about doing that any more.

guix:
This is the one I used in the past: GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks

+1, this one is really well done. I'm using it now.

aarg: Agree...Jack does good work.

I've been really digging at this specific topic for a few days now and it does not seem to be a thoroughly discussed or SOLVED topic. Many different examples and quite a few different libraries, none of which give us the option to change the mode to that which most of were raised on.

if (now.hour() > 12 )
{
hourNow = now.hour() - 12;
}
else
{
hourNow = now.hour();
}

I found the above snippet of code in a much older thread, but had no explanation how to incorporate it into my code. "hourNow" was also predefined as a BYTE, but I removed that line when trying to troubleshoot. Basically, because hourNow was not "defined in this instance", I tried to make a global variable to define it as zero, but that didn't work for me.

Can someone create an "if statement" for me with a usable variable and show me where each snippet should be pasted? I pasted the above snippet right at the beginning of the loop, just before the serial.print commands, but I can't get past the compile errors.

myggle:

if (now.hour() > 12 )

{
hourNow = now.hour() - 12;
}
else
{
hourNow = now.hour();
}

This absolutely helped me not only get 12 hour display, but also allowed me to add AM and PM I am glad I found this.

Clock.setClockMode(false); // set to 24h
Clock.setClockMode(true); // set to 12h

Look at this call during setting up your time. If your parameter is true, later you can't swith to 24h.

I learnt this after seeing your question. Thanks

@shreekant1, it's pointless and distracting to dig up 5 years old threads, the original posters have either disappeared, given up on their project, or solved their problem by now. That is why resurrecting dead threads is strongly discouraged by the forum moderators.

The DateTime library includes ample code for 12/24 hour time, using absolutely any time source, which means absolutely any RTC or any RTC library.

If you're using native C functions, you would normally just use Epoch times and ask strftime() to express it any way you want, including 12 hour times.

The code @myggle posted is incorrect:

if (now.hour() > 12 )
{
hourNow = now.hour() - 12;
}
else
{
hourNow = now.hour();
}

After midnight is not "0" o'clock in the 12 hour system. It's "12". Here is the correct code from the DateTime library:

int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
  refreshCache(t);
  if( tm.Hour == 0 )
    return 12; // 12 midnight
  else if( tm.Hour  > 12)
    return tm.Hour - 12 ;
  else
    return tm.Hour ;
}