51单片机汇编实时时钟课设

实时时钟课题要求:

实时时钟系统设计
1、用 1 个拨动开关设定“工作状态”和“设置状态”;
2、“设置状态”下,用 4 个独立按键实现时间设置:“+”“-”、“移位”、“确认”,
对年(2 位)、月、日、时、分、秒进行每 1 位的数值设置;
3、采用 DS1302 时钟芯片,采用 LCD 进行时钟显示


Proteus7.8仿真效果如下:

(注:Proteus仿真源文件见附件)


汇编代码如下:
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
/*******************************实时时钟实验(Zidone)*************************************************/
/***********LCD12864相关定义******************/
//指定管脚
LCD_E BIT P2.0 /*12864使能口*/
LCD_DI BIT P3.0 /*12864数据命令选择*/
LCD_RW BIT P3.1 /*12864读写选择*/
LCD_RST BIT P3.3 /*12864复位*/
LCD_CS1 BIT P3.2 /*12864选屏*/
LCD_CS2 BIT P2.1 /*12864选屏*/
//指定存放地址
LCD_CMD EQU 30H /*12864命令存放地址*/
LCD_DATA EQU 31H /*12864数据存放地址*/
LCD_PAGE EQU 32H /*起始B8H 存放页数(用于字符或汉字)*/
LCD_COLUMN EQU 33H /*起始40H 存放列数*/
/***********DS1302相关定义******************/
//指定管脚
T_CLK BIT P2.6 /*DS1302时钟口*/
T_IO BIT P2.7 /*DS1302输入输出口*/
T_RST BIT P2.5 /*DS1302复位口*/
//指定存放地址
SECOND EQU 40H /*秒分时日月周年低位年高位存储地址*/
MINUTE EQU 41H
HOUR EQU 42H
DAY EQU 43H
MONTH EQU 44H
WEEK EQU 45H
YEARL EQU 46H
YEARH EQU 47H
//命令字
WR_SECOND EQU 80H
RD_SECOND EQU 81H
WR_MINUTE EQU 82H
RD_MINUTE EQU 83H
WR_HOUR EQU 84H
RD_HOUR EQU 85H
WR_DAY EQU 86H
RD_DAY EQU 87H
WR_MONTH EQU 88H
RD_MONTH EQU 89H
WR_WEEK EQU 8AH
RD_WEEK EQU 8BH
WR_YEAR EQU 8CH
RD_YEAR EQU 8DH
WR_PROTECT EQU 8EH
RD_PROTECT EQU 8FH
/*********用户开关及按键管脚定义***************/
MODE_SELECT BIT P0.0 /*模式选择管脚:默认高电平,1-正常计时模式,0-时间设定模式*/
BIT_SELECT BIT P0.1 /*移位选择需要修改的参数,使其闪烁,表明选中*/
MODE_INC BIT P0.2 /*加*/
MODE_DEC BIT P0.3 /*减*/
MODE_ENSURE BIT P0.4 /*确认*/
/**************主程序**************************/
ORG 0000H
LJMP MAIN
ORG 0100H
MAIN:
MOV SP,#60H
//DS1302初始默认时间设置
MOV YEARH,#20H
MOV YEARL,#19H
MOV MONTH,#01H
MOV DAY,#05H
MOV WEEK,#06H
MOV HOUR,#21H
MOV MINUTE,#18H
MOV SECOND,#00H

LCALL DS1302Init /*DS1302初始设置*/
LCALL Lcd12864Init /*LCD12864初始化*/
//选择左半屏
SETB LCD_CS2
CLR LCD_CS1
//左半屏需要显示的固定汉字
LCALL Write_NIAN /*’年‘*/
LCALL Write_SHI /*’时‘*/
LCALL Write_XING /*’星‘*/
LCALL Write_QI_1 /*’期‘左一半*/
//选择右半屏
SETB LCD_CS1
CLR LCD_CS2
//右半屏需要显示的固定汉字
LCALL Write_YUE /*’月‘*/
LCALL Write_RI /*‘日’*/
LCALL Write_FEN /*‘分’*/
LCALL Write_MIAO /*‘秒’*/
LCALL Write_QI_2 /*‘期’右边一半*/

LOOP1:
JB MODE_SELECT,LOOP2 /*状态选择,闭合低电平为时间设置状态,断开高电平(默认状态)正常计时状态*/
LCALL DELAY10MS /*软件消抖*/
JB MODE_SELECT,LOOP2
LCALL DS1302SetTime
LOOP2:
LCALL DS1302GetData
LCALL DS1302ShowTime
LCALL DELAY800MS
LJMP LOOP1
/***************通过按键设定DS1302的起始时间**************************/
/*
首先判断模式选择开关:
1.没有按下,执行正常显示的程序(也是弹开后重新执行显示程序)
2.按下后,执行时间设置程序
其次判断移位选择开关:
1.没有按下,默认选择修改年高位
2.按下,切换需要修改的参数(顺序为:如下标识),并使其闪烁,证明选中
加按钮:
1.没有按下,等待
2.按下,判断选择的是哪一位,执行相应位的加程序
减按钮:
1.没有按下,等待
2.按下,判断选择的是哪一位,执行相应位的加程序
确认按钮:
1.没有按下,等待
2.按下,锁定当前的值写入DS1302

使选中字符闪烁:
无法在主循环延时,可以尝试开一个定时器中断,每隔一秒插叙状态,执行闪烁,写空白字。
*/
DS1302SetTime:
MOV R0,#8 /*R0默认为周*/

SELECT_BIT_IF_INC:
JB BIT_SELECT,EEE /*弹开的说明是选择好了,判断执行的是加还是减*/
LCALL DELAY10MS
JB BIT_SELECT,EEE
CJNE R0,#0,SELECT_BIT
MOV R0,#8
EEE: LJMP IF_INC

SELECT_BIT:
DEC R0
JNB BIT_SELECT,$ /*锁定,避免按下后不放就一直加*/
//加判断
IF_INC:
JB MODE_INC,GGG /*没有按下,显示默认状态(被迫这么些短转移JB距离不够,只好中转)*/
LCALL DELAY10MS
JB MODE_INC,GGG
CJNE R0,#8,SET_SECOND
LJMP SET_WEEK
//减判断
GGG:
JB MODE_DEC,AAA /*没有按下,显示默认状态(被迫这么些短转移JB距离不够,只好中转)*/
LCALL DELAY10MS
JB MODE_DEC,AAA
CJNE R0,#8,HHH
LJMP SET_WEEK_2

HHH: LJMP SET_SECOND_2
AAA:
LJMP SHOW_PROCESS
/**************************************************************************************************/
//设置周+(8)
SET_WEEK:
MOV A,WEEK
CJNE A,#7,WEEK_INC
MOV WEEK,#1

LJMP SHOW_PROCESS

WEEK_INC:
MOV A,WEEK
ADD A,#1
DA A
MOV WEEK,A
LJMP SHOW_PROCESS

//设置秒+(7)
SET_SECOND:
CJNE R0,#7,SET_MINUTE
MOV A,SECOND
CJNE A,#59H,SECOND_INC
MOV SECOND,#00H
LJMP SHOW_PROCESS

SECOND_INC:
MOV A,SECOND
ADD A,#1
DA A
MOV SECOND,A
LJMP SHOW_PROCESS

//设置分+(6)
SET_MINUTE:
CJNE R0,#6,SET_HOUR
MOV A,MINUTE
CJNE A,#59H,MINUTE_INC
MOV MINUTE,#00H
LJMP SHOW_PROCESS

MINUTE_INC:
MOV A,MINUTE
ADD A,#1
DA A
MOV MINUTE,A
LJMP SHOW_PROCESS

//设置时+(5)
SET_HOUR:
CJNE R0,#5,SET_DAY
MOV A,HOUR
CJNE A,#23H,HOUR_INC
MOV HOUR,#00
LJMP SHOW_PROCESS

HOUR_INC:
MOV A,HOUR
ADD A,#1
DA A
MOV HOUR,A
LJMP SHOW_PROCESS

//设置天+(4)
SET_DAY:
CJNE R0,#4,SET_MONTH
MOV A,DAY
CJNE A,#31H,DAY_INC
MOV DAY,#1
LJMP SHOW_PROCESS

DAY_INC:
MOV A,DAY
ADD A,#1
DA A
MOV DAY,A
LJMP SHOW_PROCESS

//设置月+(3)
SET_MONTH:
CJNE R0,#3,SET_YEARL
MOV A,MONTH
CJNE A,#12H,MONTH_INC
MOV MONTH,#1
LJMP SHOW_PROCESS

MONTH_INC:
MOV A,MONTH
ADD A,#1
DA A
MOV MONTH,A
LJMP SHOW_PROCESS

//设置年低位+(2)
SET_YEARL:
CJNE R0,#2,SET_YEARH
MOV A,YEARL
CJNE A,#99H,YEARL_INC
MOV YEARL,#00H
LJMP SHOW_PROCESS

YEARL_INC:
MOV A,YEARL
ADD A,#1
DA A
MOV YEARL,A
LJMP SHOW_PROCESS

//设置年高位+(1)
SET_YEARH:
CJNE R0,#1,FFF
MOV A,YEARH
CJNE A,#99H,YEARH_INC
MOV YEARH,#00H
LJMP SHOW_PROCESS

YEARH_INC:
MOV A,YEARH
ADD A,#1
DA A
MOV YEARH,A
LJMP SHOW_PROCESS

FFF: LJMP SET_WEEK //回调,这个特别需要注意(头疼死我了)
/************************************************************************/
//减部分咯
//设置周-(8)
SET_WEEK_2:
MOV A,WEEK
CJNE A,#1,WEEK_DEC
MOV WEEK,#7

LJMP SHOW_PROCESS

WEEK_DEC:
MOV A,WEEK
CLR AC
SUBB A,#1
DA A
MOV WEEK,A
LJMP SHOW_PROCESS

//设置秒-(7)
SET_SECOND_2:
CJNE R0,#7,SET_MINUTE_2
MOV A,SECOND
CJNE A,#00H,SECOND_DEC
MOV SECOND,#59H
LJMP SHOW_PROCESS

SECOND_DEC:
MOV A,SECOND
CLR AC
SUBB A,#1
DA A
MOV SECOND,A
LJMP SHOW_PROCESS

//设置分-(6)
SET_MINUTE_2:
CJNE R0,#6,SET_HOUR_2
MOV A,MINUTE
CJNE A,#00H,MINUTE_DEC
MOV MINUTE,#59H
LJMP SHOW_PROCESS

MINUTE_DEC:
MOV A,MINUTE
CLR AC
SUBB A,#1
DA A
MOV MINUTE,A
LJMP SHOW_PROCESS

//设置时-(5)
SET_HOUR_2:
CJNE R0,#5,SET_DAY_2
MOV A,HOUR
CJNE A,#00H,HOUR_DEC
MOV HOUR,#23H
LJMP SHOW_PROCESS

HOUR_DEC:
MOV A,HOUR
CLR AC
SUBB A,#1
DA A
MOV HOUR,A
LJMP SHOW_PROCESS

//设置天-(4)
SET_DAY_2:
CJNE R0,#4,SET_MONTH_2
MOV A,DAY
CJNE A,#01H,DAY_DEC
MOV DAY,#31H
LJMP SHOW_PROCESS

DAY_DEC:
MOV A,DAY
CLR AC
SUBB A,#1
DA A
MOV DAY,A
LJMP SHOW_PROCESS

//设置月-(3)
SET_MONTH_2:
CJNE R0,#3,SET_YEARL_2
MOV A,MONTH
CJNE A,#01H,MONTH_DEC
MOV MONTH,#12H
LJMP SHOW_PROCESS

MONTH_DEC:
MOV A,MONTH
CLR AC
SUBB A,#1
DA A
MOV MONTH,A
LJMP SHOW_PROCESS

//设置年低位-(2)
SET_YEARL_2:
CJNE R0,#2,SET_YEARH_2
MOV A,YEARL
CJNE A,#00H,YEARL_DEC
MOV YEARL,#99H
LJMP SHOW_PROCESS

YEARL_DEC:
MOV A,YEARL
CLR AC
SUBB A,#1
DA A
MOV YEARL,A
LJMP SHOW_PROCESS

//设置年高位-(1)
SET_YEARH_2:
CJNE R0,#1,III
MOV A,YEARH
CJNE A,#00H,YEARH_DEC
MOV YEARH,#99H
LJMP SHOW_PROCESS

YEARH_DEC:
MOV A,YEARH
CLR AC
SUBB A,#1
DA A
MOV YEARH,A
LJMP SHOW_PROCESS

III: LJMP SET_WEEK_2 //回调,这个特别需要注意(头疼死我了)
//把修改后的结果显示
SHOW_PROCESS:
JNB MODE_INC,$ //加按键没有弹起,就等待弹起
JNB MODE_DEC,$ //减按键没有弹起,就等待弹起
LCALL DS1302ShowTime //弹起了说明一次操作完成,就可以把结果显示出来
JB MODE_ENSURE,BBB //确认键没有按下的话,跳转回去等待移位选择
LCALL DELAY10MS
JB MODE_ENSURE,BBB
LJMP DDD
BBB: LJMP SELECT_BIT_IF_INC
DDD: LCALL DS1302Init //如果确认键按下就把初始值写入DS1302
RET

/***************时间显示**************************/
DS1302ShowTime:
//左半屏
SETB LCD_CS2
CLR LCD_CS1
//左半屏读取到的内容
LCALL Write_YEARH /*年份的高两位,如“20”*/
LCALL Write_YEARL /*年份的低两位,如“18”*/
LCALL Write_MONTH_1 /*月份的十位,也就是左半部分,如12月的“1”*/
LCALL Write_HOUR /*小时的两位,如12点10分的“12”*/
LCALL Write_MINUTE /*分钟的两位,如12点10分的“10”*/
//右半屏
SETB LCD_CS1
CLR LCD_CS2
//右半屏读取到的内容
LCALL Write_MONTH_2 /*月份的个位,也就是右半部分,如12月的“2”*/
LCALL Write_DAY /*日期的两位,如30号的“30”*/
LCALL Write_SECOND /*秒的两位,如45秒的“45”*/
LCALL Write_WEEK /*周的一位汉字,如星期一的“一”*/
RET
/**************设置DS1302初始参数*************/
DS1302Init:
CLR T_RST
CLR T_CLK
SETB T_RST
MOV B,#WR_PROTECT /*写保护寄存器地址8EH*/
LCALL DS1302WriteByte
MOV B,#00H /*WP=0允许写入*/
LCALL DS1302WriteByte
SETB T_CLK /*锁定*/
CLR T_RST
MOV R0,#SECOND
MOV R7,#7 /*秒,分,时,日,月,星期,年*/
MOV R1,#WR_SECOND
WR_LOOP6:
CLR T_RST
CLR T_CLK
SETB T_RST
MOV B,R1
LCALL DS1302WriteByte
MOV A,@R0
MOV B,A
LCALL DS1302WriteByte
INC R0
INC R1 /*写命令字是隔一个地址的*/
INC R1
SETB T_CLK /*锁定*/
CLR T_RST
DJNZ R7,WR_LOOP6
//重新锁定写保护寄存器
CLR T_RST
CLR T_CLK
SETB T_RST
MOV B,#WR_PROTECT
LCALL DS1302WriteByte
MOV B,#80H
LCALL DS1302WriteByte
SETB T_CLK
CLR T_RST
RET
/************读DS1302相关时间数据***********/
DS1302GetData:
MOV R0,#SECOND
MOV R7,#7
MOV R1,#RD_SECOND
RD_LOOP2:
CLR T_RST
CLR T_CLK
SETB T_RST
MOV B,R1
LCALL DS1302WriteByte

LCALL DS1302ReadByte
MOV @R0,A
INC R0
INC R1
INC R1
SETB T_CLK
CLR T_RST
DJNZ R7,RD_LOOP2
RET
/***********串行读出DS1302一字节子程序*******/
DS1302ReadByte:
MOV R4,#8
RD_LOOP1:
MOV C,T_IO
RRC A
SETB T_CLK
CLR T_CLK
DJNZ R4,RD_LOOP1
RET
/***********串行写入DS1302一字节子程序*******/
DS1302WriteByte:
MOV R4,#8
WR_LOOP5:
MOV A,B
RRC A
MOV B,A
MOV T_IO,C
SETB T_CLK
CLR T_CLK
DJNZ R4,WR_LOOP5
RET
/***********写字子程序*************************/
/*1.固定部分*/
//注:一个汉字占两页中间空一页,正巧使用完8页
//写‘年’
Write_NIAN:
MOV LCD_PAGE,#0B8H ;
MOV LCD_COLUMN,#67H
MOV DPTR,#HAN_NIAN
MOV A,#00H
LCALL WriteChinese
RET
//写‘月’
Write_YUE:
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#49H
MOV DPTR,#HAN_YUE
MOV A,#00H
LCALL WriteChinese
RET
//写‘日’
Write_RI:
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#6CH
MOV DPTR,#HAN_RI
MOV A,#00H
LCALL WriteChinese
RET
//写‘时’
Write_SHI:
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#5EH
MOV DPTR,#HAN_SHI
MOV A,#00H
LCALL WriteChinese
RET
//写‘分’
Write_FEN:
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#40H
MOV DPTR,#HAN_FEN
MOV A,#00H
LCALL WriteChinese
RET
//写‘秒’
Write_MIAO:
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#63H
MOV DPTR,#HAN_MIAO
MOV A,#00H
LCALL WriteChinese
RET
//写‘星’
Write_XING:
MOV LCD_PAGE,#0BEH
MOV LCD_COLUMN,#67H
MOV DPTR,#HAN_XING
MOV A,#00H
LCALL WriteChinese
RET
//写‘期’左边一半
Write_QI_1:
MOV LCD_PAGE,#0BEH
MOV LCD_COLUMN,#78H
MOV DPTR,#YING_QI_1
MOV A,#00H
LCALL WriteChar
RET
//写‘期’右边一半
Write_QI_2:
MOV LCD_PAGE,#0BEH
MOV LCD_COLUMN,#40H
MOV DPTR,#YING_QI_2
MOV A,#00H
LCALL WriteChar
RET
/*2.从DS1302获取部分*/
Write_YEARH:
/*
注:YEARH=20H手动给定,A=20H/10H=02H->20H,正巧度过两个字(8*2一个字符,20H为两个字符)
*/
//‘2’
MOV A,YEARH
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#43H
MOV DPTR,#YING_LING
LCALL WriteChar

//‘0’
MOV A,B
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#4CH
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_YEARL:
MOV A,YEARL
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#55H
MOV DPTR,#YING_LING
LCALL WriteChar

MOV A,B
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#5EH
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_MONTH_1:
//显示月份的十位,个位在右半屏显示
MOV A,MONTH
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#78H
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_HOUR:
MOV A,HOUR
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#4CH
MOV DPTR,#YING_LING
LCALL WriteChar

MOV A,B
SWAP A
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#53H
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_MINUTE:
MOV A,MINUTE
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#6FH
MOV DPTR,#YING_LING
LCALL WriteChar

MOV A,B
SWAP A
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#78H
MOV DPTR,#YING_LING
LCALL WriteChar
RET
//月份的右半部份,即个位
Write_MONTH_2:
MOV A,MONTH
MOV B,#10H
DIV AB
MOV A,B
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#40H
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_DAY:
MOV A,DAY
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#5AH
MOV DPTR,#YING_LING
LCALL WriteChar

MOV A,B
SWAP A
MOV LCD_PAGE,#0B8H
MOV LCD_COLUMN,#63H
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_SECOND:
MOV A,SECOND
MOV B,#10H
DIV AB
SWAP A
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#51H
MOV DPTR,#YING_LING
LCALL WriteChar

MOV A,B
SWAP A
MOV LCD_PAGE,#0BBH
MOV LCD_COLUMN,#5AH
MOV DPTR,#YING_LING
LCALL WriteChar
RET
Write_WEEK:
/*
00H-周一;10H-周二;20H-周三;30H-周四;40H-周五;50H-周六;60H-周日(周七)
而BCD码取值范围为01H-07H,故减一处理
*/
MOV A,WEEK
DEC A
SWAP A
MOV B,A
ADD A,B
MOV LCD_PAGE,#0BEH
MOV LCD_COLUMN,#49H
MOV DPTR,#HAN_YI
LCALL WriteChinese
RET
/*****************写汉字**********************/
WriteChinese:
//汉字上半部分8*16
MOV R3,A
MOV LCD_CMD, LCD_PAGE
LCALL Lcd12864WriteCmd
MOV LCD_CMD, LCD_COLUMN
LCALL Lcd12864WriteCmd
MOV R2, #16
WR_LOOP1:
MOV A,R3
MOVC A, @A+DPTR
MOV LCD_DATA, A
LCALL Lcd12864WriteData
INC DPTR
DJNZ R2, WR_LOOP1
//汉字下半部分8*16
INC LCD_PAGE
MOV LCD_CMD, LCD_PAGE
LCALL Lcd12864WriteCmd
MOV LCD_CMD, LCD_COLUMN
LCALL Lcd12864WriteCmd
MOV R2, #16
WR_LOOP2:
MOV A,R3
MOVC A, @A+DPTR
MOV LCD_DATA, A
LCALL Lcd12864WriteData
INC DPTR
DJNZ R2, WR_LOOP2
RET
/*******************写字符**********************/
WriteChar:
//英文上半部分
MOV R3,A
MOV LCD_CMD, LCD_PAGE
LCALL Lcd12864WriteCmd
MOV LCD_CMD, LCD_COLUMN
LCALL Lcd12864WriteCmd
MOV R2,#8
WR_LOOP3:
MOV A,R3
MOVC A, @A+DPTR
MOV LCD_DATA, A
LCALL Lcd12864WriteData
INC DPTR
DJNZ R2, WR_LOOP3
//英文下半部分
INC LCD_PAGE
MOV LCD_CMD, LCD_PAGE
LCALL Lcd12864WriteCmd
MOV LCD_CMD, LCD_COLUMN
LCALL Lcd12864WriteCmd
MOV R2, #8
WR_LOOP4:
MOV A,R3
MOVC A, @A+DPTR
MOV LCD_DATA, A
LCALL Lcd12864WriteData
INC DPTR
DJNZ R2, WR_LOOP4
RET
/**************LCD12864初始化*****************/
Lcd12864Init:
CLR LCD_RST
LCALL DELAY100US
SETB LCD_RST

SETB LCD_CS1
CLR LCD_CS2
MOV LCD_CMD, #3EH
LCALL Lcd12864WriteCmd
CLR LCD_CS1
SETB LCD_CS2
MOV LCD_CMD, #3EH
LCALL Lcd12864WriteCmd

SETB LCD_CS1
CLR LCD_CS2
MOV LCD_CMD, #3FH
LCALL Lcd12864WriteCmd
CLR LCD_CS1
SETB LCD_CS2
MOV LCD_CMD, #3FH
LCALL Lcd12864WriteCmd
RET
/***************读状态*************************/
Lcd12864ReadBusy:
MOV P1, #0FFH
ReadBusyFlagLOOP:
CLR LCD_E
LCALL LcdDelay
CLR LCD_DI
SETB LCD_RW
LCALL LcdDelay
SETB LCD_E
LCALL LcdDelay
MOV A, P1
LCALL LcdDelay
CLR LCD_E
LCALL LcdDelay
CLR LCD_RW
CLR LCD_DI
SETB LCD_E
JB ACC.7, ReadBusyFlagLOOP
RET
/**************LCD12864写命令*****************/
Lcd12864WriteCmd:
LCALL Lcd12864ReadBusy
CLR LCD_E
LCALL LcdDelay
CLR LCD_DI
CLR LCD_RW
LCALL LcdDelay
SETB LCD_E
LCALL LcdDelay
MOV P1, LCD_CMD
LCALL LcdDelay
CLR LCD_E
LCALL LcdDelay
SETB LCD_DI
SETB LCD_RW
SETB LCD_E
RET
/**************LCD12864写数据*****************/
Lcd12864WriteData:
LCALL Lcd12864ReadBusy
CLR LCD_E
LCALL LcdDelay
SETB LCD_DI
CLR LCD_RW
LCALL LcdDelay
SETB LCD_E
LCALL LcdDelay
MOV P1, LCD_DATA
LCALL LcdDelay
CLR LCD_E
LCALL LcdDelay
SETB LCD_DI
SETB LCD_RW
SETB LCD_E
RET
/***************LCD时序延时*************************/
LcdDelay:
NOP
RET
/*************LCD复位用延时*************************/
DELAY100US:
MOV R6,#13H
DL0:
MOV R5,#01H
DJNZ R5,$
DJNZ R6,DL0
RET
/***************闪烁用延时**************************/
DELAY_1S:
DEL1: MOV R5,#10;1us
DEL2: MOV R6,#200;10*1us
DEL3: MOV R7,#249;200*10*1us
DEL4: DJNZ R7,DEL4;249*200*10*2us
DJNZ R6,DEL3;200*10*2us
DJNZ R5,DEL2;10*2us
RET
/**************消抖用延时******************************/
DELAY10MS: ;误差 0us
MOV R7,#01H
DL1:
MOV R6,#26H
DL2:
MOV R5,#82H
DJNZ R5,$
DJNZ R6,DL2
DJNZ R7,DL1
RET
/**************LCD显示用延时******************************/
DELAY800MS: ;误差 0us
MOV R7,#5FH
DL3:
MOV R6,#8AH
DL4:
MOV R5,#1DH
DJNZ R5,$
DJNZ R6,DL0
DJNZ R7,DL1
NOP
NOP
RET

/**************显示的内容(字模)**********************/
/*
年(0) 月(1) 日(2) 星(3) 期(4) 一(5) 二(6) 三(7)
四(8) 五(9) 六(10) 七(11) 时(12) 分(13) 秒(14) 0(15)
1(16) 2(17) 3(18) 4(19) 5(20) 6(21) 7(22) 8(23) 9(24)
*/
HAN_NIAN:
DB 0FFH,0DFH,0E7H,038H,0BBH,0BBH,0BBH,0BBH
DB 003H,0BBH,0BBH,0BBH,0BBH,0FBH,0FFH,0FFH
DB 0FBH,0FBH,0FBH,0F8H,0FBH,0FBH,0FBH,0FBH
DB 000H,0FBH,0FBH,0FBH,0FBH,0FBH,0FBH,0FFH;年0
HAN_YUE:
DB 0FFH,0FFH,0FFH,001H,0DDH,0DDH,0DDH,0DDH
DB 0DDH,0DDH,0DDH,0DDH,001H,0FFH,0FFH,0FFH
DB 07FH,0BFH,0CFH,0F0H,0FDH,0FDH,0FDH,0FDH
DB 0FDH,0FDH,0BDH,07DH,080H,0FFH,0FFH,0FFH;月1
HAN_RI:
DB 0FFH,0FFH,0FFH,001H,07DH,07DH,07DH,07DH
DB 07DH,07DH,07DH,001H,0FFH,0FFH,0FFH,0FFH
DB 0FFH,0FFH,0FFH,000H,0BFH,0BFH,0BFH,0BFH
DB 0BFH,0BFH,0BFH,000H,0FFH,0FFH,0FFH,0FFH;日2
HAN_XING:
DB 0FFH,0FFH,0FFH,041H,0D5H,0D5H,0D5H,015H
DB 0D5H,0D5H,0D5H,0C1H,0FFH,0FFH,0FFH,0FFH
DB 0FFH,0BBH,0BDH,0B6H,0B6H,0B6H,0B6H,080H
DB 0B6H,0B6H,0B6H,0B6H,0BEH,0BFH,0FFH,0FFH;星3
YING_QI_1:
DB 0FFH,0FDH,000H,06DH,06D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FFH,0DFH,0DFH,0C0H,0DFH,0DFH,0FFH;116
YING_ER:
DB 0FFH,08FH,0F7H,0F7H,0F7H,0F7H,00FH,0FFH
DB 0FFH,0CFH,0D7H,0DBH,0DDH,0DEH,0CFH,0FFH;217
YING_SAN:
DB 0FFH,0CFH,0F7H,0F7H,0F7H,077H,08FH,0FFH
DB 0FFH,0E7H,0DFH,0DEH,0DEH,0DDH,0E3H,0FFH;318
YING_SI:
DB 0FFH,0FFH,07FH,0BFH,0CFH,007H,0FFH,0FFH
DB 0FFH,0F9H,0FAH,0DBH,0DBH,0C0H,0DBH,0DBH;419
YING_WU:
DB 0FFH,007H,077H,077H,077H,0F7H,0F7H,0FFH
DB 0FFH,0E6H,0DFH,0DFH,0DFH,0EEH,0F1H,0FFH;520
YING_LIU:
DB 0FFH,01FH,0EFH,077H,077H,06FH,0FFH,0FFH
DB 0FFH,0F0H,0EEH,0DFH,0DFH,0DFH,0E0H,0FFH;621
YING_QI:
DB 0FFH,0E7H,0F7H,0F7H,077H,097H,0E7H,0FFH
DB 0FFH,0FFH,0FFH,0C1H,0FEH,0FFH,0FFH,0FFH;722
YING_BA:
DB 0FFH,08FH,077H,0F7H,0F7H,077H,08FH,0FFH
DB 0FFH,0E3H,0DDH,0DEH,0DEH,0DDH,0E3H,0FFH;823
YING_JIU:
DB 0FFH,00FH,0F7H,0F7H,0F7H,0EFH,01FH,0FFH
DB 0FFH,0FEH,0EDH,0DDH,0DDH,0EEH,0F0H,0FFH;924

HAN_GAN:
DB 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF
DB 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF

/***************结束*************************/
END

(注:原工程文件见附件)


说明:

目前没有把当前选择的修改闪烁或者加下划线凸显出来,小伙伴们可以进行改进提升一下。


附件:
仿真

链接:https://pan.baidu.com/s/1nihf6FLmdjwm028_znZzUA
提取码:c9u5

程序

链接:https://pan.baidu.com/s/1dduZzdRvRdMlXlMDa-BBqQ
提取码:tkzc