|

楼主 |
发表于 2019-11-1 09:14:39
|
显示全部楼层
代码供参考
- import network
- import time
- from tm1638 import TM1638
- from ds1302 import DS1302
- from machine import Pin
- import gc
- gc.collect()
- class NtpClock():
- def __init__(self):
- self.hour = 0
- self.min = 0
- self.sec = 0
- self.week = 0
- self.is_WiFi_ok = 0
-
- def setup(self):
- self.ds = DS1302(clk=Pin(16), dio=Pin(5), cs=Pin(13))
- self.tm = TM1638(stb=Pin(4), clk=Pin(14), dio=Pin(12))
- self.wlan = network.WLAN(network.STA_IF)
- def do_connect_wifi(self):
- self.ssid = "home8888"
- self.password = "8888888"
- self.wlan.active(True)
- self.wlan.connect(self.ssid,self.password)
- _temcont = 0
- while True:
- _temcont += 1
- if self.wlan.isconnected():
- self.is_WiFi_ok = 1
- print('network connected ...')
- print('network config:', self.wlan.ifconfig())
- self.tm.showWIFI(0)
- return
- elif _temcont >10:
- print('network is not connected ...')
- self.tm.showWIFI(1)
- self.is_WiFi_ok = 0
- return
- self.tm.showWIFI(_temcont & 0x01)
- self.read_ds1302()
- self.tm1638_show_week()
- self.tm1638_show_hou_min()
- time.sleep(1)
-
- def dis_connect_wifi(self):
- self.wlan.active(False)
- self.is_WiFi_ok = 0
- print('network config:', self.wlan.ifconfig())
- def read_ds1302(self):
- self.hour = self.ds.Hour()
- self.min = self.ds.Minute()
- self.sec = self.ds.Second()
- self.week = self.ds.Weekday()
-
- def tm1638_show_week(self):
- self.tm.week_nub(self.week)
- def tm1638_show_hou_min(self):
- self.tm.time_numbers(self.hour,self.min,colon=(self.sec & 0x01)) #用秒的最后一位判断是否显示冒号,跳动
- myPro = NtpClock()
- myPro.setup()
- myPro.do_connect_wifi()
- #myPro.dis_connect_wifi()
- while True:
- myPro.read_ds1302()
- myPro.tm1638_show_week()
- myPro.tm1638_show_hou_min()
复制代码
|
|