forked from LairdCP/RM1xx-Applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP79410.sblib
589 lines (521 loc) · 25.8 KB
/
MCP79410.sblib
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//******************************************************************************
// Copyright (c) 2017, Adam Pierce
// Modified by Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filename ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// MCP79410 RTC library, requires the RTC I2C pins to be connected to the RM1xx
// (SIO_29 - SCL, SIO_30 - SDA) with pull-up resistors on both pins (~2.2K Ohm).
// Optionally, the alarm pin can also be connected to the RM1xx.
//
// Port of Sensorian RTC_RTCC Driver to smartBASIC
// source: https://github.com/sensorian/sensorian-firmware
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#define RTC_ADDRESS 0x6f // I2C address of RTC
#define RTC_I2CSpeed 100000 // I2C bus speed when communicting with RTC
//***************************** RTCC Memory map ********************************
#define EEPROM_WRITE 0xae // DEVICE ADDR for EEPROM (writes)
#define EEPROM_READ 0xaf // DEVICE ADDR for EEPROM (reads)
#define RTCC_WRITE 0xde // DEVICE ADDR for RTCC MCHP (writes)
#define RTCC_READ 0xdf // DEVICE ADDR for RTCC MCHP (reads)
#define SRAM_PTR 0x20 // Pointer of the SRAM area (RTCC)
#define EEPROM_SR 0xff // STATUS register in the EEPROM
#define SEC 0x00 // Address of register (SECONDS)
#define MIN 0x01 // Address of register (MINUTES)
#define HOUR 0x02 // Address of register (HOURS)
#define DAY 0x03 // Address of register (DAY OF WEEK)
#define STAT 0x03 // Address of register (STATUS)
#define DATE 0x04 // Address of register (DATE)
#define MNTH 0x05 // Address of register (MONTH)
#define YEAR 0x06 // Address of register (YEAR)
#define CTRL 0x07 // Address of register (CONTROL)
#define CAL 0x08 // Address of register (CALIBRATION)
#define ULID 0x09 // Address of register (UNLOCK ID)
#define ALM0SEC 0x0a // Address of register (ALARM0 SECONDS)
#define ALM0MIN 0x0b // Address of register (ALARM0 MINUTES)
#define ALM0HR 0x0c // Address of register (ALARM0 HOURS)
#define ALM0WDAY 0x0d // Address of register (ALARM0 CONTROL)
#define ALM0DATE 0x0e // Address of register (ALARM0 DATE)
#define ALM0MTH 0x0f // Address of register (ALARM0 MONTH)
#define ALM1SEC 0x11 // Address of register (ALARM1 SECONDS)
#define ALM1MIN 0x12 // Address of register (ALARM1 MINUTES)
#define ALM1HR 0x13 // Address of register (ALARM1 HOURS)
#define ALM1WDAY 0x14 // Address of register (ALARM1 CONTROL)
#define ALM1DATE 0x15 // Address of register (ALARM1 DATE)
#define ALM1MTH 0x16 // Address of register (ALARM1 MONTH)
#define PWRDNMIN 0x18 // Address of T_SAVER MIN(VDD->BAT)
#define PWRDNHOUR 0x19 // Address of T_SAVER HR (VDD->BAT)
#define PWRDNDATE 0x1a // Address of T_SAVER DAT(VDD->BAT)
#define PWRDNMTH 0x1b // Address of T_SAVER MTH(VDD->BAT)
#define PWRUPMIN 0x1c // Address of T_SAVER MIN(BAT->VDD)
#define PWRUPHOUR 0x1d // Address of T_SAVER HR (BAT->VDD)
#define PWRUPDATE 0x1e // Address of T_SAVER DAT(BAT->VDD)
#define PWRUPMTH 0x1f // Address of T_SAVER MTH(BAT->VDD)
//********************** GLOBAL CONSTANTS RTCC - Initialisation ****************
#define PM 0x20 // Post-meridian bit (HOUR)
#define HOUR_FORMAT 0x40 // Hour format
#define OUT_PIN 0x80 // = b7 (CTRL)
#define SQWEN 0x40 // SQWE = b6 (CTRL)
#define ALM_NO 0x00 // No alarm activated (CTRL)
#define ALM_0 0x10 // ALARM0 is activated (CTRL)
#define ALM_1 0x20 // ALARM1 is activated (CTRL)
#define ALM_01 0x30 // Both alarms are activated (CTRL)
#define MFP_01H 0x00 // MFP = SQVAW(01 HERZ) (CTRL)
#define MFP_04K 0x01 // MFP = SQVAW(04 KHZ) (CTRL)
#define MFP_08K 0x02 // MFP = SQVAW(08 KHZ) (CTRL)
#define MFP_32K 0x03 // MFP = SQVAW(32 KHZ) (CTRL)
#define MFP_64H 0x04 // MFP = SQVAW(64 HERZ) (CTRL)
#define EXTOSC 0x08 // MFP = SQVAW(64 HERZ) (CTRL)
#define ALMx_POL 0x80 // Polarity of MFP on alarm (ALMxCTL)
#define ALMxC_SEC 0x00 // ALARM compare on SEC (ALMxCTL)
#define ALMxC_MIN 0x10 // ALARM compare on MIN (ALMxCTL)
#define ALMxC_HR 0x20 // ALARM compare on HOUR (ALMxCTL)
#define ALMxC_DAY 0x30 // ALARM compare on DAY (ALMxCTL)
#define ALMxC_DAT 0x40 // ALARM compare on DATE (ALMxCTL)
#define ALMxC_ALL 0x70 // ALARM compare on all param(ALMxCTL)
#define ALMx_IF 0x08 // MASK of the ALARM_IF (ALMxCTL)
#define OSCRUN 0x20 // State of the oscillator (running or not)
#define PWRFAIL 0x10
#define VBATEN 0x08 // Enable backup battery
#define VBAT_DIS 0x37 // Disable backup battery
#define START_32KHZ 0x80 // Start crystal: ST = b7 (SEC)
#define LP 0x20 // Mask for the leap year bit (MONTH REG)
#define HOUR_12 0x40 // 12 hours format (HOUR)
#define LPYR 0x20 // Leap year bit
//******************************************************************************
#define ALM1MSK2 0x40 // Alarm 1 3rd mask bit
#define ALM1MSK1 0x20 // Alarm 1 2nd mask bit
#define ALM1MSK0 0x10 // Alarm 1 1st mask bit
#define ALM0MSK2 0x40 // Alarm 0 3rd mask bit
#define ALM0MSK1 0x20 // Alarm 0 2nd mask bit
#define ALM0MSK0 0x10 // Alarm 0 1st mask bit
//******************************************************************************
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
DIM RTC_rc
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
SUB RTC_Write(rtcc_reg, time_var)
//Used to write data to the RTC
DIM RTC_I2Ch
RTC_rc = I2cOpen(RTC_I2CSpeed, 0, RTC_I2Ch)
RTC_rc = I2cWriteReg8(RTC_ADDRESS, rtcc_reg, time_var)
I2cClose(RTC_I2Ch)
ENDSUB
//==============================================================================
//==============================================================================
FUNCTION RTC_Read(rtcc_reg)
//Used to read data from the RTC
DIM RTC_I2Ch, value
RTC_rc = I2cOpen(RTC_I2CSpeed, 0, RTC_I2Ch)
RTC_rc = I2cReadReg8(RTC_ADDRESS, rtcc_reg, value)
I2cClose(RTC_I2Ch)
ENDFUNC value
//==============================================================================
//==============================================================================
FUNCTION RTC_bcd2dec(byVal val)
//Converts a BCD (binary-coded decimal) number to decimal
val = ((val/16) * 10) + (val % 16)
endfunc val
//==============================================================================
//==============================================================================
FUNCTION RTC_dec2bcd(byVal val)
//Converts a decimal number to BCD (binary-coded decimal)
val = ((val/10)*16) + (val % 10)
ENDFUNC val
//==============================================================================
//==============================================================================
SUB RTC_EnableOsc()
//Enables the RTC oscillator
DIM ST_bit : ST_bit = RTC_Read(DAY) //Read day + OSCON bit
ST_bit = ST_bit | START_32KHZ
RTC_Write(SEC, ST_bit) //START bit is located in the Sec register
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_DisableOsc()
//Disables the RTC oscillator
DIM ST_bit : ST_bit = RTC_Read(DAY) //Read day + OSCON bit
ST_bit = ST_bit & ~START_32KHZ
RTC_Write(SEC, ST_bit) //START bit is located in the Sec register
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetHourFormat(format)
//Sets the hour format to 12 or 24 on the RTC
RTC_DisableOsc() //Diable clock
DIM Format_bit : Format_bit = RTC_Read(HOUR) //Read hour format bit
IF (format == 0) THEN
Format_bit = Format_bit & ~HOUR_FORMAT //Set format to H12 (military)
ELSE
Format_bit = Format_bit | HOUR_FORMAT //Set format to H12
ENDIF
RTC_Write(HOUR, Format_bit) //START bit is located in the Sec register
RTC_EnableOsc() //Enable clock
ENDSUB
//==============================================================================
//==============================================================================
FUNCTION RTC_IsRunning()
//Returns 1 if the RTC is running, otherwise returns 0
DIM Mask : Mask = RTC_Read(DAY)
IF ((Mask & OSCRUN) == OSCRUN) THEN //If oscillator = already running, do nothing.
EXITFUNC 1
ENDIF
ENDFUNC 0
//==============================================================================
//==============================================================================
SUB RTC_GetTime(ByRef tsec, ByRef tmin, ByRef thour, ByRef tweekday, ByRef tdate, ByRef tmonth, ByRef tyear)
//Gets the date and time from the RTC
tsec = RTC_bcd2dec(RTC_Read(SEC) & (~START_32KHZ))
tmin = RTC_bcd2dec(RTC_Read(MIN))
DIM hour_t : hour_t = RTC_Read(HOUR)
IF ((hour_t & HOUR_12) == HOUR_12) THEN //Hour is in 24 hour format
hour_t = (hour_t & 0x1F)
ELSE
hour_t = (hour_t & 0x3F)
ENDIF
thour = RTC_bcd2dec(hour_t)
tweekday = RTC_bcd2dec(RTC_Read(DAY) & ~(OSCRUN|PWRFAIL|VBATEN))
tdate = RTC_bcd2dec(RTC_Read(DATE))
tmonth = RTC_bcd2dec(RTC_Read(MNTH) & ~(LPYR))
tyear = RTC_bcd2dec(RTC_Read(YEAR))
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetTime(tsec, tmin, thour, tweekday, tdate, tmonth, tyear)
//Sets the RTC date and time
DIM sec : sec = RTC_Read(SEC) //Seconds
DIM mini : mini = 0 //Minutes
DIM hour : hour = RTC_Read(HOUR) //Hours
DIM weekday : weekday = RTC_Read(DAY) //Weekday
DIM date : date = 0 //Date
DIM month : month = RTC_Read(MNTH) //Month
DIM year : year = 0 //Year
IF ((sec & START_32KHZ) == START_32KHZ) THEN //Seconds register
sec = RTC_dec2bcd(tsec)| START_32KHZ
ELSE
sec = RTC_dec2bcd(tsec)
ENDIF
mini = RTC_dec2bcd(tmin) //Minutes
IF ((hour & HOUR_12) == HOUR_12) THEN //Hour register
hour = RTC_dec2bcd(thour) | HOUR_12
ELSE
hour = RTC_dec2bcd(thour)
ENDIF
IF ((hour & PM) == PM) THEN
hour = hour | PM
ENDIF
weekday = weekday & 0x38 //Mask 3 upper bits
weekday = weekday | RTC_dec2bcd(tweekday) //Weekday
date = RTC_dec2bcd(tdate) //Date
IF ((month & LPYR) == LPYR) THEN //Month
month = RTC_dec2bcd(tmonth) | LPYR
ELSE
month = RTC_dec2bcd(tmonth)
ENDIF
year = RTC_dec2bcd(tyear) //Year
RTC_Write(SEC, sec)
RTC_Write(MIN, mini)
RTC_Write(HOUR, hour)
RTC_Write(DAY, weekday)
RTC_Write(DATE, date)
RTC_Write(MNTH, month)
RTC_Write(YEAR, year)
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetPMAM(meridian)
//Sets current time period (PM or AM) on RTC
RTC_DisableOsc() //Diable clock
DIM PMAM_bit : PMAM_bit = RTC_Read(HOUR) //Read meridian bit
if (meridian == 0) THEN
PMAM_bit = PMAM_bit & ~PM //Set AM
ELSE
PMAM_bit = PMAM_bit | PM //Set PM
ENDIF
RTC_Write(HOUR, PMAM_bit) //Update PM/AM meridian bit
RTC_EnableOsc() //Enable clock
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_EnableAlarm(alarm)
//Enables an RTC alarm
DIM ctrl_bits : ctrl_bits = RTC_Read(CTRL)
IF (alarm == 0) THEN
ctrl_bits = ctrl_bits | ALM_0
RTC_Write(CTRL, ctrl_bits)
ELSE
ctrl_bits = ctrl_bits | ALM_1
RTC_Write(CTRL, ctrl_bits)
ENDIF
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_EnableExOsc()
//Enables the external oscillator
DIM ctrl_bits : ctrl_bits = RTC_Read(CTRL)
ctrl_bits = ctrl_bits | EXTOSC
RTC_Write(CTRL, ctrl_bits)
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_DisableExOsc()
//Disables the external oscillator
DIM ctrl_bits : ctrl_bits = RTC_Read(CTRL)
ctrl_bits = ctrl_bits & ~EXTOSC
RTC_Write(CTRL, ctrl_bits)
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_DisableAlarm(alarm)
//Disables an RTC alarm
DIM ctrl_bits : ctrl_bits = RTC_Read(CTRL)
IF (alarm == 0) THEN
ctrl_bits = ctrl_bits & ~ALM_0
RTC_Write(CTRL, ctrl_bits)
ELSE
ctrl_bits = ctrl_bits & ~ALM_1
RTC_Write(CTRL, ctrl_bits)
ENDIF
ENDSUB
//==============================================================================
//==============================================================================
FUNCTION RTC_GetAlarmStatus(alarm)
//Returns the status of an RTC alarm, 1 = active, 0 = inactive
DIM temp
if(alarm == 0) THEN
temp = RTC_Read(ALM0WDAY) //Read WKDAY register for Alarm 0
ELSE
temp = RTC_Read(ALM1WDAY) //Read WKDAY register for Alarm 1
ENDIF
IF ((temp & ALMx_IF) == ALMx_IF) THEN
EXITFUNC 1
ENDIF
ENDFUNC 0
//==============================================================================
//==============================================================================
SUB RTC_ClearInterrupt(alarm)
//Clears an RTC alarm interrupt
DIM temp
if (alarm == 0) THEN
temp = RTC_Read(ALM0WDAY) //Read WKDAY register for Alarm 0
temp = temp & (~ALMx_IF) //Clear 4th bit
RTC_Write(ALM0WDAY, temp) //Enable backup battery mode
ELSE
temp = RTC_Read(ALM1WDAY) //Read WKDAY register for Alarm 1
temp = temp & (~ALMx_IF) //Clear 4th bit
RTC_Write(ALM1WDAY, temp) //Enable backup battery mode
ENDIF
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetAlarmTime(tsec, tmin, thour, tweekday, tdate, tmonth, alarm)
//Sets the time and date for an RTC alarm
DIM sec : sec = RTC_dec2bcd(tsec)
DIM mini : mini = RTC_dec2bcd(tmin)
DIM hour : hour = RTC_dec2bcd(thour)
DIM weekday : weekday = RTC_dec2bcd(tweekday)
DIM date : date = RTC_dec2bcd(tdate)
DIM month : month = RTC_dec2bcd(tmonth)
IF (alarm == 0) THEN
//Alarm 0
RTC_Write(ALM0SEC, sec|START_32KHZ)
RTC_Write(ALM0MIN, mini)
RTC_Write(ALM0HR, hour)
RTC_Write(ALM0WDAY, weekday)
RTC_Write(ALM0DATE, date)
RTC_Write(ALM0MTH, month)
ELSE
//Alarm 1
RTC_Write(ALM1SEC, sec|START_32KHZ)
RTC_Write(ALM1MIN, mini)
RTC_Write(ALM1HR, hour)
RTC_Write(ALM1WDAY, weekday)
RTC_Write(ALM1DATE, date)
RTC_Write(ALM1MTH, month)
ENDIF
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetAlarmMFPPol(MFP_pol, alarm)
//Sets the RTC alarm pin polarity
DIM Polarity_bit : Polarity_bit = 0
IF (alarm == 0) THEN
Polarity_bit = RTC_Read(ALM0WDAY) //Read hour format bit
ELSE
Polarity_bit = RTC_Read(ALM1WDAY) //Read hour format bit
ENDIF
IF (MFP_pol == 0) THEN
Polarity_bit = Polarity_bit & ~ALMx_POL //Set MFP LOW
ELSE
Polarity_bit = Polarity_bit | ALMx_POL //Set MFP HIGH
ENDIF
if (alarm == 0) THEN
RTC_Write(ALM0WDAY, Polarity_bit) //Update polarity bit for Alarm 0
ELSE
RTC_Write(ALM1WDAY, Polarity_bit) //Update polarity bit for Alarm 1
ENDIF
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetAlarmMatch(match, alarm)
//Sets the RTC alarm match
DIM AlarmRegister : AlarmRegister = 0
IF (alarm == 0) THEN
AlarmRegister = ALM0WDAY
ELSE
AlarmRegister = ALM1WDAY
ENDIF
DIM match_bits : match_bits = RTC_Read(AlarmRegister)
SELECT match
case 0
match_bits = match_bits & ~(ALM0MSK2|ALM0MSK1|ALM0MSK0)
RTC_Write(AlarmRegister ,match_bits) //Minutes match
case 1
match_bits = match_bits | ALM0MSK0
RTC_Write(AlarmRegister, match_bits) //Minutes match
case 2
match_bits = match_bits | ALM0MSK1
RTC_Write(AlarmRegister, match_bits) //Hours match
case 3
match_bits = match_bits | (ALM0MSK1|ALM0MSK0)
RTC_Write(AlarmRegister, match_bits) //Day of week match
case 4
match_bits = ALM0MSK2
RTC_Write(AlarmRegister, match_bits) //Date match
case 5
match_bits = match_bits | (ALM0MSK2|ALM0MSK1|ALM0MSK0)
RTC_Write(AlarmRegister, match_bits) //Sec, Minutes Hours, Date match
CASE ELSE
match_bits = match_bits | ALM0MSK0
RTC_Write(AlarmRegister, match_bits) //Minutes match
ENDSELECT
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetMFP_Func(mode)
//Sets the RTC multi-function pin mode
DIM MFP_bits : MFP_bits = RTC_Read(CTRL)
SELECT mode
case 0 //For GPO clear SQWEN, ALM0EN, ALM1EN
MFP_bits = MFP_bits & ~(SQWEN|ALM_0|ALM_1)
RTC_Write(CTRL, MFP_bits)
case 1 //For ALARM Interrupts clear SQWEN and set either ALM0EN or ALM1EN
MFP_bits = MFP_bits & SQWEN
MFP_bits = MFP_bits | ALM_0
RTC_Write(CTRL, MFP_bits)
case 2 //For SQUARE WAVE set SQWEN
MFP_bits = MFP_bits & ~(ALM_0|ALM_1)
MFP_bits = MFP_bits | SQWEN
RTC_Write(CTRL, MFP_bits)
CASE ELSE //Alarm Interrupts
MFP_bits = MFP_bits & SQWEN
MFP_bits = MFP_bits | ALM_0
RTC_Write(CTRL, MFP_bits)
ENDSELECT
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_SetMFP_GPOStatus(status)
//Sets the RTC multi-function pin level (high/low)
DIM gpo_bit : gpo_bit = RTC_Read(CTRL) //General Purpose Output mode only available when (SQWEN = 0, ALM0EN = 0, and ALM1EN = 0):
IF (status == 0) THEN
gpo_bit = OUT_PIN //MFP signal level is logic low
RTC_Write(CTRL, gpo_bit)
ELSE //MFP signal level is logic high
gpo_bit = gpo_bit | OUT_PIN
RTC_Write(CTRL, gpo_bit)
ENDIF
ENDSUB
//==============================================================================
//==============================================================================
FUNCTION RTC_CheckPowerFail()
//Reads the timestamp when the main power source failed
DIM PowerFail_bit : PowerFail_bit = RTC_Read(DAY) //Read meridian bit
DIM PowerFail
IF ((PowerFail_bit & PWRFAIL) == PWRFAIL) THEN
PowerFail = 1
ELSE
PowerFail = 0
ENDIF
PowerFail_bit = PowerFail_bit & ~PWRFAIL //Clear Power failure bit
RTC_Write(DAY, PowerFail_bit) //Update PM/AM meridian bit
ENDFUNC PowerFail
//==============================================================================
//==============================================================================
FUNCTION RTC_IsVbatEnabled()
//Checks if the RTC vBat is enabled, 1 = enabled, 0 = disabled
DIM temp: temp = RTC_Read(DAY) //The 3rd bit of the RTCC_RTCC day register controls VBATEN
IF ((temp & VBATEN) == VBATEN) THEN
EXITFUNC 1
ENDIF
ENDFUNC 0
//==============================================================================
//==============================================================================
SUB RTC_EnableVbat()
//Enables vBat for the RTC
DIM temp : temp = RTC_Read(DAY) //The 3rd bit of the RTCC_RTCC day register controls VBATEN
temp = (temp | VBATEN) //Set 3rd bit to enable backup battery mode
RTC_Write(DAY, temp) //Enable backup battery mode
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_DisableVbat()
//Disables vBat for the RTC
DIM temp : temp = RTC_Read(DAY) //The 3rd bit of the RTCC_RTCC day register controls VBATEN
temp = (temp & VBAT_DIS) //Clear 3rd bit to disable backup battery mode
RTC_Write(DAY, temp) //Enable backup battery mode
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_GetPowerUpTime(ByRef tmin, ByRef thour, ByRef tdate, ByRef tmonth)
//Gets the RTC power-up time
tmin = RTC_bcd2dec(RTC_Read(PWRUPMIN))
thour = RTC_bcd2dec(RTC_Read(PWRUPHOUR))
tdate = RTC_bcd2dec(RTC_Read(PWRUPDATE))
tmonth = RTC_bcd2dec(RTC_Read(PWRUPMTH))
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_GetPowerDownTime(ByRef tmin, ByRef thour, ByRef tdate, ByRef tmonth)
//Gets the RTC power-down time
tmin = RTC_bcd2dec(RTC_Read(PWRDNMIN))
thour = RTC_bcd2dec(RTC_Read(PWRDNHOUR))
tdate = RTC_bcd2dec(RTC_Read(PWRDNDATE))
tmonth = RTC_bcd2dec(RTC_Read(PWRDNMTH))
ENDSUB
//==============================================================================
//==============================================================================
SUB RTC_Initialize()
//Used to initialise the RTC settings to default
RTC_SetHourFormat(0) //Set hour format to military time standard
RTC_DisableExOsc()
RTC_EnableVbat() //Enable battery backup
RTC_SetTime(0, 0, 0, 1, 1, 1, 12) //Configure RTC with default date and time
RTC_EnableOsc() //Start clock by enabling oscillator
ENDSUB