Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

MengFanjun的博客

距离蓝桥杯省赛还有10天,要抓紧练习了,不知道是这一届简单还是自己厉害了的原因,用了一天就做出来了,还是叫大佬帮了一个小忙,时间也不少,还能再做几届。 废话不多说,先看原题 请添加图片描述 请添加图片描述

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
#include<reg51.h>
#include<onewire.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int

sfr AUXR = 0x8e;
sfr T2H = 0xD6;
sfr T2L = 0xD7;
sfr IE2 = 0xAF;

sbit R1=P3^0;//第一排初始化(如果跳线帽模式是BTN,则只有最左侧一列能用)
sbit R2=P3^1;//第二排初始化
sbit R3=P3^2;//第三排初始化
sbit R4=P3^3;//第四排初始化

sbit DQ = P1^4;

////////////////////////////0 1 2 3 4 5 6 7 8 9 - 全灭
unsigned char code shuzi[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xbf,0xff};
/////////////////////////// A b C d E F H L P U n
unsigned char code zimu[]={0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0xc7,0x8c,0xc1,0xc8};
//////////////////////////// 0 1 2 3 4 5 6 7
unsigned char code weizhi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00};
////////////////////////// 1 2 3 4 5 6 7 8 灭
unsigned char ledweizhi[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};

int temp;
uchar shi;
uchar ge;
int temp2;
unsigned char count_c=0;
unsigned char second=50;
unsigned char minute=59;
unsigned char hour=23;
uchar count_b=0;
//时钟设置
uchar hour_p;
uchar minute_p;
uchar second_p;
//闹钟设置
uchar hour_n=0;
uchar minute_n=0;
uchar second_n=0;

uchar mode_smg=0;
uchar mode_f=0;
uchar mode_e;
//闹钟计数
uchar count_a;
uchar mode_alarm=0;
uchar alarm_num=0;
void Scan_S7();
void alarm();
void shumaguan_shuzi();
void delay_ms(int ms)
{
int q,w;
for(q = 0;q<ms;q++){
for(w=845;w>0;w--);
}
}
void shumaguan_shuzi(uchar a,uchar b)
{
delay_ms(1);
P2=(P2&0X1f)|0xC0;P0=weizhi[a];
P2=(P2&0X1f)|0xE0;P0=shuzi[b];
delay_ms(1);
}
//数码管字母显示
void shumaguan_zimu(uchar a,uchar b)
{
delay_ms(1);
P2=(P2&0X1f)|0xC0;P0=weizhi[a];
P2=(P2&0X1f)|0xE0;P0=zimu[b];
delay_ms(1);
}
void ledlight(uchar x)
{
P2=(P2&0X1f)|0x80;
P0=ledweizhi[x];
}
void Init_Keys()
{
R1=R2=R3=R4=1;
}
void cls_buzz(void)
{
P2 = (P2&0x1F|0xA0);
P0 = 0x00;
P2 &= 0x1F;
}
void Init_timer0()
{
TMOD = 0x01;
TH0 = (65535 - 50000) / 256;
TL0 = (65535 - 50000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Service_timer0() interrupt 1//定时器0
{
TH0 = (65535 - 50000) / 256;
TL0 = (65535 - 50000) % 256;
count_c++;
if(count_c == 20)
{
second++;
count_c = 0;
}
if(second == 60)
{
minute++;
second=0;
}
if(minute==60)
{
hour++;
minute=0;
if(hour==24) hour=0;
}
}
void Timer1(void) //定时模式
{
TMOD &= 0x0F;
TL1 = (65535-1000)%256;
TH1 = (65535-1000)/256;
TF1 = 0;
TR1 = 0;
ET1 = 1;
}
void Timer1_Service() interrupt 3//定时器1的定时模式
{
count_a++;
if(count_a==200)
{
count_a=0;
alarm_num++;
if(mode_alarm==0)
{
mode_alarm=1;
}
else if(mode_alarm==1)
{
mode_alarm=0;
}
}
}
void led_blink()
{
if(mode_alarm==1)
{
ledlight(0);
}
if(mode_alarm==0)
{
P2=0x80;
P0=0xff;
}
if(R1==0||R2==0||R3==0||R4==0)
{
P2=0x80;
P0=0x00;
TR1=0;
count_a=0;
alarm_num=0;
mode_alarm=0;
}
if(alarm_num==25)
{
TR1=0;
count_a=0;
alarm_num=0;
mode_alarm=0;
}
}
void Timer2(void)
{
T2H = (65535-50000)/256;
T2L = (65535-50000)%256;
EA = 1;
IE2 |=0x04;
AUXR|=0x10;
}
void Timer2_Service() interrupt 12//定时器2的定时模式
{

count_b++;
if(count_b==20)
{
count_b=0;
if(mode_smg==0)
{
mode_smg=1;
}
else if(mode_smg==1)
{
mode_smg=0;
}
}
}
void display_clock()
{
shumaguan_shuzi(0,hour/10);
shumaguan_shuzi(1,hour%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute/10);
shumaguan_shuzi(4,minute%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second/10);
shumaguan_shuzi(7,second%10);
alarm();
led_blink();
while(R4==0)
{
shumaguan_shuzi(5,shi);
shumaguan_shuzi(6,ge);
shumaguan_zimu(7,2);
}
}
void hour_blink()
{
mode_e=1;
if(mode_f==1)
{
if(mode_smg==1)
{
shumaguan_shuzi(0,hour_p/10);
shumaguan_shuzi(1,hour_p%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_p/10);
shumaguan_shuzi(4,minute_p%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_p/10);
shumaguan_shuzi(7,second_p%10);
}
if(mode_smg==0)
{
shumaguan_shuzi(0,11);
shumaguan_shuzi(1,11);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_p/10);
shumaguan_shuzi(4,minute_p%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_p/10);
shumaguan_shuzi(7,second_p%10);
}
}
if(mode_f==2)
{
if(mode_smg==1)
{
shumaguan_shuzi(0,hour_n/10);
shumaguan_shuzi(1,hour_n%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_n/10);
shumaguan_shuzi(4,minute_n%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_n/10);
shumaguan_shuzi(7,second_n%10);
}
if(mode_smg==0)
{
shumaguan_shuzi(0,11);
shumaguan_shuzi(1,11);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_n/10);
shumaguan_shuzi(4,minute_n%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_n/10);
shumaguan_shuzi(7,second_n%10);
}
}


}
void minute_blink()
{
mode_e=2;
if(mode_f==1)
{
if(mode_smg==1)
{
shumaguan_shuzi(0,hour_p/10);
shumaguan_shuzi(1,hour_p%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_p/10);
shumaguan_shuzi(4,minute_p%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_p/10);
shumaguan_shuzi(7,second_p%10);
}
if(mode_smg==0)
{
shumaguan_shuzi(0,hour_p/10);
shumaguan_shuzi(1,hour_p%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,11);
shumaguan_shuzi(4,11);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_p/10);
shumaguan_shuzi(7,second_p%10);
}
}
if(mode_f==2)
{
if(mode_smg==1)
{
shumaguan_shuzi(0,hour_n/10);
shumaguan_shuzi(1,hour_n%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_n/10);
shumaguan_shuzi(4,minute_n%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_n/10);
shumaguan_shuzi(7,second_n%10);
}
if(mode_smg==0)
{
shumaguan_shuzi(0,hour_n/10);
shumaguan_shuzi(1,hour_n%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,11);
shumaguan_shuzi(4,11);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_n/10);
shumaguan_shuzi(7,second_n%10);
}
}
}
void second_blink()
{
mode_e=3;
if(mode_f==1)
{
if(mode_smg==1)
{
shumaguan_shuzi(0,hour_p/10);
shumaguan_shuzi(1,hour_p%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_p/10);
shumaguan_shuzi(4,minute_p%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_p/10);
shumaguan_shuzi(7,second_p%10);
}
if(mode_smg==0)
{
shumaguan_shuzi(0,hour_p/10);
shumaguan_shuzi(1,hour_p%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_p/10);
shumaguan_shuzi(4,minute_p%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,11);
shumaguan_shuzi(7,11);
}
}
if(mode_f==2)
{
if(mode_smg==1)
{
shumaguan_shuzi(0,hour_n/10);
shumaguan_shuzi(1,hour_n%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_n/10);
shumaguan_shuzi(4,minute_n%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,second_n/10);
shumaguan_shuzi(7,second_n%10);
}
if(mode_smg==0)
{
shumaguan_shuzi(0,hour_n/10);
shumaguan_shuzi(1,hour_n%10);
shumaguan_shuzi(2,10);
shumaguan_shuzi(3,minute_n/10);
shumaguan_shuzi(4,minute_n%10);
shumaguan_shuzi(5,10);
shumaguan_shuzi(6,11);
shumaguan_shuzi(7,11);
}
}
}

void temperature_get()
{
uchar high,low;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);



init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();


temp=((high<<4)|(low>>4));//不带小数点
//temp2=low&0x0f*100/16;//取小数点两位
shi=temp%100/10;
ge=temp%10;
}
void display_temp()
{
while(R4==0)
{
shumaguan_shuzi(5,shi);
shumaguan_shuzi(6,ge);
shumaguan_zimu(7,2);
}
}
void alarm()
{
if(hour==hour_n&&minute==minute_n&&second==second_n)
{
P2=(P2&0X1f)|0x80;
P0=0xff;
TR1=1;
}

}
void Scan_S5()
{
if(mode_f==1)
{
if(R3==0)
{
while(R3==0);
if(mode_e==1)
{
hour_p++;
if(hour_p==24) {hour_p=0;}

}
else if(mode_e==2)
{
minute_p++;
if(minute_p==60) {minute_p=0;}
}
else if(mode_e==3)
{
second_p++;
if(second_p==60) {second_p=0;}

}
}
}
if(mode_f==2)
{
if(R3==0)
{
while(R3==0);
if(mode_e==1)
{
hour_n++;
if(hour_n==24) {hour_n=0;}

}
else if(mode_e==2)
{
minute_n++;
if(minute_n==60) {minute_n=0;}
}
else if(mode_e==3)
{
second_n++;
if(second_n==60) {second_n=0;}
}
}
}
}
void Scan_S4()
{
if(mode_f==1)
{
if(R4==0)
{
while(R4==0);
if(mode_e==1)
{
hour_p--;
if(hour_p==0) {hour_p=24;}
}
else if(mode_e==2)
{
minute_p--;
if(minute_p==0) {minute_p=60;}
}
else if(mode_e==3)
{
second_p--;
if(second_p==0) {second_p=60;}
}
}
}
if(mode_f==2)
{
if(R4==0)
{
while(R4==0);
if(mode_e==1)
{
hour_n--;
if(hour_n==0) {hour_n=24;}
}
else if(mode_e==2)
{
minute_n--;
if(minute_n==0) {minute_n=60;}
}
else if(mode_e==3)
{
second_n--;
if(second_n==0) {second_n=60;}
}
}
}

}
void Scan_S6()
{
mode_f=2;
if(R2==0)
{
while(R2==0);
hour_n=hour;
minute_n=minute;
second_n=second;
Timer2();
while(1)
{
hour_blink();
Scan_S5();
Scan_S4();
if(R2==0)
{
while(R2==0) minute_blink();
while(1)
{
minute_blink();
Scan_S5();
Scan_S4();
if(R2==0)
{
while(R2==0)second_blink();
while(1)
{
second_blink();
Scan_S5();
Scan_S4();
if(R2==0)
{
while(R2==0)
{
display_clock();
}
while(1)
{
display_clock();
Scan_S6();
Scan_S7();
}
}
}
}
}
}
}
}
}
void Scan_S7()
{
uchar mode_s7=0;
mode_f=1;
if(R1==0)
{
while(R1==0);
mode_s7++;
hour_p=hour;
minute_p=minute;
second_p=second;
Timer2();
while(1)
{
hour_blink();
Scan_S5();
Scan_S4();
if(R1==0)
{
while(R1==0) minute_blink();
while(1)
{
minute_blink();
Scan_S5();
Scan_S4();
if(R1==0)
{
while(R1==0)second_blink();
while(1)
{
second_blink();
Scan_S5();
Scan_S4();
if(R1==0)
{
while(R1==0)
{
display_clock();
hour=hour_p;
minute=minute_p;
second=second_p;
}
while(1)
{
display_clock();
Scan_S7();
Scan_S6();
}
}
}
}
}
}
}
}
}
void main()
{
Timer1();
Init_Keys();
Init_timer0();
cls_buzz();
while(1)
{
Scan_S6();
Scan_S7();
display_clock();
temperature_get();
display_temp();

}
}

设计思路,就是在做1s间隔的闪烁的时候,需要用到定时器,在做闹钟的触发响应时,也要用到定时器,考到定时器较多,我就定时器学得不好,但是总体来说还是很简单的

评论