基于单片机的计算器(三位)

计算器

DBUF        EQU      30H

TEMP     EQU      40H

YJ         EQU          50H                                ;结果存放

YJ1        EQU          51H                                ;中间结果存放

GONG     EQU          52H                             ;功能键存放

DIN       BIT            0B0H                           ;P3.0

CLK        BIT        0B1H                           ;P3.1

                   ORG       00H

START:      MOV      R3,#0               ;初始化显示为空

                   MOV      GONG,#0

                   MOV      30H,#10H

                   MOV      31H,#10H

                   MOV      32H,#10H

                   MOV      33H,#10H

                   MOV      34H,#10H

MLOOP:   CALL     DISP              ;PAN调显示子程序

WAIT:        CALL     TESTKEY                  ;判断有无按键

                   JZ      WAIT                  

                   CALL    GETKEY              ;读键

                   INC     R3                  ;按键个数

                   CJNE    A,#0,NEXT1          ;判断是否数字键

                   LJMP    E1                          ;转数字键处理

NEXT1:     CJNE    A,#1,NEXT2

                   LJMP    E1

NEXT2:     CJNE    A,#2,NEXT3

                   LJMP    E1

NEXT3:     CJNE    A,#3,NEXT4

                   LJMP    E1

NEXT4:     CJNE    A,#4,NEXT5

                   LJMP    E1

NEXT5:     CJNE    A,#5,NEXT6

                   LJMP    E1

NEXT6:     CJNE    A,#6,NEXT7

                   LJMP    E1

NEXT7:     CJNE    A,#7,NEXT8

                   LJMP    E1

NEXT8:     CJNE    A,#8,NEXT9

                   LJMP    E1

NEXT9:     CJNE    A,#9,NEXT10

                   LJMP    E1

NEXT10:   CJNE      A,#10,NEXT11        ;判断是否功能键

                   LJMP    E2                ;转功能键处理

NEXT11:   CJNE      A,#11,NEXT12

                   LJMP    E2

NEXT12:   CJNE      A,#12, NEXT13

                   LJMP     E2

NEXT13:   CJNE      A,#13,NEXT14

                   LJMP     E2

NEXT14:   CJNE      A,#14,NEXT15

                   LJMP     E2

NEXT15:   LJMP     E3                         ;判断是否清除键

E1:             CJNE      R3,#1,N1                    ;判断第几次按键

                   LJMP    E11                         ;为第一个数字

N1:             CJNE      R3,#2,N2

                   LJMP    E12               ;为第二个数字

N2:             CJNE    R3,#3,N3

                   LJMP    E13               ;为第三个数字

N3:             LJMP    E3                ;第四个数字转溢出

E11:       MOV              R4,A                       ;输入值暂存R4

                   MOV      34H,A             ;输入值送显示缓存

                   MOV      33H,#10H

                   MOV      32H,#10H

                   LJMP      MLOOP                   ;等待再次输入

E12:         MOV      R7,A              ;个位数暂存R7

                   MOV      B,#10

                   MOV      A,R4

                   MUL      AB                                        ;十位数

                   ADD                   A,R7

                   MOV      R4,A                 ;输入值存R4

                   MOV      32H,#10H                       ;输入值送显示缓存

                   MOV      33H,34H

                   MOV      34H,R7

                   LJMP     MLOOP

E13:       MOV      R7,A

                   MOV      B,#10

                   MOV      A,R4

                   MUL      AB

                   JB         OV,E3                          ;输入溢出

                   ADD       A,R7

                   JB         CY,E3                ;输入溢出

                  MOV      R4,A

                   MOV      32H,33H                                ;输入值送显示缓存

                   MOV      33H,34H

                   MOV      34H,R7

                   LJMP     MLOOP

E3:             MOV      R3,#0                 ;按键次数清零

                   MOV      R4,#0                ;输入值清零

                   MOV      YJ,#0                ;计算结果清零

                   MOV      GONG,#0          ;功能键设为零

                   MOV      30H,#10H                   ;显示清空

                   MOV      31H,#10H

                   MOV      32H,#10H

                   MOV      33H,#10H

                   MOV       34H,#10H

                   LJMP     MLOOP

E2:             MOV      34H,#10H

                   MOV      33H,#10H

                   MOV      32H,#10H

                   MOV      R0,GONG                  ;与上次功能键交换

                   MOV      GONG,A

                   MOV      A,R0

                   CJNE      A,#10,N21         ;判断功能键

                   LJMP     JIA                 ;“+”

N21:         CJNE      A,#11,N22

                   LJMP     JIAN               ;“-”

N22:     CJNE        A,#12,N23

                   LJMP      CHENG                     ;“*”

N23:      CJNE      A,#13,N24

                   LJMP     CHU              ;“/”

N24:      CJNE     A,#0,N25

                   LJMP     FIRST              ;**按功能键

N25:      LJMP     DEN                          ;“=”

N4:             LJMP     E3

FIRST:       MOV    YJ,R4               ;输入值送结果

                   MOV      R3,#0                ;按键次数清零

                   LJMP     DISP1                ;结果处理

JIA:       MOV              A,YJ                             ;上次结果送累加器

                   ADD      A,R4                             ;上次结果加输入值

                   JB         CY,N4                                    ;溢出

                   MOV      YJ,A                             ;存本次结果

                   MOV      R3,#0                                     ;按键次数清零

                   LJMP     DISP1

JIAN:         MOV    A,YJ

                   SUBB     A,R4                             ;上次结果减输入值

                   JB         CY,N4                                    ;负数溢出

                   MOV      YJ,A

                   MOV      R3,#0

                   LJMP     DISP1

CHENG:    MOV      A,YJ

                   MOV      B,A

                   MOV      A,R4

                   MUL      AB                                         ;上次结果乘输入值

                   JB         OV,N4                                    ;溢出

                   MOV      YJ,A                                     

                   LJMP     DISP1

CHU:         MOV      A,R4

                   MOV      B,A

                   MOV      A,YJ

                   DIV        AB                                         ;上次结果除输入值

                   MOV      YJ,A

                   MOV      R3,#0

                   LJMP     DISP1

DEN:         MOV      R3,#0

                   LJMP     DISP1

DISP1:       MOV                  B,#10

                   MOV                  A,YJ                             ;结果送累加器

                   DIV            AB                                         ;结果除10

                   MOV                  YJ1,A                                    ;暂存

                   MOV                  A,B                                        ;取个位数

                   MOV                  34H,A                                    ;个位数送显示缓存

                   MOV                  A,YJ1

                   JZ              DISP11                                  ;结果是否为一位数

                   MOV                  B,#10

                   MOV                  A,YJ1

                   DIV            AB

                   MOV                  YJ1,A

                   MOV                  A,B

                   MOV                  33H,A                                    ;十位送显示缓存

                   MOV                  A,YJ1

                   JZ              DISP11                                  ;结果是否为二位数

                   MOV                  32H,A                                    ;百位数送显示缓存

DISP11:     LJMP        MLOOP

DISP:         MOV                  R0,#DBUF                            ;显示子程序

                   MOV                  R1,#TEMP+4

                   MOV                  R2,#5

DP10:        MOV                  DPTR,#SEGTAB

                   MOV                  A,@R0

                   MOVC      A,@A+DPTR

                   MOV                  @R1,A

                   INC            R0

                   DEC           R1

                   DJNZ        R2,DP10

                   MOV                  R0,#TEMP

                   MOV                  R1,#5

DP12:        MOV                  R2,#8

               &n

网站导航

售前咨询:023-58556299

技术咨询:023-58722688

返回顶部