-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathds3231-lib.sb
More file actions
172 lines (136 loc) · 4.03 KB
/
ds3231-lib.sb
File metadata and controls
172 lines (136 loc) · 4.03 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//*****************************************************************************
// Project: RM186/BL600 DS3231 interface library
// Copyright (C) 2016 Jose Marcelino - Metavurt Ltd
//
// original Arduino code by
// Eric Ayars - 4/1/11
// Adapted to Laird smartBASIC by Jose Marcelino
//*****************************************************************************/
// Set this to the I2C address of the DS3231 (default 0x68)
dim dsSlaveAddr: dsSlaveAddr=0x68
// Convert normal binary coded decimal numbers to integer
function bcdToDec(byVal val)
val = ((val/16) * 10) + (val % 16)
endfunc val
// Convert normal decimal numbers to binary coded decimal
function decToBcd(byVal val)
val = ((val/10)*16) + (val % 10)
endfunc val
function getSeconds(byRef sec)
dim res
res = I2cReadReg8(dsSlaveAddr, 0x0, sec)
sec = bcdToDec(sec)
endfunc res
function getMinutes(byRef minutes)
dim res
res = I2cReadReg8(dsSlaveAddr, 0x1, minutes)
minutes = bcdToDec(minutes)
endfunc res
function getHour(byRef hour, byRef h12, byRef pm)
dim res, value
res = I2cReadReg8(dsSlaveAddr, 0x2, value)
h12 = value & b'01000000
if h12 > 0 then
pm = value & b'00100000
hour = bcdToDec(value & b'00011111)
else
hour = bcdToDec(value & b'00111111)
endif
endfunc res
function getDoW(byRef dow)
dim res
res = I2cReadReg8(dsSlaveAddr, 0x3, dow)
dow = bcdToDec(dow)
endfunc res
function getDate(byRef day)
dim res
res = I2cReadReg8(dsSlaveAddr, 0x4, day)
day = bcdToDec(day)
endfunc res
function getMonth(byRef month, byRef century)
dim res, value
res = I2cReadReg8(dsSlaveAddr, 0x5, value)
century = value & b'10000000
month = bcdToDec(value & b'01111111)
endfunc res
function getYear(byRef year)
dim res
res = I2cReadReg8(dsSlaveAddr, 0x6, year)
year = bcdToDec(year)
endfunc res
// Sets the seconds
function setSecond(byVal second)
dim res
res = I2cWriteReg8(dsSlaveAddr, 0x0, decToBcd(second))
// TODO: This function also resets the Oscillator Stop Flag, which is set
// whenever power is interrupted.
// Clear OSF flag
// byte temp_buffer = readControlByte(1);
// writeControlByte((temp_buffer & 0b01111111), 1);
endfunc res
// Sets the minutes
function setMinute(byVal minute)
dim res
res = I2cWriteReg8(dsSlaveAddr, 0x1, decToBcd(minute))
endfunc res
// Sets the hour, without changing 12/24h mode.
// The hour must be in 24h format.
function setHour(byVal hour)
dim res, h12, value
// Start by figuring out what the 12/24 mode is
res = I2cReadReg8(dsSlaveAddr, 0x2, value)
h12 = value & b'01000000
if h12 > 0 then
// 12 hour
if hour > 12 then
hour = decToBcd(hour-12) | b'01100000
else
hour = decToBcd(hour) & b'11011111
hour = hour | h12
endif
else
// 24 hour
hour = decToBcd(hour) & b'10111111
endif
res = I2cWriteReg8(dsSlaveAddr, 0x2, hour)
endfunc res
// Sets the Day of Week
function setDoW(byVal dow)
dim res
res = I2cWriteReg8(dsSlaveAddr, 0x3, decToBcd(dow))
endfunc res
// Sets the day of month
function setDate(byVal date)
dim res
res = I2cWriteReg8(dsSlaveAddr, 0x4, decToBcd(date))
endfunc res
// Sets the month
function setMonth(byVal month)
dim res
res = I2cWriteReg8(dsSlaveAddr, 0x5, decToBcd(month))
endfunc res
// Sets the year
function setYear(byVal year)
dim res
res = I2cWriteReg8(dsSlaveAddr, 0x6, decToBcd(year))
endfunc res
// sets the mode to 12-hour (true) or 24-hour (false).
// One thing that bothers me about how I've written this is that
// if the read and right happen at the right hourly millisecnd,
// the clock will be set back an hour. Not sure how to do it better,
// though, and as long as one doesn't set the mode frequently it's
// a very minimal risk.
// It's zero risk if you call this BEFORE setting the hour, since
// the setHour() function doesn't change this mode.
function setClockMode(byVal h12)
dim value, res
// Start by reading byte 0x02.
res = I2cReadReg8(dsSlaveAddr, 0x2, value)
// Set the flag to the requested value:
if h12 > 0 then
value = value | b'01000000
else
value = value & b'10111111
endif
res = I2cWriteReg8(dsSlaveAddr, 0x2, value)
endfunc res