咸鱼ZTMR实例—LCDRTC时钟

咸鱼ZTMR实例—LCDRTC时钟

RTC时钟显示


主控板:ZTMR1.1开发板
在这里插入图片描述
1.54寸液晶屏st7789 spi

在这里插入图片描述
RTC使用

from pyb import RTC  #定义对象
rtc.datetime([datetimetuple]'''datetimetuple格式:(year,month,day,weekday,hours,minutes,seconds,subseconds)
   weekday:周一至周日7天
   subsecond:255~0

基本属性介绍:

属性 说明
lcdinit() 初始化
chars 字号:16px…text:文字。x:x坐标。y:y坐标。a:间距
set_color(fg,bg) 显示彩色文字RGB565: 前景色 , 背景色
set_font(font) 字体:bauhs93 预设大号字体;tt14预设小号字体
write(text) 输入字符
set_pos(x,y) 从此坐标开始显示x:横坐标,y纵坐标
erase() 清屏
chars(text,x,y) 在指定其实位置显示字符

示例代码

import time
import ustruct
import framebuf
import tt14
import bauhs93
from pyb import RTC
from utime import sleep_us
from micropython import const
from machine import Pin
from pyb import SPI
from ztst7789class import ST7789

#---------TFT pin 定义
TFT_RST_PIN = Pin('C4')
TFT_LED_PIN = Pin('B10')
TFT_DC_PIN = Pin('C5')
TFT_CS_PIN = Pin('B11')
TFT_CLK_PIN = Pin('A5')
TFT_MISO_PIN = Pin('A6')
TFT_MOSI_PIN = Pin('A7')


	
def color565(r, g, b):  #255 255 255
	return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3

# 定义星期和时间(时分秒)显示字符列表
week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
time = ['', '', '']
bl = Pin(TFT_LED_PIN, Pin.OUT)
LCD = None

# 初始化所有相关对象
rtc = RTC()
# 默认时间(2020年8月5日周三15点30)
rtc.datetime((2020, 8, 5, 3, 15, 30, 0, 0))

def lcdinit():   #初始化函数
	global LCD
	bl.value(1)  #屏幕背光打开
	
	spi = SPI(1,SPI.MASTER,baudrate=7800000,polarity=0,phase=0)
	LCD = ST7789(spi, cs=Pin(TFT_CS_PIN), dc=Pin(TFT_DC_PIN), rst=TFT_RST_PIN)

lcdinit()    #初始化
LCD.erase()  # 清屏显示黑色背景
LCD.set_font(bauhs93)  #设置字体
LCD.set_color(color565(255,255,0),color565(0,0,0))

while True:
	t = rtc.datetime() # 获取当前时间
	LCD.chars('mizhixianyu', 60, 70) 
	LCD.chars('RTC  shiyan', 60, 100) 
	# 显示日期,字符串可以直接用“+”来连接
	LCD.chars(str(t[0]) + '-' + str(t[1]) + '-' + 
	str(t[2]) + ' ' + week[(t[3] - 1)], 60, 130)
	# 显示时间需要判断时、分、秒的值否小于 10,如果小于 10,则在显示前面补“0”以 #达
	# 到较佳的显示效果
	for i in range(4, 7):
		if t[i] < 10:
			time[i - 4] = "0"
		else:
			time[i - 4] = ""
	# 显示时间
	LCD.chars(time[0] + str(t[4]) + ':' + time[1] + 
	str(t[5]) + ':' + time[2] + str(t[6]), 60, 160)
	
	pyb.delay(300)

效果图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45020839/article/details/107815356