|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # -*- coding: UTF-8 -*- |
3 | 3 |
|
4 | | -""" |
5 | | -Main script |
6 | | -Do your stuff here, similar to the loop() function on Arduino |
7 | | -""" |
| 4 | +"""I2C EEPROM showcase""" |
8 | 5 |
|
9 | | -from be_upy_blink import flash_led |
10 | | -from machine import Pin |
11 | | -from time import sleep |
| 6 | +from eeprom import DS1307 |
| 7 | +from machine import I2C, Pin |
| 8 | +from time import gmtime, sleep, time |
12 | 9 |
|
| 10 | +I2C_ADDR = 0x68 |
13 | 11 |
|
14 | | -def loop(): |
15 | | - # set pin D4 as output (blue LED) and turn it off |
16 | | - led_pin = Pin(4, Pin.OUT) |
17 | | - led_pin.value(0) |
| 12 | +# define custom I2C interface, default is 'I2C(0)' |
| 13 | +# check the docs of your device for further details and pin infos |
| 14 | +# this are the pins for the Raspberry Pi Pico adapter board |
| 15 | +i2c = I2C(0, scl=Pin(13), sda=Pin(12), freq=800000) |
| 16 | +ds1307 = DS1307(addr=I2C_ADDR, i2c=i2c) # DS1307 on 0x68 |
18 | 17 |
|
19 | | - # loop forever |
20 | | - while True: |
21 | | - # flash LED 3 times, being each 1 second on and 3 seconds off |
22 | | - # then sleep for 3 seconds and repeat |
23 | | - flash_led(pin=led_pin, amount=3, on_time=1, off_time=2) |
24 | | - sleep(3) |
| 18 | +# get LCD infos/properties |
| 19 | +print("DS1307 is on I2C address 0x{0:02x}".format(ds1307.addr)) |
| 20 | +print("Weekday start is {}".format(ds1307.weekday_start)) |
25 | 21 |
|
| 22 | +# get the current RTC time |
| 23 | +print("Current RTC time: {}".format(ds1307.datetime)) |
26 | 24 |
|
27 | | -# MicroPython calls every function inside this file |
28 | | -loop() |
| 25 | +# set the RTC time to the current system time |
| 26 | +now = gmtime(time()) |
| 27 | +ds1307.datetime = now |
| 28 | + |
| 29 | +# Print the date and time in ISO8601 format: 2023-04-18T21:14:22 |
| 30 | +print("Today is {:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}".format( |
| 31 | + ds1307.year, ds1307.month, ds1307.day, |
| 32 | + ds1307.hour, ds1307.minute, ds1307.second)) |
| 33 | + |
| 34 | +# check whether this year is a leap year |
| 35 | +print("Is this year a leap year? {}".format(ds1307.is_leap_year(ds1307.year))) |
| 36 | + |
| 37 | +# get the day of the year |
| 38 | +print("Today is day {} of {}".format( |
| 39 | + ds1307.day_of_year(year=ds1307.year, month=ds1307.month, day=ds1307.day), |
| 40 | + ds1307.year)) |
| 41 | + |
| 42 | +# halt the oscillator |
| 43 | +print("The oscillator is currently active at {}? {}".format( |
| 44 | + ds1307.datetime, ds1307.halt)) |
| 45 | +print("Halt the oscillator and wait for 5 seconds ...") |
| 46 | +ds1307.halt = True |
| 47 | +sleep(5) |
| 48 | + |
| 49 | +print("Current RTC time: {}".format(ds1307.datetime)) |
| 50 | + |
| 51 | +print("Enable the oscillator and wait for 5 seconds ...") |
| 52 | +ds1307.halt = False |
| 53 | +sleep(5) |
| 54 | +print("Current RTC time: {}".format(ds1307.datetime)) |
| 55 | + |
| 56 | +# control the squarewave pin SQ |
| 57 | +print("Set square wave output to 1Hz and wait for 5 seconds ...") |
| 58 | +ds1307.square_wave(sqw=1) |
| 59 | +sleep(5) |
| 60 | + |
| 61 | +print("Set square wave output to 4.096kHz and wait for 5 seconds ...") |
| 62 | +ds1307.square_wave(sqw=4) |
| 63 | +sleep(5) |
| 64 | + |
| 65 | +print("Set square wave output to 8.192kHz and wait for 5 seconds ...") |
| 66 | +ds1307.square_wave(sqw=8) |
| 67 | +sleep(5) |
| 68 | + |
| 69 | +print("Set square wave output to HIGH and wait for 5 seconds ...") |
| 70 | +ds1307.square_wave(out=1) |
| 71 | +sleep(5) |
| 72 | + |
| 73 | +print("Set square wave output to LOW and wait for 5 seconds ...") |
| 74 | +ds1307.square_wave(sqw=0) |
| 75 | +sleep(5) |
0 commit comments