切换到宽版
爱科技/爱创意/爱折腾;电子/数码爱好者的家!欢迎访问新版数码之家网站
  • 3856阅读
  • 15回复

[其他]做个简单的WIFI LCD时钟 [复制链接]

上一主题 下一主题
离线hbozyq
 

发帖
365
M币
8508
专家
45
粉丝
12664
只看楼主 倒序阅读 我要置顶 楼主  发表于: 2018-11-11




如图 LCD来源老王家 1.5RMB 真香
主控8266 内置wifi支持 还能跑MPY


板子很简单 基本上就是官方推荐的最小系统
焊了个铜柱来支撑




开背光的功率也只有0.5w不到


下面是代码 clock.py 封装成模块 需要在boot里面导入
写多了py的感觉就是写起来真顺手 切片和for循环不要太方便
  1. from machine import Pin,RTC,Timer
  2. import time,network,machine
  3. import usocket as socket
  4. import asc2,ntp1
  5. machine.freq(160000000)
  6. #主频设置为160mhz
  7. wlan = network.WLAN(network.STA_IF)
  8. wlan.active(True)
  9. if not wlan.isconnected():
  10.     print('connecting to network...')    
  11.     wlan.connect('SSID','PASSWD')
  12. counter = 0
  13. while (not wlan.isconnected()) and (counter <= 50):
  14.     time.sleep_ms(100)
  15.     counter += 1
  16. while not wlan.isconnected():
  17.     print('WLAN connect failed')
  18. print('network config:', wlan.ifconfig())
  19. #连接WIFI
  20. CS = Pin(2  , Pin.OUT)
  21. RST= Pin(0  , Pin.OUT)
  22. SCK= Pin(4  , Pin.OUT)
  23. SDA= Pin(5  , Pin.OUT)
  24. A0 = Pin(16 , Pin.OUT)
  25. #LCD引脚定义
  26. rtc = RTC()
  27. timetemp = '      '
  28. daytemp = 0
  29. weekday = 0
  30. datetemp = 0
  31. #全局变量初始化
  32. tim1 = Timer(1)
  33. modle = (
  34. (0x00,0x00,0xE0,0xF0,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF0,0xE0,0x00,0x00),
  35. (0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00),
  36. (0x00,0x00,0x0F,0x1F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
  37. (0x00,0x00,0x00,0x60,0x60,0x60,0x70,0xF8,0xF8,0xFC,0xFC,0xFC,0x00,0x00,0x00,0x00),
  38. (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00),
  39. (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00),
  40. (0x00,0x00,0xE0,0xF8,0xF8,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF0,0xE0,0x00,0x00,0x00),
  41. (0x00,0x00,0x01,0x01,0x01,0xC1,0xF0,0xFC,0xFF,0xFF,0x7F,0x1F,0x07,0x00,0x00,0x00),
  42. (0x00,0x00,0x70,0x7C,0x7F,0x7F,0x7F,0x7F,0x73,0x71,0x70,0x70,0x00,0x00,0x00,0x00),
  43. (0x00,0x00,0xF0,0xF8,0xFC,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
  44. (0x00,0x00,0xC1,0xC1,0xC1,0xC1,0xDD,0x1C,0x3C,0xFF,0xFF,0xFF,0xF7,0xE3,0x00,0x00),
  45. (0x00,0x00,0x0F,0x3F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
  46. (0x00,0x00,0x00,0x80,0xF0,0xFC,0x7C,0xFC,0xFC,0xFC,0xFC,0xFC,0x00,0x00,0x00,0x00),
  47. (0x00,0xE0,0xFC,0xFF,0xDF,0xC3,0xC0,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0xC0,0x00,0x00),
  48. (0x00,0x07,0x07,0x07,0x07,0x07,0x07,0x7F,0x7F,0x7F,0x7F,0x7F,0x07,0x07,0x00,0x00),
  49. (0x00,0x00,0xFC,0xFC,0xFC,0xFC,0xFC,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x00,0x00),
  50. (0x00,0x00,0x9F,0x9F,0x9F,0x9F,0x9F,0x06,0x07,0xFF,0xFF,0xFF,0xFE,0xFC,0x00,0x00),
  51. (0x00,0x00,0x0F,0x3F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
  52. (0x00,0x00,0xE0,0xF0,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
  53. (0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x0C,0x0E,0xFE,0xFE,0xFE,0xFC,0xF8,0x00,0x00),
  54. (0x00,0x00,0x0F,0x1F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
  55. (0x00,0x00,0x00,0x1C,0x1C,0x1C,0x1C,0x1C,0xFC,0xFC,0xFC,0xFC,0xFC,0x00,0x00,0x00),
  56. (0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFE,0xFF,0xFF,0xFF,0x0F,0x00,0x00,0x00,0x00),
  57. (0x00,0x00,0x00,0x00,0x60,0x7E,0x7F,0x7F,0x7F,0x1F,0x01,0x00,0x00,0x00,0x00,0x00),
  58. (0x00,0x00,0xE0,0xF8,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
  59. (0x00,0x00,0xE3,0xF7,0xFF,0xFF,0xFF,0x1C,0x1C,0xFF,0xFF,0xFF,0xF7,0xE3,0x00,0x00),
  60. (0x00,0x00,0x0F,0x3F,0x3F,0x7F,0x7F,0x70,0x70,0x7F,0x7F,0x3F,0x3F,0x0F,0x00,0x00),
  61. (0x00,0x00,0xE0,0xF8,0xF8,0xFC,0xFC,0x1C,0x1C,0xFC,0xFC,0xF8,0xF8,0xE0,0x00,0x00),
  62. (0x00,0x00,0x3F,0x7F,0xFF,0xFF,0xFF,0xE0,0x60,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00),
  63. (0x00,0x00,0x0E,0x3E,0x3E,0x7E,0x7E,0x70,0x70,0x7F,0x7F,0x3F,0x1F,0x0F,0x00,0x00),
  64. )
  65. #大数字字模
  66. weekstr = (
  67. 'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'
  68. )
  69. def w(dat,command=1):
  70.     CS.value(0)
  71.     A0.value(command)
  72.     i = 0
  73.     while i < 8:
  74.         if(dat & 0x80):
  75.             SDA.value(1)
  76.         else:
  77.             SDA.value(0)
  78.         SCK.value(0)
  79. #        time.sleep_us(1)
  80.         dat <<= 1
  81.         i += 1
  82.         SCK.value(1)
  83. #        time.sleep_us(1)
  84.     CS.value(1)
  85. #写一字节
  86. def lcdinit():
  87.     RST.value(0)
  88.     time.sleep_ms(5)
  89.     RST.value(1)
  90.     time.sleep_ms(5)
  91.     w(0xa1,0)
  92.     w(0xa0,0)
  93.     w(0xc8,0)
  94.     w(0x26,0)
  95.     w(0x81,0)
  96.     w(26,0)
  97.     w(0x2f,0)
  98.     time.sleep_ms(5)
  99.     w(0xaf,0)
  100.     w(0xf8,0)
  101.     w(0x00,0)
  102.     fill(0xff)
  103.     fill(0x00)
  104.     setxy()    
  105. #初始化命令
  106. def setxy(x=0,y=0):
  107.     w(0xb0+y,0)
  108.     w(((x&0xf0)>>4)|0x10,0)
  109.     w((x&0x0f)|0x01,0)
  110. #设置指针位置
  111. def fill(data):
  112.     setxy()
  113.     for y in range(0,8):
  114.         w(0xB0 + y,0)
  115.         w(0x01,0)
  116.         w(0x10,0)
  117.         for x in range(0,128):
  118.             w(data)
  119.     setxy()
  120. #填充指定数据
  121. def pstr(x,y,text):
  122.     setxy(x,y)
  123.     for i in text:
  124.         for j in asc2.a[(ord(i)-32)]:
  125.             w(j)
  126.         w(0)
  127. #在指定位置输出ascii字符 最长21位
  128. def updatetime(foo = 0):
  129.     try:
  130.         rtc.datetime(ntp1.gettime())
  131.         print(rtc.datetime())
  132.     except OSError as e:
  133.         print(e)
  134.     finally:
  135.         pass
  136. #ntp获取时间
  137. def pdot():
  138.         setxy(43,2)
  139.         w(224)
  140.         w(224)
  141.         setxy(43,4)
  142.         w(7)
  143.         w(7)
  144.         setxy(79,2)
  145.         w(224)
  146.         w(224)
  147.         setxy(79,4)
  148.         w(7)
  149.         w(7)
  150. #显示时间的冒号
  151. def ptime(times):
  152.     global timetemp
  153.     if timetemp != times:
  154.         if times[2:4] == "00":
  155.             pweek()
  156.             pdate()
  157.         for i in range(0,6):
  158.             if timetemp[i] != times[i]:
  159.                 for j in range(0,3):
  160.                     setxy(16*i+11+4*int(i/2),j+2)
  161.                     for k in modle[3*int(times[i])+j]:
  162.                         w(k)
  163.         timetemp = times
  164. #刷新时间 如果一致就不刷新 不一致就刷新局部
  165. #刷新日期和星期的函数放在这个里面 原因自己思考一下
  166. def getWeekDate(year,month,day):
  167.     year = int(year)
  168.     year = year - int(year / 100) * 100    
  169.     century = int(year/100)
  170.     month = int(month)
  171.     if month == 1 or month == 2:
  172.         month = month + 12
  173.         if year == 0:
  174.             year = 99
  175.             century = century - 1
  176.         else:
  177.             year = year - 1
  178.     day =int(day)
  179.     week = year + int(year/4) + int(century/4) - 2 * century + int(26 * (month + 1)/10) + day - 1
  180.     if week < 0:
  181.         weekDay = (week % 7 + 7) % 7
  182.     else:
  183.         weekDay = week % 7
  184.     return weekDay
  185. #蔡勒公式计算星期
  186. def pweek():
  187.     global daytemp,weekstr,weekday
  188.     if daytemp != rtc.datetime()[2]:
  189.         weekday = getWeekDate(rtc.datetime()[0],rtc.datetime()[1],rtc.datetime()[2])
  190.         daytemp = rtc.datetime()[2]
  191.         pstr(2,7,'{: ^21}'.format('%s'%weekstr[weekday]))
  192. #刷新星期
  193. def pdate():
  194.     global datetemp
  195.     date = '{: ^21}'.format('%d-%02d-%02d'%(rtc.datetime()[0],rtc.datetime()[1],rtc.datetime()[2]))
  196.     if (date != datetemp):
  197.         pstr(2,6,date)
  198.         datetemp = date
  199. def init():
  200.     tim1.init(mode=Timer.PERIODIC,period=180000,callback=updatetime)
  201.     #BL.value(1)
  202.     lcdinit()
  203.     updatetime()
  204.     pstr(2,0,"\"I DONT GIVE A F**K.\"")
  205.     pdot()
  206.     ptime("%02d%02d%02d"%(rtc.datetime()[4],rtc.datetime()[5],rtc.datetime()[6]))
  207.     pweek()
  208.     pdate()
  209.     while 1:
  210.         ptime("%02d%02d%02d"%(rtc.datetime()[4],rtc.datetime()[5],rtc.datetime()[6]))
  211. #初始化时钟和定时器




本文内容包含图片或附件,获取更多资讯,请 登录 后查看;或者 注册 成为会员获得更多权限
本帖最近打赏记录:共7条打赏M币+81专家+1
weizaisifang M币 +13 謝謝分享 2018-12-16
hellodoraemo M币 +20 認真發帖 2018-11-11
qrut 专家 +1 優秀文章 2018-11-11
qrut M币 +13 優秀文章 2018-11-11
miqi328 M币 +20 专业级别 2018-11-11
ggbugs M币 +6 膜拜软硬通吃大神 2018-11-11
hongo M币 +9 優秀文章 2018-11-11
M币换购:大丈夫能屈能伸!数码之家22合一拆机工具(拆客必备系列)
 
离线万里石

发帖
361
M币
-166
专家
0
粉丝
13
只看该作者 1楼 发表于: 2018-11-11
动手能力不错
离线fhs1325

发帖
7926
M币
6525
专家
1
粉丝
9
只看该作者 2楼 发表于: 2018-11-11
如果有农历,天气参数,那就完善了
离线lian1819744

发帖
1956
M币
2665
专家
1
粉丝
22
只看该作者 3楼 发表于: 2018-11-11
跪伏!本月已经外购几十块零件还没修好电饭锅的手残党佩服
你大哥我

发帖
19
M币
892
专家
0
粉丝
0
只看该作者 4楼 发表于: 2018-11-11
感觉屏幕有点漏光?

内容来自Android手机客户端

离线lipengbo

发帖
97
M币
-145
专家
0
粉丝
4
只看该作者 5楼 发表于: 2018-11-11
会写代码的都是大神
离线ggbugs

发帖
1632
M币
11
专家
-1
粉丝
18
只看该作者 6楼 发表于: 2018-11-11
膜拜软硬通吃大神
离线ahyu99

发帖
2650
M币
5961
专家
5
粉丝
41
只看该作者 7楼 发表于: 2018-11-11
你用的ESP-12WIFI模块,那么大的耗电电流?
专业回收航母。
离线lyy-cy

发帖
326
M币
104
专家
9
粉丝
26
只看该作者 8楼 发表于: 2018-11-11
http://bbs.mydigit.cn/read.php?tid=2224769

我就是用了Python后觉得太好了,就去学软件了。
高端大气上档次,低调奢华有内涵,奔放洋气有深度,简约时尚国际范,低端粗俗甩节操,土憋矫情无下限,装模作样绿茶婊,外猛内柔女汉子,卖萌嘟嘴剪刀手,忧郁深沉无所谓,狂拽帅气吊炸天,冷艳高贵接地气,时尚亮丽小清新,可爱乡村非主流,贵族王朝杀马特,提莫团战必须死
离线devcang

发帖
18104
M币
9199
专家
20
粉丝
97
只看该作者 9楼 发表于: 2018-11-11
ESP 还喜欢C++,因为 arduino带的库也很多

~~~~~~~~~
能做自己喜欢的事情,会是最大的幸福