基于单片机双字节简易计算器程序设计.docx
- 文档编号:3423447
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:23
- 大小:19.25KB
基于单片机双字节简易计算器程序设计.docx
《基于单片机双字节简易计算器程序设计.docx》由会员分享,可在线阅读,更多相关《基于单片机双字节简易计算器程序设计.docx(23页珍藏版)》请在冰豆网上搜索。
基于单片机双字节简易计算器程序设计
;=================Const===============================
SysStackEqu07h;08H...1FH
DarkEqu10H
;-----------------Hardware----------------------------
DisSegPortEqu0000H;0:
Lightup
DisCSPortEqu2000H;1:
Select
KeyOutPortEquDisCSPort;KeyOutputPort
;KeyInout:
p1.0,P.1..P1.3;0:
Pressed
;------------------ByteUnits-------------------------
KeyGetFBit20H;1:
GetANewKey
KeyPressBit21H;1:
KeyReleased
SumPressBit22H;1+
SumPress1Bit23H
EqualPressBit24H;1=
DivPressBit25H;1/
DivPress1Bit26H
MulPressBit27H;1*
MulPress1Bit28H
SubbPressBit29H;1-
SubbPress1Bit31H
NegPressBit32H
NegPress1Bit33H;+/-
NegPress2Bit34H
DataPressBit35H;数字键
FuncPressBit36H;运算功能键
SignBit37H
KeyCodeEqu60H
DisBufEqu70H;..75H
DeciPosEqu76H;Bit=1:
ShowDeciPoint
SumVaEqu77H;pressfuncKEY,MemorytheDATAONdis
DivVaEqu78H;thesameUP
MulVaEqu79H;thesameUP
SubbVaEqu85H;thesameUP
;=======================================================
Org0000H
AJmpMain
;=======================================================
Display:
MovR1,#00000001B;R1=DisCS
MovR0,#DisBuf;DisBufPointer
DisLoop:
MovB,#0FFH
MovA,DeciPos;小数点?
AnlA,R1
JzDisNext;0没小数点
ClrB.7;1小数点
DisNext:
MovDptr,#DisCSPort;DoDisplayCS
MovA,R1
Movx@Dptr,A
MovA,@R0;GetDisChar
MovDptr,#DisTab
MovcA,@A+Dptr
AnlA,B
MovDptr,#DisSegPort;UseTable
Movx@Dptr,A;SendtoDisSegPort
ACallDelay
IncR0
MovA,R1
RLA
MovR1,A
CjneR0,#DisBuf+6,DisLoop
Ret
;-------------------------------------------------------
;Char:
'0','1','2','3','4','5','6','7','8','9'
;Code:
00H,01H,02H,03H,04H,05H,06H,07H,08H,09H
DisTab:
DB0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
;Char:
'A','B','C','D','E','F','10H'
;Code:
+,-,*,/,=,ON/C,Dark
DB088H,0BFH,0c6H,0a1H,86H,8eH,0FFH
;--------------------------------------------------------
Delay:
MovR2,#04h
Delay1:
MovR3,#0
DjnzR3,$
DjnzR2,Delay1
Ret
;-------------------------------------------------------
InitDis:
MovDisBuf+0,#0
MovDisBuf+1,#Dark
MovDisBuf+2,#Dark
MovDisBuf+3,#Dark
MovDisBuf+4,#Dark
MovDisBuf+5,#Dark
MovDeciPos,#00000000B
Ret
;------------------------------------------------------
NegDis:
MovDisBuf+5,#0BH
Ret
;-------------------------------------------------------
ScanKey:
MovDptr,#KeyOutPort
MovA,#0
Movx@Dptr,A
MovA,P1
CplA;取反处理,以便用A进行是否全0判断.指令只可判断全0不可判断全1
ANlA,#0FH;屏蔽掉无关位
Ret
;-----------------------------------------------------------------------
ReadKey:
ACallScanKey
JnzReadKey1;A为0时无按键,只要有一位为1即有按键
ClrKeyPress
ReadKeyQ:
Ret
ReadKey1:
JbKeyPress,ReadKeyQ;键压着可以准确定位是哪个键压着,故返回
MovR2,#11111110B;
MovR3,#0;准备扫描第一列
ReadKeyLp:
MovDptr,#KeyOutPort
MovA,R2;列扫描值,选则列输出,检测
Movx@Dptr,A
MovA,#0
JnbP1.0,ReadKeyOk
MovA,#6
JnbP1.1,ReadKeyOk
MovA,#12
JnbP1.2,ReadKeyOk
MovA,#18
JnbP1.3,ReadKeyOk
IncR3
MovA,R2
RLA
MovR2,A
AJmpReadKeyLp
ReadKeyOk:
AddA,R3;加上列扫描值
SetbKeyGetF
SetbKeyPress
MovDptr,#KeyChgTab
MovcA,@A+Dptr
MovKeyCode,A
Ret
;----------------------------------------------------------------
;KeyCode00H,01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH,0BH,0CH,0DH,0EH,0FH,10H,11H,12H,13H,14H,15H,16H,17H
;OldKeySpc,0,Dot,Equ,+,M+,%,1,2,3,?
M-,+/-,4,5,6,-,MRC,C,7,8,9,X,/
KeyChgTab:
DB10,0,11,12,13,14,15,1,2,3,16,17,18,4,5,6,19,20,21,7,8,9,22,23
;-------------------------------------------------------
InitKey:
ClrKeyGetF
Ret
;--------------------------------------------------------
InitFunck:
ClrSumPress
ClrEqualPress
ClrDivPress
ClrMulPress
ClrSubbPress
InitNeg:
ClrNegPress1
ClrNegPress2
Ret
;--------------------------------------------------------
;=================================================================
;执行运算
Final:
JbSumPress1,AddA;是否按下过加法键加法
JbDivPress1,DivD
JbMulPress1,MulM
JbSubbPress1,SubbS
AjmpMainLoop
;---------------------------------------------------------
AddA:
ClrSumPress1;执行加法运算
ACallFromLED
JnbNegPress,AddC1
ClrNegPress
MovA,R6
OrlA,#10000000B
MovR6,A
AddC1:
ACallInitDis
ACallAddF;调用加法子程序
ACallToLED
Ret
;----------------------------------------------------------
DivD:
ClrDivPress1;执行除法运算
ACallFromLED
JnbNegPress,DivC1
ClrNegPress
MovA,R6
OrlA,#10000000B
MovR6,A
DivC1:
ACallInitDis
ACallDivF
ACallToLED
Ret
;--------------------------------------------------------------
;R6R7*R4R5=R2R3R6R7
MulM:
ClrMulPress1
ACallFromLED
JnbNegPress,MulC1
ClrNegPress
MovA,R6
OrlA,#10000000B
MovR6,A
MulC1:
ACallInitDis
ACallMulF;调用乖法子程序
AcallToLED
Ret
;-----------------------------------------------------------------
;R4R5-R6R7=R6R7
SubbS:
ClrSubbPress1
ACallFromLED
JnbNegPress,SubbC1
ClrNegPress
MovA,R6
OrlA,#10000000B
MovR6,A
SubbC1:
ACallInitDiS
ACallSubbF
ACallToLED
Ret
;------------------------
;R4R5*R6R7=R6R7
MulF:
MovA,R4;判断两数符号同或异
XrlA,R6
MovC,ACC.7
MovSign,C
MovA,R4
ClrACC.7
MovR4,A
MovA,R6
ClrACC.7
MovR6,A
MovA,R7
MovB,R5
MULAB
XCHA,R7
MovR2,B
MovB,R4
MULAB
AddA,R2
MovR2,A
ClrA
AddcA,B
MovR3,A
MovA,R6
MovB,R5
MULAB
AddA,R2
MovR2,A
MovA,B
AddcA,R3
MovR3,A
ClrA
AddcA,#00H
XCHA,R6
MovB,R4
MULAB
AddA,R3
MovR3,A
MovA,B
AddcA,R6
XCHA,R2
MovR6,A
ClrOV
MovA,R6
MovC,Sign
MovACC.7,C
MovR6,A
BackMul:
Ret
;------------------------
;R4R5/R6R7=R6R7先R0R1余R2R3
DivF:
MOVA,R4
XRLA,R6
MOVC,ACC.7
MOVSign,C
MOVA,R4
CLRACC.7
MOVR4,A
MOVA,R6
CLRACC.7
MOVR6,A
MovR0,#0
MovR1,#0;商赋初值零
Div2Byte_1:
MovA,R5
Clrc
SubbA,R7;低字节相减
MovR5,A
MovA,R4
SubbA,R6;高位相减
MovR4,A
JcDiv2Byte_3;如果高字节都需要借位则退出循环
Clrc
MovA,R1;R1自增
AddCA,#1
MovR1,A
JncDiv2Byte_2
IncR0;商加一
Div2Byte_2:
SjmpDiv2Byte_1
Div2Byte_3:
MovA,R0
MovR6,A
MovA,R1
MovR7,A
MOVA,R6
JBACC.7,IDIVE
JBOV,IDIVE
MOVC,Sign
MOVACC.7,C
MOVR6,A
RET
IDIVE:
SETBOV
BackDiv:
Ret
;-----------------------------------------------------------------------------
AddF:
MOVA,R4
MOVC,ACC.7
MOVSign,C
XRLA,R6;判断R3R6是否同号
MOVC,ACC.7
MOVA,R4
ClrACC.7
MOVR4,A
MOVA,R6
CLRACC.7
MOVR6,A
JCDAB2
ACallAdd1
MOVA,R6
JBACC.7,DABE
DAB1:
MOVC,Sign
MOvACC.7,C
MOVR6,A
CLROV
RET
DABE:
SETBOV
RET
DAB2:
LCALLSubb1
MOVA,R6
JNBACC.7,DAB1
ACALLCmpt
CPLSign
SJMPDAB1
BackA:
Ret
;-----------------------------------------------------------------------------
SubbF:
MOVA,R6
CPLACC.7
MOVR6,A
MOVA,R4
MOVC,ACC.7
MOVSign,C
XRLA,R6;判断R3R6是否同号
MOVC,ACC.7
MOVA,R4
ClrACC.7
MOVR4,A
MOVA,R6
CLRACC.7
MOVR6,A
JCDAB2
ACallAdd1
MOVA,R6
JBACC.7,SDABE
SDAB1:
MOVC,Sign
MOvACC.7,C
MOVR6,A
CLROV
RET
SDABE:
SETBOV
RET
SDAB2:
ACallSubb1
MOVA,R6
JNBACC.7,SDAB1
ACALLCMPT
CPLSign
SJMPSDAB1
;------------------------
Cmpt:
MOVA,R7
CPLA
ADDA,#01H
MOVR7,A
MOVA,R6
CPLA
ADDCA,#00H
MOVR4,6
Ret
;-----------------------------
;R4R5+R6R7=R6R7
Add1:
ClrC
MovA,R5
AddA,R7
MovR7,A
MovA,R4
AddcA,R6
MovR6,A
Ret
;------------------------
;R4R5-R6R7=R6R7
Subb1:
MovA,R5
ClrC
SubbA,R7
MovR7,A
MovA,R4
SubbA,R6
MovR6,A
Ret
;------------------------
;=======================================================================
;从显示屏读出数
FromLED:
Movr0,#DisBuf+4
ReadLED1:
Cjne@r0,#Dark,ReadLED2
Decr0
cjner0,#DisBuf+0,READLED1
ReadLED2:
MovR6,#0
MovA,@r0
MovR7,A
Cjner0,#DisBuf+0,ReadLed3
SjmpBackFLED
ReadLED3:
ACallR6R7Mul10
Decr0
MovA,@r0
AcallAddDis
Cjner0,#DisBuf+0,ReadLED3
BackFLED:
Ret
;====================================================================
AddDis:
ClrC
AddcA,R7
MovR7,A
JncBackAD
INCR6
BackAD:
Ret
;-----------------------------------------------------------------------
;R6R7:
=R6R7*10
R6R7Mul10:
MovA,R7
MovB,#10
MulAB
MovR7,A;R7*10高
MovA,#10
XchA,B;B:
=10,A:
=BR7低
XchA,R6;A:
=R6
MulAB
AddA,R6
MovR6,A
Ret
;-------------------------------------------------------
;将数送显示
;============================DisplayNumber===============================
;changeR67into@r0.DisBuf(5)
;By:
Linjuan,improvedby:
WuChuiHong
;Improvements:
;1.MuchFaster,2.FewerCodes,3.r6,r7,r5,r6,r7,DptrNotused
;EveryExecTime=275usfornum=1to65535,Teston25/9/1998
;Change:
a,b,r0,R0,r2ifmorethan1byte
;---------------------------------------------------------------------------
;IfR6.7=1(Negtive!
)thenR6R7:
=0-R6R7,DisBuf+5:
'-'
ToLED:
MovA,R6
JnbACC.7,Correct
ClrACC.7
MovR6,A
ACallNegDis
Correct:
MovR0,#DisBuf
LCallFilDisBuf
Word2DR01:
Movr2,#4;1wordneedsonly4loops
W2DLoop:
Mova,r6
jzW2DByte;needonlydolowbyte
Movb,#10;elsedividedby10
divab
Movr6,a
Mova,r7
anla,#0f0h
orla,b
swapa
Movb,#10
divab
swapa
Xcha,r7
anla,#0fh
xcha,b
swapa
orla,b
Movb,#10
divab
Mov@R0,b
IncR0
Orla,r7
Movr7,a
djnzr2,W2DLoop
jnzW2DOut0
Ret
;------------------------------
W2DByteClr:
Movr2,#2
ACallClrDisLp
SJmpW2DByteA
;------------------------------
W2DByte:
Mova,r7
W2DByteA:
Movb,#10
divab
Mov@R0,b
jzW2DOut
Movb,#10
divab
IncR0
Mov@R0,b
jzW2DOut
IncR0
W2DOut0:
Mov@R0,a
W2DOut:
Ret
;---------------------------------------------------------------------------
ClrDisBuf0:
Movr0,#D
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 单片机 字节 简易 计算器 程序设计