本帖最后由 电子烂人 于 2024-2-14 01:14 编辑
笔者在这个任务里卡了好久好久,笔者的ntptime这个库的例程在本次任务的W5500-PICO -evb 板子上无法使用,MPY文档里都是在8266等WiFi芯片里使用,W5500-PICO会显示没有对应的库。。。
用查询方法查找固件,也会发现没有对应的这个库文件
后来在看到其他大佬的帖子后才发现,W5500evb有专用的烧录固件,同理,使用PICO链接W5500模块也可刷这个库来实现NTP功能
传送门:MicroPython - Python for microcontrollers
烧录好新固件之后再试试:
这个MPY的官方固件就包含NTPTIME这个链接NTP的专用库了
使用HELP函数可以查看NTPTIME中的各类函数定义和结构:
import ntptime
help(ntptime)
help(ntptime.settime)
help(ntptime.utime)
运行后会看到PICO当前固件里的对应函数的各种功能,这个小技巧对于开发MPY还是蛮好用的
2.从NTP服务器获取时间并显示
从上文已知NTPTIME库中的HOST常量已经设置为pool.ntp.org,我们只需使用ntptime.settime()这个函数获取NTP服务器返回的时间数据即可。
在@StreakingJerry大佬的帖子启发下,还需要手动增加对应的秒数来确保校准成功。
在已经配好网络的情况下添加驱动和时间校准代码:
from machine import Pin,SPI,PWM
from usocket import socket
import ntptime
import network
import framebuf
import time
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
def w5x00_init():
spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
nic = network.WIZNET5K(spi,Pin(17),Pin(20))
nic.active(True)
try:
nic.ifconfig('dhcp')
except:
nic.ifconfig(('192.168.0.64', '255.255.255.0', '192.168.0.1', '192.168.0.1'))
print('IP address :', nic.ifconfig())
while not nic.isconnected():
time.sleep(1)
#print(nic.regs())
class LCD_1inch14(framebuf.FrameBuffer):
def __init__(self):
self.width = 240
self.height = 135
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.red = 0x07E0
self.green = 0x001f
self.blue = 0xf800
self.white = 0xffff
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36)
self.write_data(0x70)
self.write_cmd(0x3A)
self.write_data(0x05)
self.write_cmd(0xB2)
self.write_data(0x0C)
self.write_data(0x0C)
self.write_data(0x00)
self.write_data(0x33)
self.write_data(0x33)
self.write_cmd(0xB7)
self.write_data(0x35)
self.write_cmd(0xBB)
self.write_data(0x19)
self.write_cmd(0xC0)
self.write_data(0x2C)
self.write_cmd(0xC2)
self.write_data(0x01)
self.write_cmd(0xC3)
self.write_data(0x12)
self.write_cmd(0xC4)
self.write_data(0x20)
self.write_cmd(0xC6)
self.write_data(0x0F)
self.write_cmd(0xD0)
self.write_data(0xA4)
self.write_data(0xA1)
self.write_cmd(0xE0)
self.write_data(0xD0)
self.write_data(0x04)
self.write_data(0x0D)
self.write_data(0x11)
self.write_data(0x13)
self.write_data(0x2B)
self.write_data(0x3F)
self.write_data(0x54)
self.write_data(0x4C)
self.write_data(0x18)
self.write_data(0x0D)
self.write_data(0x0B)
self.write_data(0x1F)
self.write_data(0x23)
self.write_cmd(0xE1)
self.write_data(0xD0)
self.write_data(0x04)
self.write_data(0x0C)
self.write_data(0x11)
self.write_data(0x13)
self.write_data(0x2C)
self.write_data(0x3F)
self.write_data(0x44)
self.write_data(0x51)
self.write_data(0x2F)
self.write_data(0x1F)
self.write_data(0x1F)
self.write_data(0x20)
self.write_data(0x23)
self.write_cmd(0x21)
self.write_cmd(0x11)
self.write_cmd(0x29)
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x28)
self.write_data(0x01)
self.write_data(0x17)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x35)
self.write_data(0x00)
self.write_data(0xBB)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
if __name__=='__main__':
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(32768)#max 65535
LCD = LCD_1inch14()
#color BRG
LCD.fill(LCD.white)
LCD.text("Follow me NO.4",0,5,LCD.red)
LCD.text("Advanced Task :",130,5,LCD.green)
LCD.hline(10,15,220,LCD.blue)
LCD.hline(10,115,220,LCD.blue)
LCD.vline(10,15,100,LCD.blue)
LCD.vline(230,15,100,LCD.blue)
w5x00_init()
LCD.text("now time:",15,40,LCD.blue)
#LCD.text("ip:%s"%,15,30,LCD.blue)
LCD.text("by ee_jark",120,120,LCD.blue)
LCD.show()
while True:
try:
ntptime.settime()
t = time.localtime(time.time() + 8*3600)
time.sleep_ms(1000)
break
except:
print("Can not get time!")
print("time: %s-%s-%s %s:%s:%s" % (t[0],t[1],t[2],t[3],t[4],t[5]))
LCD.text("%s-%s-%s"% (t[0],t[1],t[2]),25,60,LCD.blue)
LCD.text('%s:%s:%s'% (t[3],t[4],t[5]),25,70,LCD.blue)
LCD.show()
time.sleep(1)
LCD.fill(0xFFFF)
烧录到板卡中:
板卡上的图像如下:
补充内容 (2024-2-24 02:09):
后续使用延时同时做出了电压检测和每分钟刷新的效果,参考文章https://bbs.eeworld.com.cn/thread-1272400-1-1.html
|