Random Robo Smith
Published

Creating a Real-time clock on EncroPi using MicroPython

EncroPi: Read, Encrypt & Store your Data in Real-Time! USB Data Logger | USB RTC | USB Encryption | Based on Raspberry Pi RP2040 | DS3231

BeginnerProtip1 hour1,209

Things used in this project

Story

Read more

Code

EncroPi Real-time Clock

MicroPython
from machine import I2C
import time
import utime
from machine import Pin, UART,SPI
import time
import st7789

import vga1_8x16 as font1
import vga1_16x32 as font
import vga1_16x16 as font2
address = 0x68
register = 0x00
#sec min hour week day month year
CurrentTime = b'\x00\x50\x14\x05\x21\x06\x22' #00:00:10 friday 03/03/2022 
week  = ["Sun","Mon","Tues","Wed","Thur","Fri","Sat"];

bus = I2C(1) # i2c 1

spi = SPI(1, baudrate=40000000, sck=Pin(10), mosi=Pin(11))
tft = st7789.ST7789(spi,135,240,reset=Pin(12, Pin.OUT),cs=Pin(9, Pin.OUT),dc=Pin(8, Pin.OUT),backlight=Pin(13, Pin.OUT),rotation=1)


def PicoRTCSetTime():
    bus.writeto_mem(int(address),int(register),CurrentTime)

def PicoRTCReadTime():
    return bus.readfrom_mem(int(address),int(register),7);
        
def info():
    tft.init()
    utime.sleep(0.2)
    tft.text(font,"RP2040 RTC", 15,20)
    tft.fill_rect(15, 60, 210,10, st7789.RED)
    
    tft.text(font,"Real Time", 15,70,st7789.YELLOW)
    tft.text(font,"Clock", 15,100,st7789.YELLOW)
    tft.fill_rect(15, 140, 210, 10, st7789.BLUE)
    time.sleep(2)
    tft.fill(0) #clear screen
    
info()

PicoRTCSetTime() #You can remove this line once time is set

while 1:
    data = PicoRTCReadTime()
    a = data[0]&0x7F  #sec
    b = data[1]&0x7F  #min
    c = data[2]&0x3F  #hour
    d = data[3]&0x07  #week
    e = data[4]&0x3F  #day
    f = data[5]&0x1F  #month
    g = data[6]&0x3F #year
    #dt = "20%x/%02x/%02x %02x:%02x:%02x %s" %(g,f,e,c,b,a,week[d-1])
    t = "%02x:%02x:%02x %s" %(c,b,a,week[d-3])
    d = "%02x/%02x/20%x"%(e,f,g)
    print(t) #year,month,day,hour,min,sec,week
    print(d)
    tft.text(font,d, 15,20,st7789.WHITE)
    tft.fill_rect(15, 60, 210,10, st7789.RED)
    tft.text(font,t, 15,80,st7789.YELLOW)
    time.sleep(1)

Credits

Random Robo Smith

Random Robo Smith

93 projects • 57 followers
Electronic Enthusiast I do Projects, Tutorials, Reviews and more...
Thanks to Kamaluddin Khan.

Comments