# RTC DS3231 Help

I am new to this and I am really struggling with some conversions for the RTC chip I am trying to get my Arduino to talk to.

I have been trying to get an understanding of how the libraries actually talk to the program and in doing so I am trying to write a simple program to get the time from my RTC to output to the serial.

Here is what I have so far…

// My attempt to write an RTC sketch to show the time on an LCD screen

#include <Wire.h>
#include <LiquidCrystal.h>

int count = 0;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.println(" Lindsay’s Clock ");
lcd.setCursor(0,1);
delay(2000);
lcd.clear();
Wire.begin();
}

void loop(){
delay(1000);
Wire.write((uint8_t) 0x00);
Wire.endTransmission();

read_second = (((read_second & B11110000)>>4)*10 + (read_second & B00001111)); // convert BCD to decimal
read_minute = (((read_minute & B11110000)>>4)*10 + (read_minute & B00001111)); // convert BCD to decimal
read_hour = (((read_hour & B11110000)>>4)*10 + (read_hour & B00001111)); // convert BCD to decimal

Serial.print("Hour - “);
Serial.print(” Minute - “);
Serial.print(” Second - ");
}

The read_minute and the read_second output perfectly but the read_hours is outputting “70” at 10PM.

Can anyone help me and explain what I am doing wrong?

Thanks

scoopthepoop:
The read_minute and the read_second output perfectly but the read_hours is outputting "70" at 10PM.

Can anyone help me and explain what I am doing wrong?

The top 4 bits of the hour byte are:

Zero
12-hour mode flag
PM flag
Tens of hours

Since you get 0111 0000 at 10:00 that means the clock is in 12-hour mode, it's PM, and the hour is > 9 which is all correct.

``````if (read_hour & 0b01000000)  // 12-hour mode
{
if (read_hour & 0b00100000)  // PM flag
read_hour = (((read_hour & B00010000)>>4)*10 + (read_hour & B00001111)) + 12; // convert BCD to decimal and add 12 for PM
else
read_hour = (((read_hour & B00010000)>>4)*10 + (read_hour & B00001111)); // convert BCD to decimal
}
else
{  // 24-hour mode
read_hour = (((read_hour & B00110000)>>4)*10 + (read_hour & B00001111)); // convert BCD to decimal
}
``````

John, thanks for the reply. I had not but 10 minutes ago figured out that it was the PM AM, 12 hour 24 hour issue when I finally got ahold of this file...

http://datasheets.maxim-ic.com/en/ds/DS3231.pdf

You are EXACTLY right. I didn't take into account how the RTC dealt with the hours in 24 hour mode.

I do like your "if" statement so I am probably going to snag that.

Thanks again.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6189013,"math_prob":0.79726386,"size":1795,"snap":"2022-40-2023-06","text_gpt3_token_len":478,"char_repetition_ratio":0.15298715,"word_repetition_ratio":0.036437247,"special_character_ratio":0.3431755,"punctuation_ratio":0.21893491,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9798318,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-05T02:34:14Z\",\"WARC-Record-ID\":\"<urn:uuid:f1eb2ed5-c341-48dd-99b9-09f905ecf3e3>\",\"Content-Length\":\"27751\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:51d342ad-d75a-4a9e-bf2a-dd9bd4262d5d>\",\"WARC-Concurrent-To\":\"<urn:uuid:ce971f00-b350-4bdc-94b7-1bc8d8c11d18>\",\"WARC-IP-Address\":\"184.104.202.139\",\"WARC-Target-URI\":\"https://forum.arduino.cc/t/rtc-ds3231-help/91387\",\"WARC-Payload-Digest\":\"sha1:Q4YQF3CIVWFMNUPTPMEAONLOBJ2SZKYE\",\"WARC-Block-Digest\":\"sha1:EKKYS7DL4DPILB6M74IX4RVUMYGPENYS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337531.3_warc_CC-MAIN-20221005011205-20221005041205-00762.warc.gz\"}"}