基于micropython实现网络时间自动校准器

释放双眼,带上耳机,听听看~!

基于DS3231时钟模块+Tpyboard V102+ssd1306 oled显示屏

ssd1306.py

import machine
 from machine import I2C,Pin
DS3231_ADDR = const(0x68)
 DS3231_REG_SEC = b'\x00'
 DS3231_REG_MIN = b'\x01'
 DS3231_REG_HOUR = b'\x02'
 DS3231_REG_WEEKDAY= b'\x03'
 DS3231_REG_DAY = b'\x04'
 DS3231_REG_MONTH = b'\x05'
 DS3231_REG_YEAR = b'\x06'
 DS3231_REG_A1SEC = b'\x07'
 DS3231_REG_A1MIN = b'\x08'
 DS3231_REG_A1HOUR = b'\x09'
 DS3231_REG_A1DAY = 0x0A
 DS3231_REG_A2MIN = 0x0B
 DS3231_REG_A2HOUR = 0x0C
 DS3231_REG_A2DAY = 0x0D
 DS3231_REG_CTRL = 0x0E
 DS3231_REG_STA = 0x0F
 DS3231_REG_OFF = 0x10
 DS3231_REG_TEMP = b'\x11'
class DS3231(object):
 def __init__(self):
 self.i2c =I2C(scl=Pin(14),sda=Pin(2),freq=100000)
 def DATE(self, dat=[]):
 if dat==[]:
 t = []
 t.append(str(self.year()))
 t.append(str(self.month()))
 t.append(str(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(str(self.hour()))
 t.append(str(self.min()))
 t.append(str(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
 self.i2c.writeto(DS3231_ADDR,buf)
def getREG_DEC(self,reg):
 self.i2c.writeto(DS3231_ADDR,reg)
 t = self.i2c.readfrom(DS3231_ADDR,1)[0]
 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):
 self.i2c.writeto(DS3231_ADDR,DS3231_REG_TEMP)
 t1 = self.i2c.readfrom(DS3231_ADDR,1)[0]
 self.i2c.writeto(DS3231_ADDR,b'\x12')
 t2 = self.i2c.readfrom(DS3231_ADDR,1)[0]
 if t1>0x7F:
 return t1 - t2/256 -256
 else:
 return t1 + t2/256
main.py
#import urequests as requests
 import urequests
 import json
 import time
 from machine import SPI,Pin #导入SPI、Pin库
 import ssd1306 #导入OLED显示屏驱动库
 from ds3231 import DS3231 #导入DS3231时钟模块库
 from machine import Timer #导入Timer库
#-----------------------DS3231模块-----------------------#
 ds=DS3231() #ds3231初始化
 NOW_DATE = ''
 #-----------------------OLED显示屏-----------------------#
 #SPI接口对应的引脚定义
 spi = SPI(baudrate=10000000, polarity=1, phase=0, sck=Pin(12,Pin.OUT), mosi=Pin(13,Pin.OUT), miso=Pin(2))
 #OLED显示屏的设置,128宽 64高 spi对象 DC接的G15 RES接的G16 CS接的G5
 display = ssd1306.SSD1306_SPI(128, 64, spi, Pin(15),Pin(16), Pin(5))
 display.poweron() #打开显示屏
 display.init_display() #初始化显示
 display.text('Waiting.....',1,1) #显示的内容,x坐标,y坐标
 display.show() #进行显示
def getNetTime():
 url = 'http://quan.suning.com/getSysTime.do'
 res=urequests.get(url).text
 print(res)
 j=json.loads(res)
 t2_date = j['sysTime2'].split()[0] #日期
 t2_time = j['sysTime2'].split()[1] #时间
 display.fill(0) #清屏
 display.text(t2_date,1,2)
 display.text(t2_time,1,15)
 display.show()
 #初始日期和时间,设置一次即可
 ds.DATE([int(x) for x in t2_date[2:].split('-')]) #设置初始日期年、月、日
 ds.TIME([int(x) for x in t2_time.split(':')]) #设置初始时间时、分、秒
def updateTime(t):
 global NOW_DATE
 #读取日期和时间,拼接成正常的时间格式
 date = '20'+'-'.join(ds.DATE())
 time = ':'.join(ds.TIME())
 if NOW_DATE != date:
 getNetTime() #设定每天校对一下时间
 NOW_DATE = date
 else:
 display.fill(0) #清屏
 display.text(date,1,2)
 display.text(time,1,15)
 display.show()
if __name__ == '__main__':
 getNetTime()
 time.sleep(1)
 #-----------------------Timer定时器-----------------------#
 tim = Timer(-1) #新建一个定时器
 #每隔1秒执行一次updateTime函数调用,用于更新OLED显示屏上的时间
 tim.init(period=1000, mode=Timer.PERIODIC, callback=updateTime)

给TA打赏
共{{data.count}}人
人已打赏
动态

一个用于在Raspberry Pi上访问MPU-6050数字加速度计和陀螺仪的Python模块。

2019-12-30 13:28:55

动态

传感器融合可根据运动跟踪设备的输出来计算偏航,俯仰和横滚

2019-12-30 13:57:30

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'); })();