1 CURR_NUM EQU 30H ;定义一个变量EQU
2
3 DAT_74164 bit P0.6 ;3~10行:属于头文件,显示定义硬件资源与硬件引脚
4 CLK_74164 bit P0.7
5
6 KEY_COMMON bit P3.2
7 KEY_1 bit p0.5
8 KEY_2 bit p0.4
9
10 DIG_6 bit p0.0
11
12 org 0000H
13 jmp MAIN
14
15 ;*******************************************
16 org 0030H ;Start program from 0030H
17 MAIN:
18 mov P0,#0FFH ;Initialize the port
19 mov P1,#0FFH
20 mov P2,#0FFH
21 mov P3,#0FFH
22
23 mov CURR_NUM,#00H ;Initilize the var,给变量赋值0
24 clr KEY_COMMON ;Clear the common line,公共端清零
25
26 MAIN_LOOP:
27 jb KEY_2,LAB_KEY_1 ;Wait for key down,ADD
28 call DELAY
29 call NUM_ADD
30 jmp NEXT_LOOP
31
32 LAB_KEY_1:
33 jb KEY_1,NEXT_LOOP ;Wait for key down,SUB
34 call DELAY
35 call NUM_SUB
36 jmp NEXT_LOOP
37
38 NEXT_LOOP:
39 call DISPLAY_NUM
40 call DELAY_DISP
41 jmp MAIN_LOOP
42
43 ret
44 ;****************************************************
45 DISPLAY_NUM:
46 ;Get the code of led
47 mov a,CURR_NUM
48 mov dptr,#TAB_LED ;48,49行查断码
49 movc a,@a+dptr
50
51 ;Display the number
52 call sendTo74164
53 clr DIG_6
54
55 ret
56 ;*******************************************
57 ;Send data of A to chip 74LS164
58 sendTo74164:
59 push 07h
60 push acc
61
62 mov r7,#08 ;send 8 bits
63 SEND164_LOOP:
64 clr CLK_74164 ;clear clock-line for reading data
65 rlc a
66 mov DAT_74164,c ;move data-bit to data-line
67 setb CLK_74164 ;send data to 74164
68 djnz r7,SEND164_LOOP
69 clr CLK_74164 ;clear clock-line for reading data
70
71 pop acc
72 pop 07h
73 ret
74 ;****************************************************
75 NUM_ADD:
76 ;Get next value
77 mov a,CURR_NUM ;当前变量赋给a
78 mov dptr,#TAB_NEXT ;表中的内容加载到指针
79 movc a,@a+dptr ;查表
80 mov CURR_NUM,a ;把查表查出来的值赋给a
81
82 ret
83 ;****************************************************
84 NUM_SUB:
85 ;Get previos value
86 mov a,CURR_NUM
87 mov dptr,#TAB_PRE
88 movc a,@a+dptr
89 mov CURR_NUM,a
90
91 ret
92 ;****************************************************
93 DELAY:
94 mov r7,#200
95 DELAY_LOOP:
96 mov r6,#200
97 djnz r6,$
98 mov r6,#200
99 djnz r6,$
100
101 djnz r7,DELAY_LOOP
102 ret
103 ;****************************************************
104 DELAY_DISP:
105 mov r7,#10
106 DELAY_DISP_LOOP:
107 mov r6,#200
108 djnz r6,$
109 mov r6,#200
110 djnz r6,$
111
112 djnz r7,DELAY_DISP_LOOP
113 ret
114 ;****************************************************
115 ;****************************************************
116 ;LED code
117 TAB_LED:
118 DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
119 ;===================================================
120 ;For sub
121 TAB_PRE:
122 DB 09H,00H,01H,02H,03H,04H,05H,06H,07H,08H
123 ;===================================================
124 ;For add
125 TAB_NEXT:
126 DB 01H,02H,03H,04H,05H,06H,07H,08H,09H,00H
127 ;****************************************************
128 END