-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.LST
346 lines (333 loc) · 14.2 KB
/
calculator.LST
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
C51 COMPILER V9.01 CALCULATOR 05/21/2016 10:51:07 PAGE 1
C51 COMPILER V9.01, COMPILATION OF MODULE CALCULATOR
OBJECT MODULE PLACED IN calculator.OBJ
COMPILER INVOKED BY: G:\Keli\C51\BIN\C51.EXE calculator.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #define MAIN_Fosc 22118400L
2
3 #include "STC15Fxxxx.H"
4
5
6 #define Timer0_Reload (65536UL -(MAIN_Fosc / 1000))
7
8 #define DIS_DOT 0x20
9 #define DIS_BLACK 0x10
10 #define DIS_ 0x11
11
12
13
14
15 u8 code t_display[]={
16 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
17 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
18 //black - H J K L N o P U t G Q r M y
19 0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
20 0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
21
22 u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
23
24 // 0,1,2,3,4,5,6,7,8,9,10,11,
25 u8 code key_tab [] = {0,7,8,9,0,4,5,6,0,1,2,3,0,0,0,0,0};
26
27 sbit P_HC595_SER = P4^0; //pin 14 SER data input
28 sbit P_HC595_RCLK = P5^4; //pin 12 RCLk store (latch) clock
29 sbit P_HC595_SRCLK = P4^3; //pin 11 SRCLK Shift data clock
30
31
32 u8 LED8[8];
33 u8 display_index;
34 bit B_1ms;
35
36 u8 ADC_KeyState,ADC_KeyState1,ADC_KeyState2,ADC_KeyState3;
37 u8 ADC_KeyHoldCnt;
38 u8 KeyCode;
39 u8 cnt10ms;
40
41 u16 msecond;
42
43 u8 state,oper;
44 long num1,num2,res;
45
46 void CalculateAdcKey(u16 adc);
47 u16 Get_ADC10bitResult(u8 channel); //channel = 0~7
48 void DisplayRTC(void);
49 void RTC(void);
50
51 void init(){
52 1 P0M1 = 0; P0M0 = 0;
53 1 P1M1 = 0; P1M0 = 0;
54 1 P2M1 = 0; P2M0 = 0;
55 1 P3M1 = 0; P3M0 = 0;
C51 COMPILER V9.01 CALCULATOR 05/21/2016 10:51:07 PAGE 2
56 1 P4M1 = 0; P4M0 = 0;
57 1 P5M1 = 0; P5M0 = 0;
58 1 P6M1 = 0; P6M0 = 0;
59 1 P7M1 = 0; P7M0 = 0;
60 1
61 1 AUXR = 0x80;
62 1 TH0 = (u8)(Timer0_Reload / 256);
63 1 TL0 = (u8)(Timer0_Reload % 256);
64 1 ET0 = 1; //Timer0 interrupt enable
65 1 TR0 = 1; //Tiner0 run
66 1 EA = 1;
67 1
68 1 display_index = 0;
69 1 P1ASF = 0x10;
70 1 ADC_CONTR = 0xE0; //90T, ADC power on
71 1
72 1 state = 0;
73 1 oper = 0;
74 1
75 1 num1 = 0;
76 1 num2 = 0;
77 1 res = 0;
78 1 }
79
80
81 u16 Get_ADC10bitResult(u8 channel) //channel = 0~7
82 {
83 1 ADC_RES = 0;
84 1 ADC_RESL = 0;
85 1
86 1 ADC_CONTR = (ADC_CONTR & 0xe0) | 0x08 | channel; //start the ADC
87 1 NOP(4);
88 1
89 1 while((ADC_CONTR & 0x10) == 0) ; //wait for ADC finish
90 1 ADC_CONTR &= ~0x10;
91 1 return (((u16)ADC_RES << 2) | (ADC_RESL & 3));
92 1 }
93
94 #define ADC_OFFSET 16
95 void CalculateAdcKey(u16 adc)
96 {
97 1 u8 i;
98 1 u16 j;
99 1
100 1 if(adc < (64-ADC_OFFSET))
101 1 {
102 2 ADC_KeyState = 0;
103 2 ADC_KeyHoldCnt = 0;
104 2 }
105 1 j = 64;
106 1 for(i=1; i<=16; i++)
107 1 {
108 2 if((adc >= (j - ADC_OFFSET)) && (adc <= (j + ADC_OFFSET))) break;
109 2 j += 64;
110 2 }
111 1 ADC_KeyState3 = ADC_KeyState2;
112 1 ADC_KeyState2 = ADC_KeyState1;
113 1 if(i > 16) ADC_KeyState1 = 0;
114 1 else
115 1 {
116 2 ADC_KeyState1 = i;
117 2 if((ADC_KeyState3 == ADC_KeyState2) && (ADC_KeyState2 == ADC_KeyState1) &&
C51 COMPILER V9.01 CALCULATOR 05/21/2016 10:51:07 PAGE 3
118 2 (ADC_KeyState3 > 0) && (ADC_KeyState2 > 0) && (ADC_KeyState1 > 0))
119 2 {
120 3 if(ADC_KeyState == 0)
121 3 {
122 4 KeyCode = i;
123 4 ADC_KeyState = i;
124 4 ADC_KeyHoldCnt = 0;
125 4 }
126 3 if(ADC_KeyState == i)
127 3 {
128 4 if(++ADC_KeyHoldCnt >= 100)
129 4 {
130 5 ADC_KeyHoldCnt = 90;
131 5 KeyCode = i;
132 5 }
133 4 }
134 3 else ADC_KeyHoldCnt = 0;
135 3 }
136 2 }
137 1 }
138
139
140
141 void Send_595(u8 dat)
142 {
143 1 u8 i;
144 1 for(i=0; i<8; i++)
145 1 {
146 2 dat <<= 1;
147 2 P_HC595_SER = CY;
148 2 P_HC595_SRCLK = 1;
149 2 P_HC595_SRCLK = 0;
150 2 }
151 1 }
152
153
154 void DisplayScan(void)
155 {
156 1 Send_595(~T_COM[display_index]);
157 1 Send_595(t_display[LED8[display_index]]);
158 1
159 1 P_HC595_RCLK = 1;
160 1 P_HC595_RCLK = 0;
161 1 if(++display_index >= 8) display_index = 0;
162 1 }
163
164
165
166 void timer0 (void) interrupt TIMER0_VECTOR
167 {
168 1 DisplayScan();
169 1 B_1ms = 1;
170 1 }
171
172 void error()
173 {
174 1 u8 i;
175 1 for(i=0; i<8; i++) LED8[i] = 0x10;
176 1
177 1 state = 0;
178 1 oper = 0;
179 1
C51 COMPILER V9.01 CALCULATOR 05/21/2016 10:51:07 PAGE 4
180 1 num1 = 0;
181 1 num2 = 0;
182 1 res = 0;
183 1 }
184
185 void disp(long num)
186 {
187 1 u8 i;
188 1 if (num == 0)
189 1 {
190 2 LED8[7] = 0;
191 2 return;
192 2 }
193 1 for (i = 0;i < 8;i++)
194 1 if (num == 0) LED8[7 - i] = 0x10;
195 1 else
196 1 {
197 2 LED8[7 - i] = num % 10;
198 2 num = num / 10;
199 2 }
200 1 }
201
202 void calc()
203 {
204 1 switch (oper)
205 1 {
206 2 case 1:
207 2 res = num1 + num2;
208 2 break;
209 2 case 2:
210 2 res = num1 - num2;
211 2 break;
212 2 case 3:
213 2 res = num1 * num2;
214 2 break;
215 2 case 4:
216 2 res = num1 / num2;
217 2 break;
218 2 }
219 1 }
220
221 void react()
222 {
223 1 switch (KeyCode)
224 1 {
225 2 case 1:case 2:case 3:case 5:case 6:case 7:case 9:case 10:case 11:case 13:
226 2 if (state == 0)
227 2 {
228 3 num1 = num1 * 10 + key_tab[KeyCode];
229 3 disp(num1);
230 3 }
231 2 else if (state == 1)
232 2 {
233 3 num2 = num2 * 10 + key_tab[KeyCode];
234 3 disp(num2);
235 3 }
236 2 break;
237 2 case 4:case 8:case 12:case 16:
238 2 if (state == 0)
239 2 {
240 3 oper = KeyCode / 4;
241 3 disp(oper);
C51 COMPILER V9.01 CALCULATOR 05/21/2016 10:51:07 PAGE 5
242 3 state = 1;
243 3 }
244 2 else
245 2 {
246 3 error();
247 3 return;
248 3 }
249 2 break;
250 2 case 15:
251 2 if (state == 0)
252 2 {
253 3 res = num1;
254 3 disp(res);
255 3 state = 0;
256 3 num1 = 0;
257 3 num2 = 0;
258 3 oper = 0;
259 3 res = 0;
260 3 }
261 2 else if (state == 1)
262 2 {
263 3 calc();
264 3 disp(res);
265 3 state = 0;
266 3 num1 = 0;
267 3 num2 = 0;
268 3 oper = 0;
269 3 res = 0;
270 3 }
271 2 break;
272 2 default: error();
273 2 }
274 1 KeyCode = 0;
275 1 }
276
277 void main()
278 {
279 1 u8 i;
280 1 u16 j;
281 1
282 1 init();
283 1
284 1 for(i=0; i<8; i++) LED8[i] = 0x10;
285 1
286 1 ADC_KeyState = 0;
287 1 ADC_KeyState1 = 0;
288 1 ADC_KeyState2 = 0;
289 1 ADC_KeyState3 = 0;
290 1 ADC_KeyHoldCnt = 0;
291 1 KeyCode = 0;
292 1 cnt10ms = 0;
293 1
294 1 while(1)
295 1 {
296 2 if(B_1ms)
297 2 {
298 3 B_1ms = 0;
299 3
300 3 if(++cnt10ms >= 10)
301 3 {
302 4 cnt10ms = 0;
303 4 j = Get_ADC10bitResult(4);
C51 COMPILER V9.01 CALCULATOR 05/21/2016 10:51:07 PAGE 6
304 4 if(j < 1024) CalculateAdcKey(j);
305 4
306 4 }
307 3
308 3 //disp(KeyCode);
309 3 if(KeyCode > 0) react();
310 3
311 3 }
312 2 }
313 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1027 ----
CONSTANT SIZE = 68 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 32 5
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)