Who knows how to set the operation of ds3231 module to 12 hour system?Use the function statement "Clock setClockMode(true);" Why is the ds3231 module still running on a 24-hour system?
Which of the myriad libraries for the DS3231 are you using?
Post the code that does not work.
i use the"Clock.setClockMode(true);"But,the ds3231 module is still running on a 24-hour system,why?
Please respond to what was asked eight days ago.
Which of the myriad libraries for the DS3231 are you using?
Post the code that does not work.
The code is shown below,use the string like"2205043085730x" to set it,but and the result is like the loop of "2022 5 4 3 8 57 20 24h over","24h" prompts me that something is wrong.
'''
/*
DS3231_set.pde
Eric Ayars
4/11
Test of set-time routines for a DS3231 RTC
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
bool Century=false;
bool h12=true;
bool PM;
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 = Temp110 + Temp2;
// now month
Temp1 = (byte)InString[2] -48;
Temp2 = (byte)InString[3] -48;
Month = Temp110 + Temp2;
// now date
Temp1 = (byte)InString[4] -48;
Temp2 = (byte)InString[5] -48;
Day = Temp110 + Temp2;
// now Day of Week
DoW = (byte)InString[6] - 48;
// now Hour
Temp1 = (byte)InString[7] -48;
Temp2 = (byte)InString[8] -48;
Hour = Temp110 + Temp2;
// now Minute
Temp1 = (byte)InString[9] -48;
Temp2 = (byte)InString[10] -48;
Minute = Temp110 + Temp2;
// now Second
Temp1 = (byte)InString[11] -48;
Temp2 = (byte)InString[12] -48;
Second = Temp110 + 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); // set to 12h,but why not work?
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 on both alarms, with external interrupt
Clock.turnOnAlarm(1);
Clock.turnOnAlarm(2);
}//以下为测试12小时制添加
Serial.print("2");
if (Century) {
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.println(h12);
Serial.print(Clock.getHour(h12, PM), DEC);
Serial.print(' ');
//Serial.println(h12);
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 ");
}
Serial.println("over.");
delay(1000);
}
'''
Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
You can go back and fix your post by highlighting the code and clicking the </> in the menu bar.
The code is shown below again,use the string like"2205043085730x" to set it,but the result is just like the loop of "2022 5 4 3 8 57 20 24h over 2022 5 4 3 8 57 21 24h over","24h" prompts me that something is wrong.
/*
DS3231_set.pde
Eric Ayars
4/11
Test of set-time routines for a DS3231 RTC
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
bool Century=false;
bool h12=true;
bool PM;
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); // set to 12h,but why not work?
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 on both alarms, with external interrupt
Clock.turnOnAlarm(1);
Clock.turnOnAlarm(2);
}//以下为测试12小时制添加
Serial.print("2");
if (Century) {
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.println(h12);
Serial.print(Clock.getHour(h12, PM), DEC);
Serial.print(' ');
//Serial.println(h12);
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 ");
}
Serial.println("over.");
delay(1000);
}
I can not confirm what you see. When I run your code, I can go between 12 and 24 hour mode with
Clock.setClockMode(true);
//Clock.setClockMode(false);
The print outs are correct for each mode For example:
21:31:03.081 -> 2022 5 4 3 9 0 21 24h over.
21:31:04.093 -> 2205043085730x
21:31:04.093 -> 2022 5 4 3 8 57 30 AM over.
I wonder if you have a defective module with a stuck register bit for the 12/24 hour mode. Run this code to see the registers, and look for bit 6 changing in register 0x02. From the data sheet
The DS3231 can be run in either
12-hour or 24-hour mode. Bit 6 of the hours register is
defined as the 12- or 24-hour mode select bit. When high,
the 12-hour mode is selected
21:31:37.655 -> 0X02 01001000 //12 hour mode bit 6 set = 1
21:28:48.734 -> 0X02 00001000 //24 hour mode bit 6 set = 0
//DS3231 Register Dump
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
#define startRegister 0x00
#define endRegister 0x12
void setup()
{
Wire.begin();
Serial.begin(9600);
delay(1000);
Serial.println();
Serial.print("Register");
Serial.print("\t");
Serial.println("Bit Values");
for (int a = startRegister; a <= endRegister; a++)
{
byte b=readNVRAM(a);
Serial.print("0X");
if(a<16)
Serial.print("0");
Serial.print(a, HEX);
Serial.print("\t");
Serial.print("\t");
for (int i = 7; i >= 0; i-- )
{
Serial.print((b >> i) & 0X01);
}
Serial.println();
}
Serial.println();
}
byte readNVRAM(byte location)//// reads data from DS1307 NVRAM location
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(location);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 1);//only 1 byte, does not need wire.available
return Wire.read();
}
void loop(){
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.