-
UID:1158100
-
- 注册时间2012-05-16
- 最后登录2022-02-25
- 在线时间345小时
-
-
访问TA的空间加好友用道具
|
如图 LCD来源老王家 1.5RMB 真香主控8266 内置wifi支持 还能跑MPY 板子很简单 基本上就是官方推荐的最小系统焊了个铜柱来支撑
开背光的功率也只有0.5w不到 下面是代码 clock.py 封装成模块 需要在boot里面导入写多了py的感觉就是写起来真顺手 切片和for循环不要太方便 - from machine import Pin,RTC,Timer
- import time,network,machine
- import usocket as socket
- import asc2,ntp1
- machine.freq(160000000)
- #主频设置为160mhz
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- if not wlan.isconnected():
- print('connecting to network...')
- wlan.connect('SSID','PASSWD')
- counter = 0
- while (not wlan.isconnected()) and (counter <= 50):
- time.sleep_ms(100)
- counter += 1
- while not wlan.isconnected():
- print('WLAN connect failed')
- print('network config:', wlan.ifconfig())
- #连接WIFI
- CS = Pin(2 , Pin.OUT)
- RST= Pin(0 , Pin.OUT)
- SCK= Pin(4 , Pin.OUT)
- SDA= Pin(5 , Pin.OUT)
- A0 = Pin(16 , Pin.OUT)
- #LCD引脚定义
- rtc = RTC()
- timetemp = ' '
- daytemp = 0
- weekday = 0
- datetemp = 0
- #全局变量初始化
- tim1 = Timer(1)
- modle = (
- (0x00,0x00,0xE0,0xF0,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF0,0xE0,0x00,0x00),
- (0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00),
- (0x00,0x00,0x0F,0x1F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
- (0x00,0x00,0x00,0x60,0x60,0x60,0x70,0xF8,0xF8,0xFC,0xFC,0xFC,0x00,0x00,0x00,0x00),
- (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00),
- (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00),
- (0x00,0x00,0xE0,0xF8,0xF8,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF0,0xE0,0x00,0x00,0x00),
- (0x00,0x00,0x01,0x01,0x01,0xC1,0xF0,0xFC,0xFF,0xFF,0x7F,0x1F,0x07,0x00,0x00,0x00),
- (0x00,0x00,0x70,0x7C,0x7F,0x7F,0x7F,0x7F,0x73,0x71,0x70,0x70,0x00,0x00,0x00,0x00),
- (0x00,0x00,0xF0,0xF8,0xFC,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
- (0x00,0x00,0xC1,0xC1,0xC1,0xC1,0xDD,0x1C,0x3C,0xFF,0xFF,0xFF,0xF7,0xE3,0x00,0x00),
- (0x00,0x00,0x0F,0x3F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
- (0x00,0x00,0x00,0x80,0xF0,0xFC,0x7C,0xFC,0xFC,0xFC,0xFC,0xFC,0x00,0x00,0x00,0x00),
- (0x00,0xE0,0xFC,0xFF,0xDF,0xC3,0xC0,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0xC0,0x00,0x00),
- (0x00,0x07,0x07,0x07,0x07,0x07,0x07,0x7F,0x7F,0x7F,0x7F,0x7F,0x07,0x07,0x00,0x00),
- (0x00,0x00,0xFC,0xFC,0xFC,0xFC,0xFC,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x00,0x00),
- (0x00,0x00,0x9F,0x9F,0x9F,0x9F,0x9F,0x06,0x07,0xFF,0xFF,0xFF,0xFE,0xFC,0x00,0x00),
- (0x00,0x00,0x0F,0x3F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
- (0x00,0x00,0xE0,0xF0,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
- (0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x0C,0x0E,0xFE,0xFE,0xFE,0xFC,0xF8,0x00,0x00),
- (0x00,0x00,0x0F,0x1F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
- (0x00,0x00,0x00,0x1C,0x1C,0x1C,0x1C,0x1C,0xFC,0xFC,0xFC,0xFC,0xFC,0x00,0x00,0x00),
- (0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFE,0xFF,0xFF,0xFF,0x0F,0x00,0x00,0x00,0x00),
- (0x00,0x00,0x00,0x00,0x60,0x7E,0x7F,0x7F,0x7F,0x1F,0x01,0x00,0x00,0x00,0x00,0x00),
- (0x00,0x00,0xE0,0xF8,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
- (0x00,0x00,0xE3,0xF7,0xFF,0xFF,0xFF,0x1C,0x1C,0xFF,0xFF,0xFF,0xF7,0xE3,0x00,0x00),
- (0x00,0x00,0x0F,0x3F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
- (0x00,0x00,0xE0,0xF8,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
- (0x00,0x00,0x3F,0x7F,0xFF,0xFF,0xFF,0xE0,0x60,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00),
- (0x00,0x00,0x0E,0x3E,0x3E,0x7E,0x7E,0x70,0x70,0x7F,0x7F,0x3F,0x1F,0x0F,0x00,0x00),
- )
- #大数字字模
- weekstr = (
- 'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'
- )
- def w(dat,command=1):
- CS.value(0)
- A0.value(command)
- i = 0
- while i < 8:
- if(dat & 0x80):
- SDA.value(1)
- else:
- SDA.value(0)
- SCK.value(0)
- # time.sleep_us(1)
- dat <<= 1
- i += 1
- SCK.value(1)
- # time.sleep_us(1)
- CS.value(1)
- #写一字节
- def lcdinit():
- RST.value(0)
- time.sleep_ms(5)
- RST.value(1)
- time.sleep_ms(5)
- w(0xa1,0)
- w(0xa0,0)
- w(0xc8,0)
- w(0x26,0)
- w(0x81,0)
- w(26,0)
- w(0x2f,0)
- time.sleep_ms(5)
- w(0xaf,0)
- w(0xf8,0)
- w(0x00,0)
- fill(0xff)
- fill(0x00)
- setxy()
- #初始化命令
- def setxy(x=0,y=0):
- w(0xb0+y,0)
- w(((x&0xf0)>>4)|0x10,0)
- w((x&0x0f)|0x01,0)
- #设置指针位置
- def fill(data):
- setxy()
- for y in range(0,8):
- w(0xB0 + y,0)
- w(0x01,0)
- w(0x10,0)
- for x in range(0,128):
- w(data)
- setxy()
- #填充指定数据
- def pstr(x,y,text):
- setxy(x,y)
- for i in text:
- for j in asc2.a[(ord(i)-32)]:
- w(j)
- w(0)
- #在指定位置输出ascii字符 最长21位
- def updatetime(foo = 0):
- try:
- rtc.datetime(ntp1.gettime())
- print(rtc.datetime())
- except OSError as e:
- print(e)
- finally:
- pass
- #ntp获取时间
- def pdot():
- setxy(43,2)
- w(224)
- w(224)
- setxy(43,4)
- w(7)
- w(7)
- setxy(79,2)
- w(224)
- w(224)
- setxy(79,4)
- w(7)
- w(7)
- #显示时间的冒号
- def ptime(times):
- global timetemp
- if timetemp != times:
- if times[2:4] == "00":
- pweek()
- pdate()
- for i in range(0,6):
- if timetemp[i] != times[i]:
- for j in range(0,3):
- setxy(16*i+11+4*int(i/2),j+2)
- for k in modle[3*int(times[i])+j]:
- w(k)
- timetemp = times
- #刷新时间 如果一致就不刷新 不一致就刷新局部
- #刷新日期和星期的函数放在这个里面 原因自己思考一下
- def getWeekDate(year,month,day):
- year = int(year)
- year = year - int(year / 100) * 100
- century = int(year/100)
- month = int(month)
- if month == 1 or month == 2:
- month = month + 12
- if year == 0:
- year = 99
- century = century - 1
- else:
- year = year - 1
- day =int(day)
- week = year + int(year/4) + int(century/4) - 2 * century + int(26 * (month + 1)/10) + day - 1
- if week < 0:
- weekDay = (week % 7 + 7) % 7
- else:
- weekDay = week % 7
- return weekDay
- #蔡勒公式计算星期
- def pweek():
- global daytemp,weekstr,weekday
- if daytemp != rtc.datetime()[2]:
- weekday = getWeekDate(rtc.datetime()[0],rtc.datetime()[1],rtc.datetime()[2])
- daytemp = rtc.datetime()[2]
- pstr(2,7,'{: ^21}'.format('%s'%weekstr[weekday]))
- #刷新星期
- def pdate():
- global datetemp
- date = '{: ^21}'.format('%d-%02d-%02d'%(rtc.datetime()[0],rtc.datetime()[1],rtc.datetime()[2]))
- if (date != datetemp):
- pstr(2,6,date)
- datetemp = date
- def init():
- tim1.init(mode=Timer.PERIODIC,period=180000,callback=updatetime)
- #BL.value(1)
- lcdinit()
- updatetime()
- pstr(2,0,"\"I DONT GIVE A F**K.\"")
- pdot()
- ptime("%02d%02d%02d"%(rtc.datetime()[4],rtc.datetime()[5],rtc.datetime()[6]))
- pweek()
- pdate()
- while 1:
- ptime("%02d%02d%02d"%(rtc.datetime()[4],rtc.datetime()[5],rtc.datetime()[6]))
- #初始化时钟和定时器
|