Skip to content

Instantly share code, notes, and snippets.

@neosarchizo
Last active November 14, 2022 07:53
Show Gist options
  • Save neosarchizo/631a51e811e93f429f51beb49478cca2 to your computer and use it in GitHub Desktop.
Save neosarchizo/631a51e811e93f429f51beb49478cca2 to your computer and use it in GitHub Desktop.
MicroPython - RTC 제어
from machine import Pin, SoftI2C, RTC, Timer
from ssd1306 import SSD1306_I2C
WEEK = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Mon ~ Sun : 0 ~ 6
# ex) 01:20:05
pads = ['', '', '']
i2c = SoftI2C(sda=Pin(13), scl=Pin(14))
display = SSD1306_I2C(128, 64, i2c, addr=0x3C)
rtc = RTC()
# rtc.datetime()
# year, month, day, hour, minute, second
if rtc.datetime()[0] != 2022:
rtc.datetime((2022, 11, 14, 16, 25, 0, 0, 0))
def on_timeout(_):
datetime = rtc.datetime()
display.fill(0)
display.text('DeviceMart', 0, 0)
display.text('RTC Clock', 0, 15)
display.text(
str(datetime[0]) +
'-' +
str(datetime[1]) +
'-' +
str(datetime[2]) +
' ' +
WEEK[datetime[3]],
0,
40
)
for i in range(4, 7): # 4 ~ 6 : pads for hours, mins, secs
if datetime[i] < 10:
pads[i - 4] = '0'
else:
pads[i - 4] = ''
display.text(
pads[0] +
str(datetime[4]) +
':' +
pads[1] +
str(datetime[5]) +
':' +
pads[2] +
str(datetime[6]),
0,
55
)
display.show()
timer = Timer(-1)
timer.init(period=300, mode=Timer.PERIODIC, callback=on_timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment