|
| 1 | +# Simple demo of reading and writing the time for the DS1307 real-time clock. |
| 2 | +# Change the if False to if True below to set the time, otherwise it will just |
| 3 | +# print the current date and time every second. Notice also comments to adjust |
| 4 | +# for working with hardware vs. software I2C. |
| 5 | + |
| 6 | +import time |
| 7 | +import board |
| 8 | +# For hardware I2C (M0 boards) use this line: |
| 9 | +import busio as io |
| 10 | +# Or for software I2C (ESP8266) use this line instead: |
| 11 | +#import bitbangio as io |
| 12 | + |
| 13 | +import adafruit_ds1307 |
| 14 | + |
| 15 | +# Change to the appropriate I2C clock & data pins here! |
| 16 | +i2c_bus = io.I2C(board.SCL, board.SDA) |
| 17 | + |
| 18 | +# Create the RTC instance: |
| 19 | +rtc = adafruit_ds1307.DS1307(i2c_bus) |
| 20 | + |
| 21 | +# Lookup table for names of days (nicer printing). |
| 22 | +days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") |
| 23 | + |
| 24 | + |
| 25 | +#pylint: disable-msg=bad-whitespace |
| 26 | +#pylint: disable-msg=using-constant-test |
| 27 | +if False: # change to True if you want to set the time! |
| 28 | + # year, mon, date, hour, min, sec, wday, yday, isdst |
| 29 | + t = time.struct_time((2017, 10, 29, 15, 14, 15, 0, -1, -1)) |
| 30 | + # you must set year, mon, date, hour, min, sec and weekday |
| 31 | + # yearday is not supported, isdst can be set but we don't do anything with it at this time |
| 32 | + print("Setting time to:", t) # uncomment for debugging |
| 33 | + rtc.datetime = t |
| 34 | + print() |
| 35 | +#pylint: enable-msg=using-constant-test |
| 36 | +#pylint: enable-msg=bad-whitespace |
| 37 | + |
| 38 | +# Main loop: |
| 39 | +while True: |
| 40 | + t = rtc.datetime |
| 41 | + #print(t) # uncomment for debugging |
| 42 | + print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year)) |
| 43 | + print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec)) |
| 44 | + time.sleep(1) # wait a second |
0 commit comments