RobTillaart / DS3232

Arduino library for DS3232 RTC (minimalistic)
MIT License
2 stars 0 forks source link

weekDay is always 0 #2

Closed donsenilo1968 closed 6 months ago

donsenilo1968 commented 6 months ago

Hi

Even in your Example (with added weekDay) it always returns 0.

RobTillaart commented 6 months ago

Thanks for the issue. Might take some time before I can investigate.

RobTillaart commented 6 months ago

What platform do you use? What is the output of DS3232_demo_read.ino? Do you have pull up resistors? What is the wire length?

donsenilo1968 commented 6 months ago

Arduino Nano Date and Time No Max. 3cm

RobTillaart commented 6 months ago

So date and time works but not weekdays ....

Need a code / datasheet check as day of week is iirc the only read only register..

donsenilo1968 commented 6 months ago

Yep. Weekdays are always 0. This is the only Datasheet I could found.

RobTillaart commented 6 months ago

Datasheet from the manufacturer of the device:

RobTillaart commented 6 months ago

Registers: image

Register 3 is Day of week - 1..7

uint8_t DS3232::weekDay() { return _reg[3]; }

the register 3 is filled in read() and not masked or so. The bcd2dec() function that converts the BCD values from the registers is not needed for weekday as it does not change the value.

Can you add a print statement into the read function in ds3232.cpp (line 61) ? Please post what is read from the registers.

int DS3232::read()
{
  _wire->beginTransmission(_address);
  _wire->write(DS3232_SECONDS);
  _rv = _wire->endTransmission();
  if (_rv != 0) return DS3232_ERROR_I2C;

  if (Wire.requestFrom(_address, (uint8_t)7) != 7)
  {
    return DS3232_ERROR_I2C;
  }
  for (int i = 0; i < 7; i++)
  {
    uint8_t tmp = _wire->read();  //  <<  changed
    Serial.println(tmp, HEX);  //  <<  changed
    _reg[i] = bcd2dec(tmp);  //  <<  changed
  }
  _reg[0] &= 0x7F;
  _reg[2] &= 0x3F;
  _lastRead = millis();

  return DS3232_OK;
}
donsenilo1968 commented 6 months ago

I get this:

16031 24-4-5 11:40:2 1 4 40 11 1 5 4 24 18033 24-4-5 11:40:4 1 6 40 11 1 5 4 24

RobTillaart commented 6 months ago

Adding the registers / meaning

| register | meaning      | value |
|:--------:|:-------------|:-----:|
|    0     | seconds      |   4   |
|    1     | minutes      |  40   |
|    2     | hours        |  11   |
|    3     | dayofweek    |   1   |
|    4     | day of month |   5   |
|    5     | month        |   4   |
|    6     | year         |  24   |
18033 24-4-5 11:40:4

Datasheet states The day-of-week register increments at midnight. Values that correspond to the day of week are user defined but must be sequential (i.e., if 1 equals Sunday, then 2 equals Monday, and so on). Illogical time and date entries result in undefined operation.

The day of week is 1 where we would expect 5 or 6 (?) As the value is now 1, I assume the RTC started yesterday with a value 0 and it was updated last midnight.

That would mean I need to check the library if the DOW is set.

RobTillaart commented 6 months ago

That would mean I need to check the library if the DOW is set.

Has no setWeekDay() so ==> BUG

RobTillaart commented 6 months ago

@donsenilo1968 Created a develop branch that adds setWeekDay() (and some minor edits) If you have time, can you verify if it works?

update - valid values seem to be 1..7

donsenilo1968 commented 6 months ago

With the Develop Branch

Before demo_write: 212216 24-4-5 20:12:25 1 214219 24-4-5 20:12:27 1

After demo_write: 144150 24-4-5 20:15:50 5 146152 24-4-5 20:15:53 5

If I understand it correctly, once the Weekday is set it increments at midnight and starts again on Mondays with 1.

But (in my Case) I use Udp NTP Client to get the actual Date and Time and it seems there is no Way to get the Weekday.

RobTillaart commented 6 months ago

That is what I understand too. Please let me know the weekday tomorrow to be sure. Then I can merge the new code.

Thanks for testing!

donsenilo1968 commented 6 months ago

I found here an interesting Part about getting the Weekday. If this is true, the Output for the Weekday was correct (1 = Friday).

RobTillaart commented 6 months ago

I found here an interesting Part about getting the Weekday. If this is true, the Output for the Weekday was correct (1 = Friday).

Then it could automatically update it to 2 tomorrow.

Indeed an interesting find

RobTillaart commented 6 months ago

@donsenilo1968 Added a note about the Thursday == 0 weekday in the readme.md

donsenilo1968 commented 6 months ago

Hi Rob

It seems like it works.

20240407_011236_001

I read the Date and Time via NTP, add the Timezone and calculate the Weekday from the epoch. Then I write all to the Clock (Date and Time at the Bottom of the Display). This happens when the Arduino starts and every Monday. The Date, Time and Weekday at the Top are the Data getting from the Clock.

Thank you very much for your Help and updating the Code. Best Regards from Vienna, Markus

RobTillaart commented 6 months ago

Thanks for the photo, looks good!

The NTP time is the moment of the last sync I assume? Or is there another reason for the 5 minutes difference?

Are there other things missing in the library? (I am thinking about adding EEPROM support)

donsenilo1968 commented 6 months ago

The NTP time is the moment of the last sync I assume? Yes, exactly.

I use this Lib because it's minimalistic and I don't need any other Functions for my Project. Is adding more Functions necessary ? It's your Baby, so it's your Decision.

RobTillaart commented 6 months ago

My plan is to create a derived extended class DS3232-EXT Which has the additional functions. Exactly because that keeps the base class minimalistic.

Another option I consider is to have a 2nd class DS3232EE that only handles the EEPROM part of the device. Not derived, complete disjunct as there is no reason to have it merged in one object afaik. (except convenience) It could be in same library though

Other functionality like alarms etc would be logically more related to the time keeping. These should therefor be in the EXT class.

RobTillaart commented 6 months ago

Merged develop into master and released 0.4.0 version,