forked from micropython-Chinese-Community/mpy-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS1307.py
129 lines (108 loc) · 3.85 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'''
DS1307 RTC drive
Author: shaoziyang
Date: 2018.3
http://www.micropython.org.cn
'''
from machine import I2C, Pin
DS1307_I2C_ADDRESS = (104)
DS1307_REG_SECOND = (0)
DS1307_REG_MINUTE = (1)
DS1307_REG_HOUR = (2)
DS1307_REG_WEEKDAY = (3)
DS1307_REG_DAY = (4)
DS1307_REG_MONTH = (5)
DS1307_REG_YEAR = (6)
DS1307_REG_CTRL = (7)
DS1307_REG_RAM = (8)
class DS1307():
def __init__(self, i2c):
self.i2c = i2c
# set reg
def setReg(self, reg, dat):
buf = bytearray(2)
buf[0] = reg
buf[1] = dat
self.i2c.writeto(DS1307_I2C_ADDRESS, buf)
# get reg
def getReg(self, reg):
buf = bytearray(1)
buf[0] = reg
self.i2c.writeto(DS1307_I2C_ADDRESS, buf)
t = self.i2c.readfrom(DS1307_I2C_ADDRESS, 1)
return t[0]
def start(self):
t = self.getReg(DS1307_REG_SECOND)
self.setReg(DS1307_REG_SECOND, t&0x7F)
def stop(self):
t = self.getReg(DS1307_REG_SECOND)
self.setReg(DS1307_REG_SECOND, t|0x80)
def DecToHex(self, dat):
return (dat//10) * 16 + (dat%10)
def HexToDec(self, dat):
return (dat//16) * 10 + (dat%16)
def DateTime(self, DT=None):
if DT == None:
self.i2c.writeto(DS1307_I2C_ADDRESS, bytearray([0]))
buf = self.i2c.readfrom(DS1307_I2C_ADDRESS, 7)
DT = [0] * 8
DT[0] = self.HexToDec(buf[6]) + 2000
DT[1] = self.HexToDec(buf[5])
DT[2] = self.HexToDec(buf[4])
DT[3] = self.HexToDec(buf[3])
DT[4] = self.HexToDec(buf[2])
DT[5] = self.HexToDec(buf[1])
DT[6] = self.HexToDec(buf[0])
DT[7] = 0
return DT
else:
buf = bytearray(8)
buf[0] = 0
buf[1] = self.DecToHex(DT[6]%60) # second
buf[2] = self.DecToHex(DT[5]%60) # minute
buf[3] = self.DecToHex(DT[4]%24) # hour
buf[4] = self.DecToHex(DT[3]%8) # week day
buf[5] = self.DecToHex(DT[2]%32) # date
buf[6] = self.DecToHex(DT[1]%13) # month
buf[7] = self.DecToHex(DT[0]%100) # year
self.i2c.writeto(DS1307_I2C_ADDRESS, buf)
def Year(self, year = None):
if year == None:
return self.HexToDec(self.getReg(DS1307_REG_YEAR)) + 2000
else:
self.setReg(DS1307_REG_YEAR, self.DecToHex(year%100))
def Month(self, month = None):
if month == None:
return self.HexToDec(self.getReg(DS1307_REG_MONTH))
else:
self.setReg(DS1307_REG_MONTH, self.DecToHex(month%13))
def Day(self, day = None):
if day == None:
return self.HexToDec(self.getReg(DS1307_REG_DAY))
else:
self.setReg(DS1307_REG_DAY, self.DecToHex(day%32))
def Weekday(self, weekday = None):
if weekday == None:
return self.HexToDec(self.getReg(DS1307_REG_WEEKDAY))
else:
self.setReg(DS1307_REG_WEEKDAY, self.DecToHex(weekday%8))
def Hour(self, hour = None):
if hour == None:
return self.HexToDec(self.getReg(DS1307_REG_HOUR))
else:
self.setReg(DS1307_REG_HOUR, self.DecToHex(hour%24))
def Minute(self, minute = None):
if minute == None:
return self.HexToDec(self.getReg(DS1307_REG_MINUTE))
else:
self.setReg(DS1307_REG_MINUTE, self.DecToHex(minute%60))
def Second(self, second = None):
if second == None:
return self.HexToDec(self.getReg(DS1307_REG_SECOND))
else:
self.setReg(DS1307_REG_SECOND, self.DecToHex(second%60))
def ram(self, reg, dat = None):
if dat == None:
return self.getReg(DS1307_REG_RAM + (reg%56))
else:
self.setReg(DS1307_REG_RAM + (reg%56), dat)