Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTC datetime doesn't change #27

Closed
TinManAkshay opened this issue Oct 19, 2022 · 6 comments
Closed

RTC datetime doesn't change #27

TinManAkshay opened this issue Oct 19, 2022 · 6 comments

Comments

@TinManAkshay
Copy link

TinManAkshay commented Oct 19, 2022

This is my code:

image

Output:

image

Once I have set the time and then try to print the time, time doesn't change. It will just print the fixed value. Its not iterating through the time. I'd appreciate if anybody could suggest the fix or point out some issues with the system.

Thanks,
Akshay

@FoamyGuy
Copy link
Contributor

@TinManAkshay can you post your full code as text? The code in the screenshot doesn't contain a main loop. But the output
in the screenshot shows it outputting the time several times. It seems that portion of the code is not visible in the screenshot. Can we see the code that is in the main loop and the rest of the file.

@TinManAkshay
Copy link
Author

TinManAkshay commented Oct 20, 2022

Hi @FoamyGuy -

Here is the entire code:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple demo of reading and writing the time for the PCF8523 real-time clock.
# Change the if False to if True below to set the time, otherwise it will just
# print the current date and time every second.  Notice also comments to adjust
# for working with hardware vs. software I2C.

import time
import board
from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_pcf8523
import busio

DEVICE_ADDRESS = 0x68
DEVICE_REGISTER = 0x03

WRITEBYTE = 0x80

#0x01 - 00
#0x02 - 00
#0x03 - 95 //10111111
#0x04 - seconds/miutes
#0x05 - hours
#0x06 - date
#0x07 - weekday
#0x08 - months
#0x09 - years
comm_port = busio.I2C(board.SCL, board.SDA)
device = I2CDevice(comm_port, DEVICE_ADDRESS)

i2c = board.I2C()
rtc = adafruit_pcf8523.PCF8523(i2c)

# Lookup table for names of days (nicer printing).
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")


# pylint: disable-msg=using-constant-test
if True:  # change to True if you want to set the time!
    #                     year, mon, date, hour, min, sec, wday, yday, isdst
    t = time.struct_time((2022, 10, 19, 11, 59, 10, 2, -1, -1))
    # you must set year, mon, date, hour, min, sec and weekday
    # yearday is not supported, isdst can be set but we don't do anything with it at this time
    print("Setting time to:", t)  # uncomment for debugging
    rtc.datetime = t
    print()
# pylint: enable-msg=using-constant-test

if False:
    
    with device as bus_device:
        bus_device.write(bytes([DEVICE_REGISTER]))
        #bus_device.write(bytes(WRITEBYTE))
        result = bytearray(1)
        bus_device.readinto(result)
        print("".join("{:02x}".format(x) for x in result))
        #bus_device.write(bytes(DEVICE_REGISTER))
        #result2 = bytearray(1)
        #bus_device.write_then_readinto(result2, (WRITEBYTE))
        #print("".join("{:02x}".format(x) for x in result2))
    
# Main loop:
while True:
    t = rtc.datetime
    print(t)     # uncomment for debugging
    print(
        "The date is {} {}/{}/{}".format(
            days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year
        )
    )
    print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
    time.sleep(1)  # wait a second
    #sessionname = time.strftime("%Y%b%d-%H%M%S%Z").upper()
    #print(sessionname)

@caternuson
Copy link

What PCF8523 RTC breakout is being used?

Why the attempts to read/set the Oscillator STOP flag?

@TinManAkshay
Copy link
Author

I have my own custom PCB which has PCF8523 integrated and I thought of using these libraries to get the RTC up and running. One thing I am thinking on top of my head that we haven't connected any external crystal oscillator to PCF8523. Recently I observed that this breakout board uses external crystal https://learn.adafruit.com/adafruit-ds3231-precision-rtc-breakout/

Do you see think not having crystal oscillator might be the reason why I cannot iterate through the accurate time?

Regarding STOP flag, I kept the set flag to True by mistake. This is how it is once I set the time, turns the flag to False and prints the time, its the same behavior/output as shown in the above pic.

Thanks,
Akshay

@caternuson
Copy link

You'll want to add an external crystal for the oscillator to function. There are some PCF8523 designs here that can be used as reference:
https://learn.adafruit.com/adafruit-pcf8523-real-time-clock/downloads

Closing since not a library issue.

@TinManAkshay
Copy link
Author

TinManAkshay commented Oct 21, 2022

Hi -

1st method:

I have added dtoverlay=i2c_rtc,pcf8523 in the boot/config.txt
i2cdetect -y 1 shows UU instead 0x68.

I soldered the crystal oscillator and rtc chip started keeping track of time after setting the time, let's say 1pm Although, when I shutdown the system for 10 mins approx., rtc chip should give me 1:10pm, but its giving me 1:01pm time. There is a battery coin cell as well. It seems like rtc loses 10mins time after complete shutdown of the system and when I turn on the system(via main power supply) the time would start from where it was left before like 1:01pm.

I dont know how to fix this issue. Please suggest.

2nd method:
When I comment out dtoverlay and it obviousl shows me 0x68. In this case, I am not losing time even after I turn on the system after 10 mins. Seems like library is writing 0b000 to enable battery switch over. That's exactly what we want. I do not know how to write to control register3 while device is in reserved address mode (0xUU). Any suggestions would be appreciated.

Thanks,
Akshay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants