[SOLVED] Problem setting day on PCF8523 RTC

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

[SOLVED] Problem setting day on PCF8523 RTC

Post by jpj » Sat Dec 17, 2016 3:32 pm

I have the Adafruit HUZZAH esp8266 with Adalogger attached:

https://learn.adafruit.com/adafruit-fea ... ah-esp8266
https://learn.adafruit.com/adafruit-ada ... g/overview

Adafruit has the uRTC code here:
https://github.com/adafruit/Adafruit-uRTC

When I set the rtc datetime with a two digit day, i.e. 2016-12-17, day displays as 7 when recalled. I think this is a bug but would appreciate some assistance identifying the issue before I try to modify the uRTC code.

When I set the day to a single digit, i.e. 2016-12-3, reading datetime from the rtc appears correct. Here is a screenlog from my REPL session, showing micropython version. I have added some white space to enhance readability.

Thanks,
J

Code: Select all

WebREPL daemon started on ws://192.168.4.1:8266
WebREPL daemon started on ws://0.0.0.0:8266
Started webrepl in normal mode
connecting to network...
network config: ('192.168.1.113', '255.255.255.0', '192.168.1.1', '192.168.1.1')

MicroPython v1.8.6-150-gd9c8397 on 2016-12-12; ESP module with ESP8266
Type "help()" for more information.

>>> import urtc
>>> from machine import I2C, Pin
>>> i2c = I2C(scl=Pin(5), sda=Pin(4))
>>> rtc = urtc.PCF8523(i2c)

>>> datetime = urtc.datetime_tuple(year=2016, month=12, day=17, weekday=6, hour=14, minute=4, second=0, millisecond=0)
>>> rtc.datetime(datetime)

>>> rtc.datetime()
DateTimeTuple(year=2016, month=12, day=7, weekday=6, hour=14, minute=4, second=10, millisecond=0)
>>> rtc.datetime()
DateTimeTuple(year=2016, month=12, day=7, weekday=6, hour=14, minute=4, second=17, millisecond=0)

>>> datetime = urtc.datetime_tuple(year=2016, month=12, day=3, weekday=6, hour=14, minute=4, second=0, millisecond=0)
>>> rtc.datetime(datetime)
>>> rtc.datetime()
DateTimeTuple(year=2016, month=12, day=3, weekday=6, hour=14, minute=4, second=15, millisecond=0)

>>> rtc.datetime()
DateTimeTuple(year=2016, month=12, day=3, weekday=6, hour=14, minute=5, second=13, millisecond=0)
Last edited by jpj on Sun Dec 18, 2016 12:57 pm, edited 1 time in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Problem setting day on PCF8523 RTC

Post by deshipu » Sat Dec 17, 2016 6:48 pm

This indeed seems like a bug. I will try to get that fixed over the weekend. Thanks for reporting.

By the way, in the future, you might get a faster response if you just report a bug against that library at Github -- I don't check this forum every day anymore.

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Problem setting day on PCF8523 RTC

Post by jpj » Sat Dec 17, 2016 6:54 pm

deshipu wrote:This indeed seems like a bug. I will try to get that fixed over the weekend. Thanks for reporting.

By the way, in the future, you might get a faster response if you just report a bug against that library at Github -- I don't check this forum every day anymore.
Excellent, thanks! I'll definitely report any future issues on GitHub as you recommend.
Last edited by jpj on Thu Dec 22, 2016 3:27 pm, edited 1 time in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Problem setting day on PCF8523 RTC

Post by deshipu » Sat Dec 17, 2016 11:45 pm

I see what the problem is. The PCF8523 chip has the weekday and day swapped compared to the DS3231 and DS1307 chips. I didn't notice that when I was unifying the code for them, hence the bug. I will make a correction as soon as possible.

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Problem setting day on PCF8523 RTC

Post by jpj » Sat Dec 17, 2016 11:50 pm

deshipu wrote:I see what the problem is. The PCF8523 chip has the weekday and day swapped compared to the DS3231 and DS1307 chips. I didn't notice that when I was unifying the code for them, hence the bug. I will make a correction as soon as possible.
Thank you!

I've been writing a data logging program today and having the correct timestamp in the logfile will great.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Problem setting day on PCF8523 RTC

Post by deshipu » Sat Dec 17, 2016 11:53 pm

I now fixed the problem and bumped the version to 1.1, please try it now.

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Problem setting day on PCF8523 RTC

Post by jpj » Sun Dec 18, 2016 12:39 am

I'm testing, thanks.

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Problem setting day on PCF8523 RTC

Post by jpj » Sun Dec 18, 2016 1:01 am

Not working. I double checked that I had your revised urtc.py in place. The day and weekday appear to get assigned in reverse order.

Code: Select all

>>> import urtc
>>> from machine import I2C, Pin
>>> i2c = I2C(scl=Pin(5), sda=Pin(4))
>>> rtc = urtc.PCF8523(i2c)
>>> datetime = urtc.datetime_tuple(year=2016, month=12, day=18, weekday=7, hour=0, minute=52, second=0, millisecond=0)
>>> rtc.datetime(datetime)
>>> rtc.datetime()
DateTimeTuple(year=2016, month=12, day=7, weekday=18, hour=0, minute=52, second=6, millisecond=0)

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Problem setting day on PCF8523 RTC

Post by jpj » Sun Dec 18, 2016 2:23 am

I removed my Adalogger, put the HUZZA on a breadboard and connected a DS1307 RTC. Using your new version 1.1 urtc.py it functions properly.

Code: Select all

>>> datetime = urtc.datetime_tuple(year=2016, month=12, day=18, weekday=0, hour=02, minute=30, second=02, millisecond=0)
>>> rtc.datetime(datetime)
>>> rtc.datetime()
DateTimeTuple(year=2016, month=12, day=18, weekday=0, hour=2, minute=30, second=6, millisecond=0)

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Problem setting day on PCF8523 RTC

Post by jpj » Sun Dec 18, 2016 3:11 am

I modified your version 1.1 urtc.py locally and have it working with the PCF8523 on my Adalogger. I don't know if this will work for other RTCs, but here is my change shown with line numbers:

Your version 1.1 urtc.py:

Code: Select all

     61             return datetime_tuple(
     62                 year=_bcd2bin(buffer[6]) + 2000,
     63                 month=_bcd2bin(buffer[5]),
     64                 day=_bcd2bin(buffer[4]),
     65                 weekday=_bcd2bin(buffer[3]),
     66                 hour=_bcd2bin(buffer[2]),
     67                 minute=_bcd2bin(buffer[1]),
     68                 second=_bcd2bin(buffer[0]),
     69             )
     
my local change

Code: Select all

     61             return datetime_tuple(
     62                 year=_bcd2bin(buffer[6]) + 2000,
     63                 month=_bcd2bin(buffer[5]),
     64                 weekday=_bcd2bin(buffer[4]),
     65                 day=_bcd2bin(buffer[3]),
     66                 hour=_bcd2bin(buffer[2]),
     67                 minute=_bcd2bin(buffer[1]),
     68                 second=_bcd2bin(buffer[0]),
     69             )
     

Post Reply