DS3231 setting RTC accuracy improved

pull/7/head
Peter Hinch 2016-01-06 10:45:20 +00:00
rodzic eea1614115
commit 5371d0c213
1 zmienionych plików z 12 dodań i 8 usunięć

Wyświetl plik

@ -21,7 +21,7 @@ def now(): # Return the current time from the RTC in secs and millisecs from ye
# Driver for DS3231 accurate RTC module (+- 1 min/yr) needs adapting for Pyboard
# source https://github.com/scudderfish/uDS3231
def bcd2dec(bcd):
return (((bcd & 0b11110000)>>4)*10 + (bcd & 0b00001111))
return (((bcd & 0xf0) >> 4) * 10 + (bcd & 0x0f))
def dec2bcd(dec):
t = dec // 10
@ -42,7 +42,10 @@ class DS3231:
raise DS3231Exception("DS3231 not found on I2C bus at %d" % DS3231_I2C_ADDR)
def get_time(self, set_rtc = False):
data = self.ds3231.mem_read(7, DS3231_I2C_ADDR, 0)
if set_rtc:
data = self.await_transition() # For accuracy set RTC immediately after a seconds transition
else:
data = self.ds3231.mem_read(7, DS3231_I2C_ADDR, 0) # don't wait
ss = bcd2dec(data[0])
mm = bcd2dec(data[1])
if data[2] & 0x40:
@ -84,7 +87,8 @@ class DS3231:
data = self.ds3231.mem_read(7, DS3231_I2C_ADDR, 0)
ss = data[0]
while ss == data[0]:
data = self.ds3231.mem_read(7, DS3231_I2C_ADDR, 0) # Aawait a seconds transition
data = self.ds3231.mem_read(7, DS3231_I2C_ADDR, 0) # Await a seconds transition
return data
# Get calibration factor for Pyboard RTC. Note that the DS3231 doesn't have millisecond resolution so we
# wait for a seconds transition to emulate it.