-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_v2.LST
389 lines (375 loc) · 16.4 KB
/
calculator_v2.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
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
C51 COMPILER V9.01 CALCULATOR_V2 05/21/2016 12:04:30 PAGE 1
C51 COMPILER V9.01, COMPILATION OF MODULE CALCULATOR_V2
OBJECT MODULE PLACED IN calculator_v2.OBJ
COMPILER INVOKED BY: G:\Keli\C51\BIN\C51.EXE calculator_v2.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,sign;
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_V2 05/21/2016 12:04:30 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 sign = 1;
75 1
76 1 num1 = 0;
77 1 num2 = 0;
78 1 res = 0;
79 1 }
80
81
82 u16 Get_ADC10bitResult(u8 channel) //channel = 0~7
83 {
84 1 ADC_RES = 0;
85 1 ADC_RESL = 0;
86 1
87 1 ADC_CONTR = (ADC_CONTR & 0xe0) | 0x08 | channel; //start the ADC
88 1 NOP(4);
89 1
90 1 while((ADC_CONTR & 0x10) == 0) ; //wait for ADC finish
91 1 ADC_CONTR &= ~0x10;
92 1 return (((u16)ADC_RES << 2) | (ADC_RESL & 3));
93 1 }
94
95 #define ADC_OFFSET 16
96 void CalculateAdcKey(u16 adc)
97 {
98 1 u8 i;
99 1 u16 j;
100 1
101 1 if(adc < (64-ADC_OFFSET))
102 1 {
103 2 ADC_KeyState = 0;
104 2 ADC_KeyHoldCnt = 0;
105 2 }
106 1 j = 64;
107 1 for(i=1; i<=16; i++)
108 1 {
109 2 if((adc >= (j - ADC_OFFSET)) && (adc <= (j + ADC_OFFSET))) break;
110 2 j += 64;
111 2 }
112 1 ADC_KeyState3 = ADC_KeyState2;
113 1 ADC_KeyState2 = ADC_KeyState1;
114 1 if(i > 16) ADC_KeyState1 = 0;
115 1 else
116 1 {
117 2 ADC_KeyState1 = i;
C51 COMPILER V9.01 CALCULATOR_V2 05/21/2016 12:04:30 PAGE 3
118 2 if((ADC_KeyState3 == ADC_KeyState2) && (ADC_KeyState2 == ADC_KeyState1) &&
119 2 (ADC_KeyState3 > 0) && (ADC_KeyState2 > 0) && (ADC_KeyState1 > 0))
120 2 {
121 3 if(ADC_KeyState == 0)
122 3 {
123 4 KeyCode = i;
124 4 ADC_KeyState = i;
125 4 ADC_KeyHoldCnt = 0;
126 4 }
127 3 if(ADC_KeyState == i)
128 3 {
129 4 if(++ADC_KeyHoldCnt >= 100)
130 4 {
131 5 ADC_KeyHoldCnt = 90;
132 5 KeyCode = i;
133 5 }
134 4 }
135 3 else ADC_KeyHoldCnt = 0;
136 3 }
137 2 }
138 1 }
139
140
141
142 void Send_595(u8 dat)
143 {
144 1 u8 i;
145 1 for(i=0; i<8; i++)
146 1 {
147 2 dat <<= 1;
148 2 P_HC595_SER = CY;
149 2 P_HC595_SRCLK = 1;
150 2 P_HC595_SRCLK = 0;
151 2 }
152 1 }
153
154
155 void DisplayScan(void)
156 {
157 1 Send_595(~T_COM[display_index]);
158 1 Send_595(t_display[LED8[display_index]]);
159 1
160 1 P_HC595_RCLK = 1;
161 1 P_HC595_RCLK = 0;
162 1 if(++display_index >= 8) display_index = 0;
163 1 }
164
165
166
167 void timer0 (void) interrupt TIMER0_VECTOR
168 {
169 1 DisplayScan();
170 1 B_1ms = 1;
171 1 }
172
173 void error()
174 {
175 1 u8 i;
176 1 for(i=0; i<8; i++) LED8[i] = 0x10;
177 1
178 1 state = 0;
179 1 oper = 0;
C51 COMPILER V9.01 CALCULATOR_V2 05/21/2016 12:04:30 PAGE 4
180 1 sign = 1;
181 1
182 1 num1 = 0;
183 1 num2 = 0;
184 1 res = 0;
185 1 }
186
187 void disp(long num)
188 {
189 1 u8 i;
190 1 if (num == 0)
191 1 {
192 2 LED8[7] = 0;
193 2 return;
194 2 }
195 1 if (num >= 0)
196 1 {
197 2 for (i = 0;i < 8;i++)
198 2 if (num == 0) LED8[7 - i] = 0x10;
199 2 else
200 2 {
201 3 LED8[7 - i] = num % 10;
202 3 num = num / 10;
203 3 }
204 2 }
205 1 else
206 1 {
207 2 LED8[0] = 0x11; //negative sign
208 2 num = -num;
209 2 for (i = 0;i < 7;i++)
210 2 if (num == 0) LED8[6 - i] = 0x10;
211 2 else
212 2 {
213 3 LED8[6 - i] = num % 10;
214 3 num = num / 10;
215 3 }
216 2 }
217 1 }
218
219 void calc()
220 {
221 1 switch (oper)
222 1 {
223 2 case 1:
224 2 res = num1 + num2;
225 2 break;
226 2 case 2:
227 2 res = num1 - num2;
228 2 break;
229 2 case 3:
230 2 res = num1 * num2;
231 2 break;
232 2 case 4:
233 2 res = num1 / num2;
234 2 break;
235 2 }
236 1 }
237
238 void react()
239 {
240 1 switch (KeyCode)
241 1 {
C51 COMPILER V9.01 CALCULATOR_V2 05/21/2016 12:04:30 PAGE 5
242 2 case 1:case 2:case 3:case 5:case 6:case 7:case 9:case 10:case 11:case 13:
243 2 if (state == 0)
244 2 {
245 3 num1 = num1 * 10 + sign * key_tab[KeyCode];
246 3 disp(num1);
247 3 }
248 2 else if (state == 1)
249 2 {
250 3 num2 = num2 * 10 + sign * key_tab[KeyCode];
251 3 disp(num2);
252 3 }
253 2 break;
254 2 case 8:
255 2 if (state == 0)
256 2 {
257 3 if (num1 == 0) sign = -1;
258 3 else
259 3 {
260 4 oper = KeyCode / 4;
261 4 disp(oper);
262 4 state = 1;
263 4 sign = 1;
264 4 }
265 3 }
266 2 else if (state == 1)
267 2 {
268 3 if (num2 == 0) sign = -1;
269 3 else
270 3 {
271 4 error();
272 4 return;
273 4 }
274 3 }
275 2 break;
276 2 case 4:case 12:case 16:
277 2 if (state == 0)
278 2 {
279 3 oper = KeyCode / 4;
280 3 disp(oper);
281 3 state = 1;
282 3 }
283 2 else
284 2 {
285 3 error();
286 3 return;
287 3 }
288 2 break;
289 2 case 15:
290 2 if (state == 0)
291 2 {
292 3 res = num1;
293 3 disp(res);
294 3 state = 0;
295 3 num1 = 0;
296 3 num2 = 0;
297 3 oper = 0;
298 3 sign = 1;
299 3 res = 0;
300 3 }
301 2 else if (state == 1)
302 2 {
303 3 calc();
C51 COMPILER V9.01 CALCULATOR_V2 05/21/2016 12:04:30 PAGE 6
304 3 disp(res);
305 3 state = 0;
306 3 num1 = 0;
307 3 num2 = 0;
308 3 sign = 1;
309 3 oper = 0;
310 3 res = 0;
311 3 }
312 2 break;
313 2 default: error();
314 2 }
315 1 KeyCode = 0;
316 1 }
317
318 void main()
319 {
320 1 u8 i;
321 1 u16 j;
322 1
323 1 init();
324 1
325 1 for(i=0; i<8; i++) LED8[i] = 0x10;
326 1
327 1 ADC_KeyState = 0;
328 1 ADC_KeyState1 = 0;
329 1 ADC_KeyState2 = 0;
330 1 ADC_KeyState3 = 0;
331 1 ADC_KeyHoldCnt = 0;
332 1 KeyCode = 0;
333 1 cnt10ms = 0;
334 1
335 1 while(1)
336 1 {
337 2 if(B_1ms)
338 2 {
339 3 B_1ms = 0;
340 3
341 3 if(++cnt10ms >= 10)
342 3 {
343 4 cnt10ms = 0;
344 4 j = Get_ADC10bitResult(4);
345 4 if(j < 1024) CalculateAdcKey(j);
346 4
347 4 }
348 3
349 3 //disp(KeyCode);
350 3 if(KeyCode > 0) react();
351 3
352 3 }
353 2 }
354 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1264 ----
CONSTANT SIZE = 68 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 33 5
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILER V9.01 CALCULATOR_V2 05/21/2016 12:04:30 PAGE 7
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)