|
1 | 1 | # Examples |
2 | 2 |
|
3 | | -Usage examples of this `micropython-package-template` library |
| 3 | +Usage examples of this `micropython-ds1307` library |
4 | 4 |
|
5 | 5 | --------------- |
6 | 6 |
|
7 | | -## TBD |
| 7 | +## General |
8 | 8 |
|
9 | | -Some text with *italic*, **bold** and `hightlighted` words. |
| 9 | +An example of all implemented functionalities can be found at the |
| 10 | +[MicroPython DS1307 examples folder][ref-micropython-ds1307-examples] |
10 | 11 |
|
11 | | -```{note} |
12 | | -A reStructuredText highlighted note |
| 12 | +## Setup DS1307 |
| 13 | + |
| 14 | +```python |
| 15 | +from eeprom import DS1307 |
| 16 | +from machine import I2C, Pin |
| 17 | + |
| 18 | +I2C_ADDR = 0x68 |
| 19 | + |
| 20 | +# define custom I2C interface, default is 'I2C(0)' |
| 21 | +# check the docs of your device for further details and pin infos |
| 22 | +# this are the pins for the Raspberry Pi Pico adapter board |
| 23 | +i2c = I2C(0, scl=Pin(13), sda=Pin(12), freq=800000) |
| 24 | +ds1307 = DS1307(addr=I2C_ADDR, i2c=i2c) # DS1307 on 0x68 |
| 25 | + |
| 26 | +# get LCD infos/properties |
| 27 | +print("DS1307 is on I2C address 0x{0:02x}".format(ds1307.addr)) |
| 28 | +print("Weekday start is {}".format(ds1307.weekday_start)) |
13 | 29 | ``` |
14 | 30 |
|
15 | | -```{eval-rst} |
16 | | -.. warning:: |
17 | | - Some eval warning |
| 31 | +## Set time |
| 32 | + |
| 33 | +Set the RTC time to the current system time |
| 34 | + |
| 35 | +```python |
| 36 | +from time import gmtime, time |
| 37 | + |
| 38 | +now = gmtime(time()) |
| 39 | +ds1307.datetime = now |
18 | 40 | ``` |
19 | 41 |
|
| 42 | +## Get time |
| 43 | + |
| 44 | +Get the current RTC time and all available properties of `DS1307` |
| 45 | + |
20 | 46 | ```python |
21 | | -def hello_world(): |
22 | | - print('Hello') |
| 47 | +# Print the date and time in ISO8601 format: 2023-04-18T21:14:22 |
| 48 | +print("Today is {:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}".format( |
| 49 | + ds1307.year, ds1307.month, ds1307.day, |
| 50 | + ds1307.hour, ds1307.minute, ds1307.second)) |
| 51 | + |
| 52 | +# check whether this year is a leap year |
| 53 | +print("Is this year a leap year? {}".format(ds1307.is_leap_year(ds1307.year))) |
| 54 | + |
| 55 | +# get the day of the year |
| 56 | +print("Today is day {} of {}".format( |
| 57 | + ds1307.day_of_year(year=ds1307.year, month=ds1307.month, day=ds1307.day), |
| 58 | + ds1307.year)) |
| 59 | +``` |
| 60 | + |
| 61 | +## Oscillator |
| 62 | + |
| 63 | +Interact with the oscillator of the RTC |
| 64 | + |
| 65 | +```python |
| 66 | +from time import sleep |
| 67 | + |
| 68 | +print("The oscillator is currently active at {}? {}".format( |
| 69 | + ds1307.datetime, ds1307.halt)) |
| 70 | +print("Halt the oscillator and wait for 5 seconds ...") |
| 71 | +ds1307.halt = True |
| 72 | +sleep(5) |
| 73 | + |
| 74 | +print("Current RTC time: {}".format(ds1307.datetime)) |
| 75 | + |
| 76 | +print("Enable the oscillator and wait for 5 seconds ...") |
| 77 | +ds1307.halt = False |
| 78 | +sleep(5) |
| 79 | +print("Current RTC time: {}".format(ds1307.datetime)) |
| 80 | +``` |
| 81 | + |
| 82 | +## Square Wave Pin |
| 83 | + |
| 84 | +Control the squarewave pin `SQ` |
| 85 | + |
| 86 | +```python |
| 87 | +from time import sleep |
| 88 | + |
| 89 | +print("Set square wave output to 1Hz and wait for 5 seconds ...") |
| 90 | +ds1307.square_wave(sqw=1) |
| 91 | +sleep(5) |
| 92 | + |
| 93 | +print("Set square wave output to 4.096kHz and wait for 5 seconds ...") |
| 94 | +ds1307.square_wave(sqw=4) |
| 95 | +sleep(5) |
| 96 | + |
| 97 | +print("Set square wave output to 8.192kHz and wait for 5 seconds ...") |
| 98 | +ds1307.square_wave(sqw=8) |
| 99 | +sleep(5) |
| 100 | + |
| 101 | +print("Set square wave output to HIGH and wait for 5 seconds ...") |
| 102 | +ds1307.square_wave(out=1) |
| 103 | +sleep(5) |
| 104 | + |
| 105 | +print("Set square wave output to LOW and wait for 5 seconds ...") |
| 106 | +ds1307.square_wave(sqw=0) |
| 107 | +sleep(5) |
23 | 108 | ``` |
24 | 109 |
|
25 | | -| Type | Individual | Prefered food | |
26 | | -| ----- | ---------- | ------------- | |
27 | | -| Car | E-Power | Electricity | |
28 | | -| Animal | Cat | Mice | |
| 110 | +[ref-micropython-ds1307-examples]: https://github.com/brainelectronics/micropython-ds1307/tree/main/examples |
0 commit comments