-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.c
executable file
·1886 lines (1737 loc) · 50.7 KB
/
main.c
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: main.c
* Author: Jordan Wills
*
* Created on October 9, 2014, 1:50 PM
*
* This program causes the PIC to act as a binary clock. The following list
* details the functionality of each pin in the system. If a pin is not listed,
* it is assumed to be unused.
*
* A0: Pushbutton 1. Ultra low power wake used for deep sleep wake on btn press
*
* B0: Pushbutton 0. INT0 used for deep sleep wake on button press
* B1: Lower LED row digit 6 (minutes "32")
* B2: Upper LED row digit 2 (hours "2")
* B3: Upper LED row digit 3 (hours "4")
* B4: Upper LED row digit 4 (hours "8")
* B5: Upper LED row digit 1 (hours "1")
* B6: Date LED / programmer clock (PGC)
* B7: Time LED / programmer data (PGD)
*
* C0: RTCC crystal OSO
* C1: RTCC crystal OSI
* C3: Lower LED row digit 4 (minutes "8")
* C4: Lower LED row digit 3 (minutes "4")
* C5: Lower LED row digit 2 (minutes "2")
* C6: Lower LED row digit 1 (minutes "1")
* C7: Lower LED row digit 5 (minutes "16")
*
* The following list details the functionality of various peripherals:
* Timer0: Debounce Pushbutton 0
* Timer1: Debounce Pushbutton 1
* Timer2: LED change animation
* Timer3: During "set" mode, blinks the row currently being modified
*/
/******************************************************************************
* Software License Agreement
*
* Copyright © 2014 Jordan Wills. All rights reserved.
*
* SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
* OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
* PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR
* OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,
* BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT
* DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
* INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
* COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY
* CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
* OR OTHER SIMILAR COSTS.
*
*****************************************************************************/
#include <p18f24j11.h>
#pragma config WDTEN = OFF, OSC = INTOSC, RTCOSC = T1OSCREF, XINST = OFF
//*****************************************************************************
// Chip wide defines
//*****************************************************************************
//
// Calibration settings
//
#define CONFIG_BUILD 0
#define RTCCAL_VAL 0
//
// Clock values
//
#define INTOSC_FREQ ((unsigned long)4000000)
//
// Timer0 and PB0
//
#define T0_PSCALE 16
#define T0_PSCALE_BITS 0x3
#define T0_uS_PER_TICK 16
// debounce = debounce time(uS) / 16
#define T0_DEBOUNCE 3125
// hold = hold time(uS) / 16
#define T0_HOLD 62500
//
// Timer1 and PB1
//
#define T1_PSCALE 1
#define T1_PSCALE_BITS 0x0
#define T1_uS_PER_TICK 30
// debounce = debounce time(uS) / 8
#define T1_DEBOUNCE 1640
// hold = hold time(uS) / 8
#define T1_HOLD 32787
//
// Pushbutton state machine defines
//
#define PB_STATE_IDLE 0
#define PB_STATE_DEBOUNCING 1
#define PB_STATE_MID_PRESS 2
#define PB_STATE_HELD 3
#define PB_STATE_RELEASED 4
//
// LED display states
//
#define DISP_STATE_TIME_S 0
#define DISP_STATE_TIME_H 1
#define DISP_STATE_DATE 2
#define DISP_STATE_SET_TIME 3
#define DISP_STATE_SET_DATE 4
//
// set time states
//
#define SET_STATE_LOWER 0
#define SET_STATE_UPPER 1
//
// LED port and pin definitions
//
#define PB0_PORT PORTB
#define PB0_PORT_BITS PORTBbits
#define PB0_PIN RB0
#define PB0 PB0_PORT_BITS.PB0_PIN
#define PB1_PORT PORTA
#define PB1_PORT_BITS PORTAbits
#define PB1_PIN RA0
#define PB1 PB1_PORT_BITS.PB1_PIN
#define L_ROW_PORT PORTC
#define L_ROW_PORT_PINS PORTCbits
#define L6_PORT PORTB
#define L6_PORT_PINS PORTBbits
#define L_L1_PIN RC6
#define L_L2_PIN RC5
#define L_L3_PIN RC4
#define L_L4_PIN RC3
#define L_L5_PIN RC7
#define L_L6_PIN RB1
#define LED_L1 L_ROW_PORT_PINS.L_L1_PIN
#define LED_L2 L_ROW_PORT_PINS.L_L2_PIN
#define LED_L3 L_ROW_PORT_PINS.L_L3_PIN
#define LED_L4 L_ROW_PORT_PINS.L_L4_PIN
#define LED_L5 L_ROW_PORT_PINS.L_L5_PIN
#define LED_L6 L6_PORT_PINS.L_L6_PIN
#define U_ROW_PORT PORTB
#define U_ROW_PORT_PINS PORTBbits
#define U_L1_PIN RB5
#define U_L2_PIN RB2
#define U_L3_PIN RB3
#define U_L4_PIN RB4
#define LED_U1 U_ROW_PORT_PINS.U_L1_PIN
#define LED_U2 U_ROW_PORT_PINS.U_L1_PIN
#define LED_U3 U_ROW_PORT_PINS.U_L1_PIN
#define LED_U4 U_ROW_PORT_PINS.U_L1_PIN
#define DT_PORT PORTB
#define DT_PORT_PINS PORTBbits
#define DT_TIME_PIN RB7
#define DT_DATE_PIN RB6
#define LED_DATE DT_PORT_PINS.DT_DATE_PIN
#define LED_TIME DT_PORT_PINS.DT_TIME_PIN
//
// Type for mapping out bits of dleep sleep persistent memory byte0
//
typedef union t_DSGPR0map
{
struct
{
//
// No matter how much I use a PIC, I will never be comfortable with
// using bitfields
//
unsigned char PB0State : 2;
unsigned char PB1State : 2;
};
unsigned char ucRaw;
} DSGPR0map;
//
// Type for mapping out bits of dleep sleep persistent memory byte1
//
typedef union t_DSGPR1map
{
struct
{
unsigned char dispState : 3;
unsigned char setState : 1;
unsigned char blinkState : 1;
unsigned char blackout : 1;
};
unsigned char ucRaw;
} DSGPR1map;
//
// Macros for deep sleep fields... deprecated by bit fields
//
#define PB0_DSREG DSGPR0
#define PB0_STATE_MASK 0x03
#define PB0_STATE_SHIFT 0
#define PB1_DSREG DSGPR0
#define PB1_STATE_MASK 0x0C
#define PB1_STATE_SHIFT 2
#define DISP_STATE_DSREG DSGPR1
#define DISP_STATE_MASK 0x07
#define DISP_STATE_SHIFT 0
#define DISP_BLINK_DSREG DSGPR1
#define DISP_BLINK_MASK 0x08
#define DISP_BLINK_SHIFT 3
//*****************************************************************************
// Global variables
//*****************************************************************************
unsigned char g_ucPB0State = PB_STATE_IDLE;
unsigned char g_ucPB1State = PB_STATE_IDLE;
volatile unsigned char g_ucNapTime = 0;
DSGPR0map g_sDSGPR0;
DSGPR1map g_sDSGPR1;
//*****************************************************************************
// Function prototypes
//*****************************************************************************
void PressPB0(void);
void HoldPB0(void);
void PressPB1(void);
void HoldPB1(void);
int displayTime(void);
//*****************************************************************************
// CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE
//*****************************************************************************
//*****************************************************************************
//
// These should never be entered. If we get here, something has gone awry. In
// release mode, do nothing. In dev mode, turn on & hold the date and time
// LEDs to signify that bad has occurred.
//
//*****************************************************************************
//#pragma interrupt HiISR
void HiISR(void)
{
LED_DATE = 1;
LED_TIME = 1;
//while(1);
}
//#pragma interruptlow LoISR
void LoISR(void)
{
LED_DATE = 1;
LED_TIME = 1;
//while(1);
}
//*****************************************************************************
//
// Configure the GPIOs. Note that this gets called on both cold boot and deep
// sleep wake, so don't set anything here that you don't want done on a DS wake
// (like default LED values)
//
//*****************************************************************************
void configGPIOs(void)
{
//
// Set all GPIOs as digital, not analog
//
ANCON0 = 0xFF;
ANCON1 = 0x1F;
//
// Set PA0 to input, PA1..7 as output
//
TRISA = 1;
//
// Set PB0 to input, PB1..7 as output
//
TRISB = 1;
//
// Set port b to enable weak pull-up
//
INTCON2bits.NOT_RBPU = 1;
//
// Set all PC to output
//
TRISC = 0;
//
// Set unused ports to drive low, per low power recommendations in datasheet
//
PORTCbits.RC2 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;
PORTAbits.RA3 = 0;
PORTAbits.RA5 = 0;
}
//*****************************************************************************
//
// Configure the Real time clock/calendar peripheral.
//
//*****************************************************************************
void configRTCC(void)
{
//
// unlock writes to the RTCC
//
EECON2 = 0x55;
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = 1;
//
// enable the RTCC, set pointers to read seconds and minutes
//
RTCCFGbits.RTCEN = 1;
RTCCFGbits.RTCPTR0 = 0;
RTCCFGbits.RTCPTR1 = 0;
//
// enable the RTCC alarm, chime @ 1 Hz
//
ALRMCFGbits.CHIME = 1;
ALRMCFGbits.AMASK0 = 1;
ALRMCFGbits.AMASK1 = 0;
ALRMCFGbits.AMASK2 = 0;
ALRMCFGbits.AMASK3 = 0;
ALRMCFGbits.ALRMEN = 1;
}
//*****************************************************************************
//
// Configure the deep sleep module. We want to be able to wake from the RTCC,
// INT0, and the ULPWU interrupt. Also, make sure deep sleep really means deep
// sleep, not low power sleep.
//
//*****************************************************************************
void configDS(void)
{
WDTCONbits.REGSLP = 1;
OSCCONbits.IDLEN = 0;
DSCONHbits.RTCWDIS = 0;
DSCONHbits.DSULPEN = 0;
DSCONLbits.ULPWDIS = 0;
}
//*****************************************************************************
//
// Configure Timer3. Source from internal Oscillator, maximum prescaler, enable
// 16 bit mode.
//
//*****************************************************************************
void configTimer3(void)
{
//
// set source to intOSC/4, 1 MHz
//
T3CONbits.TMR3CS = 0;
//
// Set prescaler to 1:8
//
T3CONbits.T3CKPS = 0x3;
//
// Enable 16 bit operations
//
T3CONbits.RD16 = 1;
//
// zero out timer
//
TMR3H = 0;
TMR3L = 0;
//
// Turn on timer
//
//T3CONbits.TMR3ON = 1;
return;
}
//*****************************************************************************
//
// Configure Timer1. Source from T1OSC (external 32.768 crystal), no prescaler,
// 8 bit width, no counting/gating
//
//*****************************************************************************
void configTimer1(void)
{
//
// set source to T1OSC (T1CKI pin)
//
T1CONbits.TMR1CS = 2;
//
// set the prescaler to 0, giving us a max timeout of 2 s
//
// set prescaler to 1:1, giving us a range of:
// (1/32.768KHz) * prescaler = 30.5 us per tick
// 1 = 30.5 uS, 0xFFFF = 2 s
//
T1CONbits.T1CKPS = 0x0;
//
// Enable T1 external oscillator
//
T1CONbits.T1OSCEN = 1;
//
// Set timer to 8 bit width
//
T1CONbits.RD16 = 1;
//
// Make sure T1 gate is diabled
//
T1GCONbits.TMR1GE = 0;
PIR1bits.TMR1IF = 0;
PIE1bits.TMR1IE = 0;
T1CONbits.TMR1ON = 0;
}
//*****************************************************************************
//
// Configure Timer2. Source from internal Oscillator, 1:16 prescaler
//
//*****************************************************************************
void configTimer2(void)
{
//
// Set prescaler to 1:16
//
T2CONbits.T2CKPS = 0x2;
//
// zero out timer
//
TMR2 = 0;
}
//*****************************************************************************
//
// Configure Timer0. Source from internal Oscillator, 1:16 prescaler, enable
// 8 bit mode.
//
//*****************************************************************************
void configTimer0(void)
{
//
// set source to intOSC
//
T0CONbits.T0CS = 0;
T0CONbits.T08BIT = 0;
//
// set prescaler to 1:16, giving us a range of:
// (1/4MHz * 4) * prescaler = 16 us per tick
// 1 = 16 uS, 0xFFFF = 1.05 s
//
T0CONbits.T0PS = 0x03;
T0CONbits.PSA = 0;
//
// Always write TMR0H before writing TMR0L
//
TMR0H = 0;
TMR0L = 0;
//
// enable timer interrupt
//
INTCONbits.TMR0IE = 0;
INTCONbits.TMR0IF = 0;
}
//*****************************************************************************
//
// This function will cause the system to go into deep sleep mode. If all goes
// well, this function never returns. On wake, the system will start executing
// at main.
//
//*****************************************************************************
void enterDS(void)
{
DSCONHbits.DSULPEN = 1;
DSGPR0 = g_sDSGPR0.ucRaw;
DSGPR1 = g_sDSGPR1.ucRaw;
DSCONHbits.DSEN = 1;
Sleep();
}
//*****************************************************************************
//
// This function will update the state of pushbutton 0. If it detects a press
// or hold of the pushbutton, it will call the corresponding function. On
// return, it will signify whether it is safe for the state machine to go to
// sleep (and thus disable and reset the timer being used). This function uses
// the Timer0 peripheral to gauge debounce and hold times.
//
// pfnPress: a pointer to the function called when the button is pressed
// pfnHold: a pointer to the function called when the button is held
//
// return: 0 if the it is safe to sleep system, 1 if it needs to stay awake.
//
//*****************************************************************************
int debouncePB0(void (*pfnPress)(void), void (*pfnHold)(void))
{
unsigned int uiTime;
if((g_ucPB0State == PB_STATE_IDLE) && !PB0)
{
g_ucPB0State = PB_STATE_DEBOUNCING;
TMR0H = 0;
TMR0L = 0;
T0CONbits.TMR0ON = 1;
return 1;
}
if(PB0)
{
//
// If the button isn't pressed and we're in idle state, go back to bed
//
if(g_ucPB0State == PB_STATE_IDLE)
{
return 0;
}
//
// If the button is not pressed and we're in debounce state, the button
// went from press to not press during the bounce window. Act like
// nothing happened, go back to bed.
//
if(g_ucPB0State == PB_STATE_DEBOUNCING)
{
g_ucPB0State = PB_STATE_IDLE;
T0CONbits.TMR0ON = 0;
return 0;
}
//
// If the button is not pressed and we're past the bounce window but
// not past the hold window, we have a normal pb press. Take action.
//
else if(g_ucPB0State == PB_STATE_MID_PRESS)
{
pfnPress();
T0CONbits.TMR0ON = 0;
g_ucPB0State = PB_STATE_IDLE;
return 0;
}
//
// If the button is not pressed and was previously held, we should kick
// off the debounce timer for release
//
else if(g_ucPB0State == PB_STATE_HELD)
{
TMR0H = 0;
TMR0L = 0;
T0CONbits.TMR0ON = 1;
g_ucPB0State = PB_STATE_RELEASED;
return 1;
}
//
// If the button has been released from a hold, we wait until the timer
// is past its debounce value, then are safe to go to sleep
//
else if(g_ucPB0State == PB_STATE_RELEASED)
{
uiTime = TMR0L;
uiTime = uiTime | ((unsigned int)TMR0H << 8);
//
// Make sure that the button is debounced on release before saying
// we're good to go to sleep
//
if(uiTime > T0_DEBOUNCE)
{
T0CONbits.TMR0ON = 0;
g_ucPB0State = PB_STATE_IDLE;
return 0;
}
return 1;
}
}
else
{
uiTime = TMR0L;
uiTime = uiTime | ((unsigned int)TMR0H << 8);
//
// If the button is pressed and we're past the hold window, evaluate
// the current state.
//
if(uiTime > T0_HOLD)
{
//
// If the current state has not already transitioned to held, take
// the hold action and update it. If the state already is held,
// there's nothing to do until the user releases the button.
//
if(g_ucPB0State != PB_STATE_HELD)
{
g_ucPB0State = PB_STATE_HELD;
T0CONbits.TMR0ON = 0;
pfnHold();
return 1;
}
return 1;
}
//
// If the button is pressed and we're past the bounce window, mark the
// state as mid-press. We'll evaluate if it's a press or a hold later.
//
else if(uiTime > T0_DEBOUNCE)
{
g_ucPB0State = PB_STATE_MID_PRESS;
return 1;
}
return 1;
}
}
//*****************************************************************************
//
// This function will update the state of pushbutton 1. If it detects a press
// or hold of the pushbutton, it will call the corresponding function. On
// return, it will signify whether it is safe for the state machine to go to
// sleep (and thus disable and reset the timer being used). This function uses
// the Timer1 peripheral to gauge debounce and hold times.
//
// pfnPress: a pointer to the function called when the button is pressed
// pfnHold: a pointer to the function called when the button is held
//
// return: 0 if the it is safe to sleep system, 1 if it needs to stay awake.
//
//*****************************************************************************
int debouncePB1(void (*pfnPress)(void), void (*pfnHold)(void))
{
static unsigned long ulTime = 0;
if((g_ucPB1State == PB_STATE_IDLE) && !PB1)
{
g_ucPB1State = PB_STATE_DEBOUNCING;
TMR1H = 0;
TMR1L = 0;
PIR1bits.TMR1IF = 0; //delme
T1CONbits.TMR1ON = 1;
return 1;
}
if(PB1)
{
if(g_ucPB1State == PB_STATE_IDLE)
{
return 0;
}
//
// If the button is not pressed and we're in debounce state, the button
// went from press to not press during the bounce window. Act like
// nothing happened, go back to bed.
//
if(g_ucPB1State == PB_STATE_DEBOUNCING)
{
g_ucPB1State = PB_STATE_IDLE;
PIR1bits.TMR1IF = 0; //delme
T1CONbits.TMR1ON = 0;
return 0;
}
//
// If the button is not pressed and we're past the bounce window but
// not past the hold window, we have a normal pb press. Take action.
//
else if(g_ucPB1State == PB_STATE_MID_PRESS)
{
pfnPress();
PIR1bits.TMR1IF = 0; //delme
T1CONbits.TMR1ON = 0;
g_ucPB1State = PB_STATE_IDLE;
return 0;
}
//
// If the button is not pressed and we're past the hold window, we have
// a release after a hold. Action should have already occured, go back
// to sleep.
//
else if(g_ucPB1State == PB_STATE_HELD)
{
g_ucPB1State = PB_STATE_IDLE;
return 1;
}
}
else
{
//
// if we overflow the counter (which we will on a hold), add 0x10000 to
// the time, then and off the bottom 16 bits.
//
//if(PIR1bits.TMR1IF)
//{
// ulTime = 0x10000;
//}
//else
//{
// ulTime = 0;
//}
//ulTime += TMR1L;
ulTime = TMR1L;
ulTime |= ((unsigned int)TMR1H << 8);
//
// If the button is pressed and we're past the hold window, evaluate
// the current state.
//
//if(uiTime > T0_HOLD)
if(ulTime > T1_HOLD)
{
//
// If the current state has not already transitioned to held, take
// the hold action and update it. If the state already is held,
// there's nothing to do until the user releases the button.
//
if(g_ucPB1State != PB_STATE_HELD)
{
pfnHold();
g_ucPB1State = PB_STATE_HELD;
T1CONbits.TMR1ON = 0;
}
return 1;
}
//
// If the button is pressed and we're past the bounce window, mark the
// state as mid-press. We'll evaluate if it's a press or a hold later.
//
else if(ulTime > T1_DEBOUNCE)
{
g_ucPB1State = PB_STATE_MID_PRESS;
return 1;
}
return 1;
}
}
//*****************************************************************************
//
// This function is called whenever PB0 is pressed. If the watch is in display
// mode, a PB0 press will cycle to the next display mode (seconds, minutes &
// hours, or days & months). If the watch is in set mode, a PB0 press will
// increment the field that is currently selected, rolling over when necessary.
//
// return: none
//
//*****************************************************************************
void PressPB0(void)
{
unsigned char ucMinute;
unsigned char ucHour;
unsigned char ucMonth;
unsigned char ucDay;
unsigned char ucValH;
if(g_sDSGPR1.blackout)
{
g_sDSGPR1.blackout = 0;
return;
}
g_ucNapTime = 0;
//
// If we're in display mode, PB0 will toggle between showing date or time.
//
if( g_sDSGPR1.dispState == DISP_STATE_TIME_S)
{
g_sDSGPR1.dispState = DISP_STATE_TIME_H;
}
else if( g_sDSGPR1.dispState == DISP_STATE_TIME_H)
{
g_sDSGPR1.dispState = DISP_STATE_DATE;
}
else if(g_sDSGPR1.dispState == DISP_STATE_DATE)
{
g_sDSGPR1.dispState = DISP_STATE_TIME_S;
}
else if(g_sDSGPR1.dispState == DISP_STATE_SET_TIME)
{
//
// When setting the time, lower is minutes, upper is hours
//
if(g_sDSGPR1.setState == SET_STATE_LOWER)
{
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
ucMinute = (RTCVALH & 0xF) + (10*(RTCVALH >> 4));
ucMinute++;
//
// Minutes are easy to increment. Go up until you reach 60, then
// back to 0.
//
if(ucMinute >= 60)
{
ucMinute = 0;
}
//
// Write the value back to RTCC peripheral in BCD form
//
RTCVALH = (ucMinute % 10) | ((ucMinute/10) << 4);
}
else
{
RTCCFGbits.RTCPTR0 = 1;
RTCCFGbits.RTCPTR1 = 0;
ucHour = (RTCVALL & 0xF) + (10*(RTCVALL >> 4));
ucHour++;
//
// Hours are pretty easy to increment as well. Go up until you
// hit 24, then back to 0.
//
if(ucHour >= 24)
{
ucHour = 0;
}
//
// Write the value back to RTCC peripheral in BCD form
//
RTCVALL = (ucHour % 10) | ((ucHour/10) << 4);
}
}
else if(g_sDSGPR1.dispState == DISP_STATE_SET_DATE)
{
//
// When setting the date, lower is days, upper is months
//
if(g_sDSGPR1.setState == SET_STATE_LOWER)
{
RTCCFG &= ~0x03;
RTCCFG |= 0x2;
ucDay = (RTCVALL & 0xF) + (10*(RTCVALL >> 4));
ucValH = RTCVALH;
ucMonth = (ucValH & 0xF) + (10*(ucValH >> 4));
ucDay++;
//
// firguring out how to roll over months is irritating. Stupid
// Roman calendar.
//
if(ucMonth == 2)
{
if(ucDay >= 29)
{
ucDay = 1;
}
}
//
// Thirty days have September,
// April, June, and November.
// All the rest have 31,
// Except for February, because he's a jerk.
//
else if((ucMonth == 1) || (ucMonth == 3) || (ucMonth == 5) ||
(ucMonth == 7) || (ucMonth == 8) || (ucMonth == 10) ||
(ucMonth == 12))
{
if(ucDay >= 32)
{
ucDay = 1;
}
}
else
{
if(ucDay >= 31)
{
ucDay = 1;
}
}
//
// Write the day back to the RTCC in BCD form
//
RTCCFG &= ~0x03;
RTCCFG |= 0x2;
RTCVALL = (ucDay % 10) | ((ucDay/10) << 4);
}
else
{
RTCCFG &= ~0x03;
RTCCFG |= 0x2;
ucValH = RTCVALH;
ucMonth = (ucValH & 0xF) + (10*(ucValH >> 4));
ucMonth++;
//
// Month is easy to figure out... just be careful about 1..12 as
// as opposed to 0..11.
//
if(ucMonth >= 13)
{
ucMonth = 1;
}
//
// Write the month back to the RTCC in BCD form
//
RTCCFG &= ~0x03;
RTCCFG |= 0x2;
RTCVALH = (ucMonth % 10) | ((ucMonth/10) << 4);
}
}
}
//*****************************************************************************
//
// This function is called whenever PB0 is held. Regardless of watch state,
// holding PB0 will cause the watch to go into deep sleep with all LEDs turned
// off, waking up when another button is pressed.
//
// return: none
//
//*****************************************************************************
void HoldPB0(void)
{
//
// If the button is held while in anything other than a "display" mode,
// ignore the press
//
if((g_sDSGPR1.dispState == DISP_STATE_TIME_S) ||
(g_sDSGPR1.dispState == DISP_STATE_TIME_H) ||
(g_sDSGPR1.dispState == DISP_STATE_DATE))
{
g_ucNapTime = 1;
}
}
//*****************************************************************************
//
// This function is called whenever PB1 is held. PB1 is used to enter "set"
// mode, or move to setting the next value if the watch is already in a set
// mode.
//
// return: none
//
//*****************************************************************************
void HoldPB1(void)
{
if(g_sDSGPR1.blackout)
{
g_sDSGPR1.blackout = 0;
return;
}
//
// If we're in any display mode, PB1 press will enter set mode. We always
// start by setting time until PB1 is pressed again, then set date until
// PB1 press, then we go back to display time mode.
//
if( (g_sDSGPR1.dispState == DISP_STATE_TIME_S) ||
(g_sDSGPR1.dispState == DISP_STATE_TIME_H) ||
(g_sDSGPR1.dispState == DISP_STATE_DATE))
{
//
// unlock writes to the RTCC, then disable clock while we're modifying
// values
//
EECON2 = 0x55;
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = 1;
RTCCFGbits.RTCEN = 0;
TMR3H = 0;
TMR3L = 0;
T3CONbits.TMR3ON = 1;
g_sDSGPR1.dispState = DISP_STATE_SET_TIME;
LED_DATE = 0;
LED_TIME = 1;
}
return;
}
//*****************************************************************************
//
// This function is called whenever PB1 is pressed. Pressing PB1 will toggle
// whether or not display updates blink in or shift in (default). I like the
// shiftin, but some find it annoying.