python时钟编程教程_Micropython TurnipBit 电子时钟 青少年编程入门

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值