from machine import I2C, Pin
import utime
# DS1307'nin I2C adresi
DS1307_ADDR = 0x68
# I2C nesnesini oluştduk
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
#Data BCD olarak geldiğinden dönüşüm fonk kullandık.
def bcd_to_decimal(bcd):
return (bcd // 16) * 10 + (bcd % 16)
def read_ds1307():
"""DS1307'den tarih ve saat bilgisini oku"""
i2c.writeto(DS1307_ADDR, b'\x00') # DS1307 adresinin 0x00'ına (DATETIME_REG) yazma işlemi yapılır.
data = i2c.readfrom(DS1307_ADDR, 7) # DS1307 adresinden 7 byte veri okunur
second = bcd_to_decimal(data[0] & 0x7F)
minute = bcd_to_decimal(data[1])
hour = bcd_to_decimal(data[2] & 0x3F)
day = bcd_to_decimal(data[4])
month = bcd_to_decimal(data[5])
year = bcd_to_decimal(data[6]) + 2000
return year, month, day, hour, minute, second
# DS1307'ye güç ver
i2c.writeto_mem(DS1307_ADDR, 0x00, b'\x00')
while True:
# Güncel tarih ve saati al
year, month, day, hour, minute, second = read_ds1307()
if second < 10:
second = "0" + str(second)
if minute < 10:
minute = "0" + str(minute)
if hour < 10:
hour = "0" + str(hour)
if day < 10:
day = "0" + str(day)
if month < 10:
month = "0" + str(month)
# Tarihi ve saati yazdır
print("Tarih: {}-{}-{}, Saat: {}:{}:{}".format(year, month, day, hour, minute, second))
utime.sleep(1)