from microbit import *
DS3231_ADDR = 0x68
DS3231_REG_SEC = b'\x00'
DS3231_REG_MIN = b'\x01'
DS3231_REG_HOUR = b'\x02'
DS3231_REG_DAY = b'\x04'
DS3231_REG_MONTH = b'\x05'
DS3231_REG_YEAR = b'\x06'
DS3231_REG_TEMP = b'\x11'
class DS3231(object):
def __init__(self):
pass
def DATE(self, dat=[]):
if dat==[]:
t = []
t.append(self.year())
t.append(self.month())
t.append(self.day())
return t
else:
self.year(dat[0])
self.month(dat[1])
self.day(dat[2])
def TIME(self, dat=[]):
if dat==[]:
t = []
t.append(self.hour())
t.append(self.min())
t.append(self.sec())
return t
else:
self.hour(dat[0])
self.min(dat[1])
self.sec(dat[2])
def DateTime(self, dat=[]):
if dat==[]:
return self.DATE() + self.TIME()
else:
self.year(dat[0])
self.month(dat[1])
self.day(dat[2])
self.hour(dat[3])
self.min(dat[4])
self.sec(dat[5])
def dec2hex(self, dat):
return (int(dat/10)<<4) + (dat%10)
def setREG(self, dat,reg):
buf = bytearray(2)
buf[0] = reg[0]
buf[1] = dat
i2c.write(DS3231_ADDR,buf, repeat=False)
def getREG_DEC(self,reg):
i2c.write(DS3231_ADDR,reg)
t = i2c.read(DS3231_ADDR,1)[0]
#print("--===",t,"----------")
return (t>>4)*10 + (t%16)
def sec(self, sec=''):
if sec == '':
return self.getREG_DEC(DS3231_REG_SEC)
else:
self.setREG(self.dec2hex(sec), DS3231_REG_SEC)
def min(self, min=''):
if min == '':
return self.getREG_DEC(DS3231_REG_MIN)
else:
self.setREG(self.dec2hex(min), DS3231_REG_MIN)
def hour(self, hour=''):
if hour=='':
return self.getREG_DEC(DS3231_REG_HOUR)
else:
self.setREG(self.dec2hex(hour), DS3231_REG_HOUR)
def day(self, day=''):
if day=='':
return self.getREG_DEC(DS3231_REG_DAY)
else:
self.setREG(self.dec2hex(day), DS3231_REG_DAY)
def month(self, month=''):
if month=='':
return self.getREG_DEC(DS3231_REG_MONTH)
else:
self.setREG(self.dec2hex(month), DS3231_REG_MONTH)
def year(self, year=''):
if year=='':
return self.getREG_DEC(DS3231_REG_YEAR)
else:
self.setREG(self.dec2hex(year), DS3231_REG_YEAR)
def TEMP(self):
i2c.write(DS3231_ADDR,DS3231_REG_TEMP, repeat=False)
t1 = i2c.read(DS3231_ADDR,1, repeat=False)[0]
i2c.write(DS3231_ADDR,b'\x12', repeat=False)
t2 = i2c.read(DS3231_ADDR,1, repeat=False)[0]
if t1>0x7F:
return t1 - t2/256 -256
else:
return t1 + t2/256
ds=DS3231()
ds.DATE([17,10,24])
ds.TIME([10,03,00])
while True:
Time=ds.TIME()
Time_s=''
for i in Time:
Time_s+=str(i)+':'
display.scroll(Time_s[:-1]+"-")
#print(Time)
#print(ds.TEMP())
sleep(1000)