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
|
| #include <AT89X52.H>
#include "KEY44.H"
#include "7SEG.H"
bit Flag;
sbit LED = P3 ^ 0;
unsigned char time;
unsigned char NTime;
void timer_init(void)
{
TMOD = 0x01;
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void time_Stop(void)
{
TMOD = 0x00;
TH0 = 0;
TL0 = 0;
ET0 = 0;
EA = 0;
TR0 = 0;
}
void main(void)
{
NTime = 0;
while(1)
{
unsigned char key = Matrix_keyboard();
if(key == 1)
{
timer_init();
LED = 1;
}
if(key == 2)
{
time_Stop();
Flag = 0;
NTime = 0;
LED = 0;
}
if (Flag == 1)
{
Flag = 0;
Seg_Show(NTime);
NTime += 1;
if(NTime == 99 + 1)
{
NTime = 0;
}
}
}
}
void t_0(void) interrupt 1 using 0
{
TR0 = 0;
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
time++;
if(time == 100)
{
time = 0;
Flag = 1;
}
TR0 = 1;
}
/*----------------------------------------
File: 7SEG.h
Date:Loser007 May 5, 2018 18:04
----------------------------------------*/
#ifndef __7SEG_H__
#define __7SEG_H__
#pragma SAVE
#pragma REGPARMS
extern void Seg_Show(unsigned char Time);
unsigned char code seg_cc[] = //Common cathode digital tube segment code
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x77, //A
0x7C, //B
0x39, //C
0x5E, //D
0x79, //E
0x71, //F
};
/*----------------------------------------
Name: Seg_Show
Features: Show time in 2 SEG
Parameter: No
Return: N0
Instructions:SEG I/0 From P1^0 to P1^7 & P2^0 to P2^7
Date:Loser007 May 5, 2018 10:40
----------------------------------------*/
void Seg_Show(unsigned char Time)
{
P0 = seg_cc[Time / 10];
P2 = seg_cc[Time % 10];
}
#endif
/*----------------------------------------
File: key44.h
Date:Loser007 May 5, 2018 12:59
----------------------------------------*/
#ifndef __KEY44_H__
#define __KEY44_H__
#pragma SAVE
#pragma REGPARMS
extern char Matrix_keyboard(void);
/*----------------------------------------
Name: Matrix_keyboard
Features: Get a case from the 4*4 keyboard
Parameter: No
Return:Key code (char)
Instructions:Keyboard I/0 From P1^0 to P1^7
Date:Loser007 May 5, 2018 10:40
----------------------------------------*/
char Matrix_keyboard(void)
{
unsigned char Key = 0, X = 0, Y = 0;
P1 = 0xF0; //11110000
X = P1 & 0xF0; //Get row (P1 XOR 11110000) Get Height
if(X != 0xF0)
{
X = P1 ^ 0xF0;
P1 = 0x0F; //00001111
Y = P1 & 0x0F;
if(Y != 0x0F)
{
Y = P1 ^ 0x0F; //Get Column (P1 XOR 00001111) Get Low
Key = X + Y;
}
}
switch (Key)
{
// 1
case 0x81:
return 0; //1000 0001
case 0x82:
return 1; //1000 0010
case 0x84:
return 2; //1000 0100
case 0x88:
return 3; //1000 1000
// 2
case 0x41:
return 4; //0100 0001
case 0x42:
return 5; //0100 0010
case 0x44:
return 6; //0100 0100
case 0x48:
return 7; //0100 1000
// 3
case 0x21:
return 8; //0010 0001
case 0x22:
return 9; //0010 0010
case 0x24:
return 10; //0010 0100
case 0x28:
return 11; //0010 1000
//4
case 0x11:
return 12; //0001 0001
case 0x12:
return 13; //0001 0010
case 0x14:
return 14; //0001 0100
case 0x18:
return 15; //0001 1000
default:
return 255;
}
}
#endif
|