Opäť v prvom rade potrebná knižnica, jej názov bude „ds3231.py“.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from machine import Pin, I2C, SPI, PWM
import framebuf
import utime
import binascii
# the first version use i2c1
# it should solder the R3 with 0R resistor if want to use alarm function,please refer to the Sch file on waveshare Pico-RTC-DS3231 wiki
# https://www.waveshare.net/w/upload/0/08/Pico-RTC-DS3231_Sch.pdf
# ===Set up RTC ===============
class ds3231(object):
# 13:45:00 Mon 24 May 2021
# the register value is the binary-coded decimal (BCD) format
# sec min hour week day month year
NowTime = b'\x00\x45\x13\x02\x24\x05\x21'
w = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
address = 0x68
start_reg = 0x00
alarm1_reg = 0x07
control_reg = 0x0e
status_reg = 0x0f
def __init__(self,i2c_port,i2c_scl,i2c_sda):
self.bus = I2C(i2c_port,scl=Pin(i2c_scl),sda=Pin(i2c_sda))
def set_time(self,new_time):
hour = new_time[0] + new_time[1]
minute = new_time[3] + new_time[4]
second = new_time[6] + new_time[7]
week = "0" + str(self.w.index(new_time.split(",",2)[1])+1)
year = new_time.split(",",2)[2][2] + new_time.split(",",2)[2][3]
month = new_time.split(",",2)[2][5] + new_time.split(",",2)[2][6]
day = new_time.split(",",2)[2][8] + new_time.split(",",2)[2][9]
now_time = binascii.unhexlify((second + " " + minute + " " + hour + " " + week + " " + day + " " + month + " " + year).replace(' ',''))
#print(binascii.unhexlify((second + " " + minute + " " + hour + " " + week + " " + day + " " + month + " " + year).replace(' ','')))
#print(self.NowTime)
self.bus.writeto_mem(int(self.address),int(self.start_reg),now_time)
def read_time(self):
t = self.bus.readfrom_mem(int(self.address),int(self.start_reg),7)
sc = t[0]&0x7F #second
mn = t[1]&0x7F #minute
hr = t[2]&0x3F #hour
wk = t[3]&0x07 #week
dy = t[4]&0x3F #day
mth = t[5]&0x1F #month
# print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],self.w[t[3]-1]))
return ("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],self.w[t[3]-1]))
def read_time_spec(self):
t = self.bus.readfrom_mem(int(self.address),int(self.start_reg),7)
sc = t[0]&0x7F #second
mn = t[1]&0x7F #minute
hr = t[2]&0x3F #hour
wk = t[3]&0x07 #week
dy = t[4]&0x3F #day
mth = t[5]&0x1F #month
# print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],self.w[t[3]-1]))
return ("20%x/%02x/%02x %02x:%02x:%02x" %(t[6],t[5],t[4],t[2],t[1],t[0]))
def set_alarm_time(self,alarm_time):
# init the alarm pin
self.alarm_pin = Pin(ALARM_PIN,Pin.IN,Pin.PULL_UP)
# set alarm irq
self.alarm_pin.irq(lambda pin: print("alarm1 time is up"), Pin.IRQ_FALLING)
# enable the alarm1 reg
self.bus.writeto_mem(int(self.address),int(self.control_reg),b'\x05')
# convert to the BCD format
hour = alarm_time[0] + alarm_time[1]
minute = alarm_time[3] + alarm_time[4]
second = alarm_time[6] + alarm_time[7]
date = alarm_time.split(",",2)[2][8] + alarm_time.split(",",2)[2][9]
now_time = binascii.unhexlify((second + " " + minute + " " + hour + " " + date).replace(' ',''))
# write alarm time to alarm1 reg
self.bus.writeto_mem(int(self.address),int(self.alarm1_reg),now_time)
def temperature(self):
t = self.bus.readfrom_mem(int(self.address),int(self.start_reg),19)
whole = t[17]&127 # Lowest 7 bits
decimal = ((t[18]& 192)/64) *0.25
temp = whole + decimal
if (t[17]&128): # High bit set? Indicates negative value
temp = temp * -1
return temp
# ======== Main ==========
Teraz sa môžeme pozrieť na kúsok testovacieho kódu, ak ho chceme spustiť treba ho nazvať „main.py“
from ds3231 import ds3231
from time import sleep
# www.tkinter.eu
print("www.tkinter.eu")
I2C_PORT = 1
I2C_SDA = 6
I2C_SCL = 7
rtc = ds3231(I2C_PORT,I2C_SCL,I2C_SDA)
while True:
rtc.read_time()
#rtc.set_time('19:17:50,Sunday,2023-09-03')
now=rtc.read_time_spec()
print(now)
sleep(5)