-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathds1302.ts
345 lines (316 loc) · 9.91 KB
/
ds1302.ts
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* makecode DS1302 RTC Package.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
/**
* DS1302 block
*/
//% weight=100 color=#A040E0 icon="\uf017" block="RTC DS1302"
namespace DS1302 {
let DS1302_REG_SECOND = 0x80
let DS1302_REG_MINUTE = 0x82
let DS1302_REG_HOUR = 0x84
let DS1302_REG_DAY = 0x86
let DS1302_REG_MONTH = 0x88
let DS1302_REG_WEEKDAY = 0x8A
let DS1302_REG_YEAR = 0x8C
let DS1302_REG_WP = 0x8E
let DS1302_REG_CTRL = 0x90
let DS1302_REG_RAM = 0xC0
/**
* convert a Hex data to Dec
*/
function HexToDec(dat: number): number {
return (dat >> 4) * 10 + (dat % 16);
}
/**
* convert a Dec data to Hex
*/
function DecToHex(dat: number): number {
return Math.idiv(dat, 10) * 16 + (dat % 10)
}
/**
* DS1302 RTC class
*/
export class DS1302RTC {
clk: DigitalPin;
dio: DigitalPin;
cs: DigitalPin;
/**
* write a byte to DS1302
*/
write_byte(dat: number) {
for (let i = 0; i < 8; i++) {
pins.digitalWritePin(this.dio, (dat >> i) & 1);
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
}
/**
* read a byte from DS1302
*/
read_byte(): number {
let d = 0;
for (let i = 0; i < 8; i++) {
d = d | (pins.digitalReadPin(this.dio) << i);
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
return d;
}
/**
* read reg
*/
getReg(reg: number): number {
let t = 0;
pins.digitalWritePin(this.cs, 1);
this.write_byte(reg);
t = this.read_byte();
pins.digitalWritePin(this.cs, 0);
return t;
}
/**
* write reg
*/
setReg(reg: number, dat: number) {
pins.digitalWritePin(this.cs, 1);
this.write_byte(reg);
this.write_byte(dat);
pins.digitalWritePin(this.cs, 0);
}
/**
* write reg with WP protect
*/
wr(reg: number, dat: number) {
this.setReg(DS1302_REG_WP, 0)
this.setReg(reg, dat)
this.setReg(DS1302_REG_WP, 0)
}
/**
* get Year
*/
//% blockId="DS1302_get_year" block="%ds|get year"
//% weight=80 blockGap=8
//% parts="DS1302"
getYear(): number {
return Math.min(HexToDec(this.getReg(DS1302_REG_YEAR + 1)), 99) + 2000
}
/**
* set year
* @param dat is the Year will be set, eg: 2018
*/
//% blockId="DS1302_set_year" block="%ds|set year %dat"
//% weight=81 blockGap=8
//% parts="DS1302"
setYear(dat: number): void {
this.wr(DS1302_REG_YEAR, DecToHex(dat % 100))
}
/**
* get Month
*/
//% blockId="DS1302_get_month" block="%ds|get month"
//% weight=78 blockGap=8
//% parts="DS1302"
getMonth(): number {
return Math.max(Math.min(HexToDec(this.getReg(DS1302_REG_MONTH + 1)), 12), 1)
}
/**
* set month
* @param dat is Month will be set. eg: 2
*/
//% blockId="DS1302_set_month" block="%ds|set month %dat"
//% weight=79 blockGap=8
//% parts="DS1302"
//% dat.min=1 dat.max=12
setMonth(dat: number): void {
this.wr(DS1302_REG_MONTH, DecToHex(dat % 13))
}
/**
* get Day
*/
//% blockId="DS1302_get_day" block="%ds|get day"
//% weight=76 blockGap=8
//% parts="DS1302"
getDay(): number {
return Math.max(Math.min(HexToDec(this.getReg(DS1302_REG_DAY + 1)), 31), 1)
}
/**
* set day
* @param dat is the Day will be set, eg: 15
*/
//% blockId="DS1302_set_day" block="%ds|set day %dat"
//% weight=77 blockGap=8
//% parts="DS1302"
//% dat.min=1 dat.max=31
setDay(dat: number): void {
this.wr(DS1302_REG_DAY, DecToHex(dat % 32))
}
/**
* get Week Day
*/
//% blockId="DS1302_get_weekday" block="%ds|get weekday"
//% weight=74 blockGap=8
//% parts="DS1302"
getWeekday(): number {
return Math.max(Math.min(HexToDec(this.getReg(DS1302_REG_WEEKDAY + 1)), 7), 1)
}
/**
* set weekday
* @param dat is the Week Day will be set, eg: 4
*/
//% blockId="DS1302_set_weekday" block="%ds|set weekday %dat"
//% weight=75 blockGap=8
//% parts="DS1302"
//% dat.min=1 dat.max=7
setWeekday(dat: number): void {
this.wr(DS1302_REG_WEEKDAY, DecToHex(dat % 8))
}
/**
* get Hour
*/
//% blockId="DS1302_get_hour" block="%ds|get hour"
//% weight=72 blockGap=8
//% parts="DS1302"
getHour(): number {
return Math.min(HexToDec(this.getReg(DS1302_REG_HOUR + 1)), 23)
}
/**
* set hour
* @param dat is the Hour will be set, eg: 0
*/
//% blockId="DS1302_set_hour" block="%ds|set hour %dat"
//% weight=73 blockGap=8
//% parts="DS1302"
//% dat.min=0 dat.max=23
setHour(dat: number): void {
this.wr(DS1302_REG_HOUR, DecToHex(dat % 24))
}
/**
* get Minute
*/
//% blockId="DS1302_get_minute" block="%ds|get minute"
//% weight=72 blockGap=8
//% parts="DS1302"
getMinute(): number {
return Math.min(HexToDec(this.getReg(DS1302_REG_MINUTE + 1)), 59)
}
/**
* set minute
* @param dat is the Minute will be set, eg: 0
*/
//% blockId="DS1302_set_minute" block="%ds|set minute %dat"
//% weight=71 blockGap=8
//% parts="DS1302"
//% dat.min=0 dat.max=59
setMinute(dat: number): void {
this.wr(DS1302_REG_MINUTE, DecToHex(dat % 60))
}
/**
* get Second
*/
//% blockId="DS1302_get_second" block="%ds|get second"
//% weight=70 blockGap=8
//% parts="DS1302"
getSecond(): number {
return Math.min(HexToDec(this.getReg(DS1302_REG_SECOND + 1)), 59)
}
/**
* set second
* @param dat is the Second will be set, eg: 0
*/
//% blockId="DS1302_set_second" block="%ds|set second %dat"
//% weight=69 blockGap=8
//% parts="DS1302"
//% dat.min=0 dat.max=59
setSecond(dat: number): void {
this.wr(DS1302_REG_SECOND, DecToHex(dat % 60))
}
/**
* set Date and Time
* @param year is the Year will be set, eg: 2018
* @param month is the Month will be set, eg: 2
* @param day is the Day will be set, eg: 15
* @param weekday is the Weekday will be set, eg: 4
* @param hour is the Hour will be set, eg: 0
* @param minute is the Minute will be set, eg: 0
* @param second is the Second will be set, eg: 0
*/
//% blockId="DS1302_set_DateTime" block="%ds|set Date and Time: Year %year|Month %month|Day %day|WeekDay %weekday|Hour %hour|Minute %minute|Second %second"
//% weight=50 blockGap=8
//% parts="DS1302"
//% year.min=2000 year.max=2100
//% month.min=1 month.max=12
//% day.min=1 day.max=31
//% weekday.min=1 weekday.max=7
//% hour.min=0 hour.max=23
//% minute.min=0 minute.max=59
//% second.min=0 second.max=59
DateTime(year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number): void {
this.setYear(year);
this.setMonth(month);
this.setDay(day);
this.setWeekday(weekday);
this.setHour(hour);
this.setMinute(minute);
this.setSecond(second);
}
/**
* start ds1302 RTC (go on)
*/
//% blockId="DS1302_start" block="%ds|start RTC"
//% weight=41 blockGap=8
//% parts="DS1302"
start() {
let t = this.getSecond()
this.setSecond(t & 0x7f)
}
/**
* pause ds1302 RTC
*/
//% blockId="DS1302_pause" block="%ds|pause RTC"
//% weight=40 blockGap=8
//% parts="DS1302"
pause() {
let t = this.getSecond()
this.setSecond(t | 0x80)
}
/**
* read RAM
*/
//% blockId="DS1302_read_ram" block="%ds|read ram %reg"
//% weight=43 blockGap=8
//% parts="DS1302"
//% reg.min=0 reg.max=30
readRam(reg: number): number {
return this.getReg(DS1302_REG_RAM + 1 + (reg % 31) * 2)
}
/**
* write RAM
*/
//% blockId="DS1302_write_ram" block="%ds|write ram %reg|with %dat"
//% weight=42 blockGap=8
//% parts="DS1302"
//% reg.min=0 reg.max=30
writeRam(reg: number, dat: number) {
this.wr(DS1302_REG_RAM + (reg % 31) * 2, dat)
}
}
/**
* create a DS1302 object.
* @param clk the CLK pin for DS1302, eg: DigitalPin.P13
* @param dio the DIO pin for DS1302, eg: DigitalPin.P14
* @param cs the CS pin for DS1302, eg: DigitalPin.P15
*/
//% weight=200 blockGap=8
//% blockId="DS1302_create" block="CLK %clk|DIO %dio|CS %cs"
export function create(clk: DigitalPin, dio: DigitalPin, cs: DigitalPin): DS1302RTC {
let ds = new DS1302RTC();
ds.clk = clk;
ds.dio = dio;
ds.cs = cs;
pins.digitalWritePin(ds.clk, 0);
pins.digitalWritePin(ds.cs, 0);
return ds;
}
}