-
Notifications
You must be signed in to change notification settings - Fork 21
/
ds1307.py
99 lines (86 loc) · 3.87 KB
/
ds1307.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
MicroPython TinyRTC I2C Module, DS1307 RTC + AT24C32N EEPROM
https://github.com/mcauser/micropython-tinyrtc-i2c
MIT License
Copyright (c) 2018 Mike Causer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
BCD Format:
https://en.wikipedia.org/wiki/Binary-coded_decimal
"""
from micropython import const
DATETIME_REG = const(0) # 0x00-0x06
CHIP_HALT = const(128)
CONTROL_REG = const(7) # 0x07
RAM_REG = const(8) # 0x08-0x3F
class DS1307(object):
"""Driver for the DS1307 RTC."""
def __init__(self, i2c, addr=0x68):
self.i2c = i2c
self.addr = addr
self.weekday_start = 1
self._halt = False
def _dec2bcd(self, value):
"""Convert decimal to binary coded decimal (BCD) format"""
return (value // 10) << 4 | (value % 10)
def _bcd2dec(self, value):
"""Convert binary coded decimal (BCD) format to decimal"""
return ((value >> 4) * 10) + (value & 0x0F)
def datetime(self, datetime=None):
"""Get or set datetime"""
if datetime is None:
buf = self.i2c.readfrom_mem(self.addr, DATETIME_REG, 7)
return (
self._bcd2dec(buf[6]) + 2000, # year
self._bcd2dec(buf[5]), # month
self._bcd2dec(buf[4]), # day
self._bcd2dec(buf[3] - self.weekday_start), # weekday
self._bcd2dec(buf[2]), # hour
self._bcd2dec(buf[1]), # minute
self._bcd2dec(buf[0] & 0x7F), # second
0 # subseconds
)
buf = bytearray(7)
buf[0] = self._dec2bcd(datetime[6]) & 0x7F # second, msb = CH, 1=halt, 0=go
buf[1] = self._dec2bcd(datetime[5]) # minute
buf[2] = self._dec2bcd(datetime[4]) # hour
buf[3] = self._dec2bcd(datetime[3] + self.weekday_start) # weekday
buf[4] = self._dec2bcd(datetime[2]) # day
buf[5] = self._dec2bcd(datetime[1]) # month
buf[6] = self._dec2bcd(datetime[0] - 2000) # year
if (self._halt):
buf[0] |= (1 << 7)
self.i2c.writeto_mem(self.addr, DATETIME_REG, buf)
def halt(self, val=None):
"""Power up, power down or check status"""
if val is None:
return self._halt
reg = self.i2c.readfrom_mem(self.addr, DATETIME_REG, 1)[0]
if val:
reg |= CHIP_HALT
else:
reg &= ~CHIP_HALT
self._halt = bool(val)
self.i2c.writeto_mem(self.addr, DATETIME_REG, bytearray([reg]))
def square_wave(self, sqw=0, out=0):
"""Output square wave on pin SQ at 1Hz, 4.096kHz, 8.192kHz or 32.768kHz,
or disable the oscillator and output logic level high/low."""
rs0 = 1 if sqw == 4 or sqw == 32 else 0
rs1 = 1 if sqw == 8 or sqw == 32 else 0
out = 1 if out > 0 else 0
sqw = 1 if sqw > 0 else 0
reg = rs0 | rs1 << 1 | sqw << 4 | out << 7
self.i2c.writeto_mem(self.addr, CONTROL_REG, bytearray([reg]))