-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_kbd.c
More file actions
144 lines (124 loc) · 2.58 KB
/
timer_kbd.c
File metadata and controls
144 lines (124 loc) · 2.58 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
/*
* Author: Michał Dziuba (mail@michaldziuba.dev)
* All rights reserved.
* This code is proprietary and may not be used, copied, modified, or distributed
* without explicit permission from the author.
*/
#include <8051.h>
#define TRUE 1
#define FALSE 0
#define TH_0 231
#define MAX_COUNTER 1152
__xdata unsigned char *CSDS = (__xdata unsigned char *)0xFF38;
__xdata unsigned char *CSDB = (__xdata unsigned char *)0xFF30;
__bit __at(0x96) SEG_OFF;
unsigned char SS = 45, MM = 58, HH = 23;
unsigned char display_values[6] = {0};
unsigned char KBD[4] = {0, 0, 0, 0};
#define ENTER 0b000001
#define LEFT 0b100000
#define RIGHT 0b000100
#define DOWN 0b010000
const unsigned char patterns[] = {
0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110,
0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111,
};
void t0_int(void) __interrupt(1) {
F0 = TRUE;
TH0 = TH0 | TH_0;
}
static unsigned char idx = 0;
void set_display() {
display_values[0] = SS % 10;
display_values[1] = SS / 10;
display_values[2] = MM % 10;
display_values[3] = MM / 10;
display_values[4] = HH % 10;
display_values[5] = HH / 10;
}
void increment_time() {
SS++;
if (SS >= 60) {
SS = 0;
MM++;
if (MM >= 60) {
MM = 0;
HH++;
if (HH >= 24) {
HH = 0;
}
}
}
}
void keyboard_handler() {
if ((KBD[0] != KBD[1]) && (KBD[0] != KBD[2]) && (KBD[0] != KBD[3])) {
if (KBD[0] & LEFT) {
HH = (HH + 1) % 24;
} else if (KBD[0] & DOWN) {
MM = (MM + 1);
if (MM > 59) {
MM = 0;
HH = (HH + 1) % 24;
}
} else if (KBD[0] & RIGHT) {
increment_time();
}
set_display();
}
KBD[3] = KBD[2];
KBD[2] = KBD[1];
KBD[1] = KBD[0];
KBD[0] = 0;
}
void refresh_display() {
static unsigned char mask = 1;
unsigned char value;
value = display_values[idx];
SEG_OFF = TRUE;
*CSDB = mask;
*CSDS = patterns[value];
SEG_OFF = FALSE;
if (P3_5) {
KBD[0] |= mask;
}
idx = (idx + 1) % 6;
mask = mask << 1;
if (mask == 32) {
if (KBD[0] != 0) {
keyboard_handler();
} else {
KBD[0] = 0;
KBD[1] = 0;
KBD[2] = 0;
KBD[3] = 0;
}
} else if (mask > 32) {
mask = 1;
}
}
void main() {
int counter;
TMOD = 0b01110000;
TH0 = TH_0;
ET0 = TRUE;
EA = TRUE;
TR0 = TRUE;
F0 = FALSE;
P1_7 = 0;
counter = MAX_COUNTER;
set_display();
for (;;) {
if (!F0) {
continue;
}
F0 = FALSE;
refresh_display();
counter--;
if (counter)
continue;
P1_7 = !P1_7;
increment_time();
set_display();
counter = MAX_COUNTER;
}
}