基于单片机双字节简易计算器程序设计.doc_第1页
基于单片机双字节简易计算器程序设计.doc_第2页
基于单片机双字节简易计算器程序设计.doc_第3页
基于单片机双字节简易计算器程序设计.doc_第4页
基于单片机双字节简易计算器程序设计.doc_第5页
免费预览已结束,剩余12页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

;= Const =SysStack Equ 07h ;08H.1FHDark Equ 10H;- Hardware -DisSegPort Equ 0000H ;0:Light upDisCSPort Equ 2000H ;1:SelectKeyOutPort Equ DisCSPort;Key Output Port;KeyInout:p1.0,P.1.P1.3 ;0:Pressed;- Byte Units -KeyGetF Bit 20H ;1:Get A New KeyKeyPress Bit 21H ;1:Key ReleasedSumPress Bit 22H ;1 +SumPress1 Bit 23HEqualPress Bit 24H ;1 =DivPress Bit 25H ;1 /DivPress1 Bit 26HMulPress Bit 27H ;1 *MulPress1 Bit 28HSubbPress Bit 29H ;1 -SubbPress1 Bit 31HNegPress Bit 32HNegPress1 Bit 33H ;+/-NegPress2 Bit 34HDataPress Bit 35H ;数字键FuncPress Bit 36H ;运算功能键Sign Bit 37HKeyCode Equ 60HDisBuf Equ 70H;.75HDeciPos Equ 76H ;Bit=1:Show DeciPointSumVa Equ 77H ;press func KEY,Memory the DATA ON disDivVa Equ 78H ;the same UPMulVa Equ 79H ;the same UPSubbVa Equ 85H ;the same UP;= Org 0000H AJmp Main;=Display: Mov R1,#00000001B ;R1=DisCS Mov R0,#DisBuf ;DisBuf PointerDisLoop: Mov B,#0FFH Mov A,DeciPos ;小数点? Anl A,R1 Jz DisNext ;0没小数点 Clr B.7 ;1小数点DisNext: Mov Dptr,#DisCSPort;Do Display CS Mov A,R1 Movx Dptr,A Mov A,R0 ;Get Dis Char Mov Dptr,#DisTab Movc A,A+Dptr Anl A,B Mov Dptr,#DisSegPort;Use Table Movx Dptr,A ;Send to DisSeg Port ACall Delay Inc R0 Mov A,R1 RL A Mov R1,A Cjne R0,#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, 09HDisTab: DB 0C0H,0F9H,0A4H,0B0H, 99H, 92H, 82H,0F8H, 80H, 90H; Char: A, B, C, D, E, F, 10H; Code: + , - , * , / , = , ON/C ,Dark DB 088H,0BFH,0c6H,0a1H, 86H, 8eH ,0FFH;-Delay: Mov R2,#04hDelay1: Mov R3,#0 Djnz R3,$ Djnz R2,Delay1 Ret;-InitDis: Mov DisBuf+0,#0 Mov DisBuf+1,#Dark Mov DisBuf+2,#Dark Mov DisBuf+3,#Dark Mov DisBuf+4,#Dark Mov DisBuf+5,#Dark Mov DeciPos,#00000000B Ret;-NegDis: Mov DisBuf+5,#0BH Ret;-ScanKey: Mov Dptr,#KeyOutPort Mov A,#0 Movx Dptr,A Mov A,P1 Cpl A ;取反处理,以便用A进行是否全0判断.指令只可判断全0不可判断全1 ANl A,#0FH ;屏蔽掉无关位 Ret;-ReadKey: ACall ScanKey Jnz ReadKey1 ;A为0时无按键,只要有一位为1即有按键 Clr KeyPressReadKeyQ: RetReadKey1: Jb KeyPress,ReadKeyQ ; 键压着可以准确定位是哪个键压着,故返回 Mov R2,#11111110B ; Mov R3,#0 ;准备扫描第一列ReadKeyLp: Mov Dptr,#KeyOutPort Mov A,R2 ;列扫描值,选则列输出,检测 Movx Dptr,A Mov A,#0 Jnb P1.0,ReadKeyOk Mov A,#6 Jnb P1.1,ReadKeyOk Mov A,#12 Jnb P1.2,ReadKeyOk Mov A,#18 Jnb P1.3,ReadKeyOk Inc R3 Mov A,R2 RL A Mov R2,A AJmp ReadKeyLpReadKeyOk: Add A,R3 ;加上列扫描值 Setb KeyGetF Setb KeyPress Mov Dptr,#KeyChgTab Movc A,A+Dptr Mov KeyCode,A Ret;-; KeyCode 00H,01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH,0BH,0CH,0DH,0EH,0FH,10H,11H,12H,13H,14H,15H,16H,17H; OldKey Spc, 0 ,Dot,Equ,+ ,M+ ,% , 1 , 2 ,3 ,? ,M- ,+/-, 4 , 5 ,6 , - ,MRC, C , 7 ,8 , 9 , X , /KeyChgTab: DB 10, 0 ,11 ,12 ,13 ,14 ,15 , 1 , 2 ,3 ,16 ,17, 18 , 4 , 5 ,6 , 19,20 ,21 , 7 ,8 , 9 , 22,23;-InitKey: Clr KeyGetF Ret;-InitFunck: Clr SumPress Clr EqualPress Clr DivPress Clr MulPress Clr SubbPressInitNeg: Clr NegPress1 Clr NegPress2 Ret;-;=;执行运算Final: Jb SumPress1,AddA ;是否按下过加法键加法 Jb DivPress1,DivD Jb MulPress1,MulM Jb SubbPress1,SubbS Ajmp MainLoop;-AddA: Clr SumPress1 ;执行加法运算 ACall FromLED Jnb NegPress,AddC1 Clr NegPress Mov A,R6 Orl A,#10000000B Mov R6,AAddC1: ACall InitDis ACall AddF ;调用加法子程序 ACall ToLED Ret;-DivD: Clr DivPress1 ;执行除法运算 ACall FromLED Jnb NegPress,DivC1 Clr NegPress Mov A,R6 Orl A,#10000000B Mov R6,ADivC1: ACall InitDis ACall DivF ACall ToLED Ret;-; R6R7*R4R5=R2R3R6R7MulM: Clr MulPress1 ACall FromLED Jnb NegPress,MulC1 Clr NegPress Mov A,R6 Orl A,#10000000B Mov R6,AMulC1: ACall InitDis ACall MulF ;调用乖法子程序 Acall ToLED Ret;-; R4R5-R6R7=R6R7SubbS: Clr SubbPress1 ACall FromLED Jnb NegPress,SubbC1 Clr NegPress Mov A,R6 Orl A,#10000000B Mov R6,ASubbC1: ACall InitDiS ACall SubbF ACall ToLED Ret;-;R4R5*R6R7=R6R7MulF: Mov A,R4 ;判断两数符号同或异 Xrl A,R6 Mov C,ACC.7 Mov Sign,C Mov A,R4 Clr ACC.7 Mov R4,A Mov A,R6 Clr ACC.7 Mov R6,A Mov A,R7 Mov B,R5 MUL AB XCH A,R7 Mov R2,B Mov B,R4 MUL AB Add A,R2 Mov R2,A Clr A Addc A,B Mov R3,A Mov A,R6 Mov B,R5 MUL AB Add A,R2 Mov R2,A Mov A,B Addc A,R3 Mov R3,A Clr A Addc A,#00H XCH A,R6 Mov B,R4 MUL AB Add A,R3 Mov R3,A Mov A,B Addc A,R6 XCH A,R2 Mov R6,A Clr OV Mov A,R6 Mov C,Sign Mov ACC.7,C Mov R6,ABackMul: Ret;-;R4R5/R6R7=R6R7 先R0R1 余R2R3DivF: MOV A,R4 XRL A,R6 MOV C,ACC.7 MOV Sign,C MOV A,R4 CLR ACC.7 MOV R4,A MOV A,R6 CLR ACC.7 MOV R6,A Mov R0,#0 Mov R1,#0 ;商赋初值零Div2Byte_1:Mov A,R5 Clr c Subb A,R7 ;低字节相减 Mov R5,A Mov A,R4 Subb A,R6 ;高位相减 Mov R4,A Jc Div2Byte_3 ;如果高字节都需要借位则退出循环 Clr c Mov A,R1 ;R1自增 AddC A,#1 Mov R1,A Jnc Div2Byte_2 Inc R0 ;商加一Div2Byte_2:Sjmp Div2Byte_1Div2Byte_3:Mov A,R0 Mov R6,A Mov A,R1 Mov R7,A MOV A,R6 JB ACC.7,IDIVE JB OV,IDIVE MOV C,Sign MOV ACC.7,C MOV R6,A RET IDIVE: SETB OVBackDiv: Ret;-AddF: MOV A,R4 MOV C,ACC.7 MOV Sign,C XRL A,R6 ; 判断R3R6是否同号 MOV C,ACC.7 MOV A,R4 Clr ACC.7 MOV R4,A MOV A,R6 CLR ACC.7 MOV R6,A JC DAB2 ACall Add1 MOV A,R6 JB ACC.7,DABE DAB1 : MOV C,Sign MOv ACC.7,C MOV R6,A CLR OV RET DABE : SETB OV RET DAB2 : LCALL Subb1 MOV A,R6 JNB ACC.7,DAB1 ACALL Cmpt CPL Sign SJMP DAB1BackA: Ret;-SubbF: MOV A,R6 CPL ACC.7 MOV R6,A MOV A,R4 MOV C,ACC.7 MOV Sign,C XRL A,R6 ; 判断R3R6是否同号 MOV C,ACC.7 MOV A,R4 Clr ACC.7 MOV R4,A MOV A,R6 CLR ACC.7 MOV R6,A JC DAB2 ACall Add1 MOV A,R6 JB ACC.7,SDABESDAB1: MOV C,Sign MOv ACC.7,C MOV R6,A CLR OV RET SDABE: SETB OV RET SDAB2: ACall Subb1 MOV A,R6 JNB ACC.7,SDAB1 ACALL CMPT CPL Sign SJMP SDAB1 ;-Cmpt : MOV A,R7 CPL A ADD A,#01H MOV R7,A MOV A,R6 CPL A ADDC A,#00H MOV R4,6 Ret;-;R4R5+R6R7=R6R7Add1: Clr C Mov A,R5 Add A,R7 Mov R7,A Mov A,R4 Addc A,R6 Mov R6,A Ret;-;R4R5-R6R7=R6R7Subb1: Mov A,R5 Clr C Subb A,R7 Mov R7,A Mov A,R4 Subb A,R6 Mov R6,A Ret;-;=;从显示屏读出数FromLED: Mov r0,#DisBuf+4ReadLED1: Cjne r0,#Dark,ReadLED2 Dec r0 cjne r0,#DisBuf+0,READLED1 ReadLED2: Mov R6,#0 Mov A,r0 Mov R7,A Cjne r0,#DisBuf+0,ReadLed3 Sjmp BackFLEDReadLED3: ACall R6R7Mul10 Dec r0 Mov A,r0 Acall AddDis Cjne r0,#DisBuf+0,ReadLED3BackFLED: Ret;=AddDis: Clr C Addc A,R7 Mov R7,A Jnc BackAD INC R6BackAD: Ret;-;R6R7:=R6R7*10R6R7Mul10: Mov A,R7 Mov B,#10 Mul AB Mov R7,A ;R7*10 高 Mov A,#10 Xch A,B ;B:=10, A:=B R7低 Xch A,R6 ;A:=R6 Mul AB Add A,R6 Mov R6,A Ret ;-;将数送显示;= Display Number =;change R67 into r0.DisBuf(5);By: Linjuan, improved by: Wu ChuiHong;Improvements:; 1. Much Faster, 2. Fewer Codes, 3. r6,r7,r5,r6,r7,Dptr Not used;Every Exec Time=275 us for num=1 to 65535, Test on 25/9/1998;Change: a,b,r0,R0,r2 if more than 1 byte;-;If R6.7=1(Negtive!) then R6R7:=0-R6R7, DisBuf+5:-ToLED: Mov A,R6 Jnb ACC.7,Correct Clr ACC.7 Mov R6,A ACall NegDisCorrect: Mov R0,#DisBuf LCall FilDisBufWord2DR01: Mov r2,#4 ;1 word needs only 4 loopsW2DLoop: Mov a,r6 jz W2DByte ; need only do low byte Mov b,#10 ; else divided by 10 div ab Mov r6,a Mov a,r7 anl a,#0f0h orl a,b swap a Mov b,#10 div ab swap a Xch a,r7 anl a,#0fh xch a,b swap a orl a,b Mov b,#10 div ab Mov R0,b Inc R0 Orl a,r7 Mov r7,a djnz r2,W2DLoop jnz W2DOut0 Ret;-W2DByteClr:Mov r2,#2 ACall ClrDisLp SJmp W2DByteA;-W2DByte: Mov a,r7W2DByteA: Mov b,#10 div ab Mov R0,b jz W2DOut Mov b,#10 div ab Inc R0 Mov R0,b jz W2DOut Inc R0W2DOut0: Mov R0,aW2DOut: Ret;-ClrDisBuf0:Mov r0,#DisBufClrDisBufA:Mov r2,#15ClrDisLp: Mov r0,# Inc r0 Djnz r2,ClrDisLp Ret;-FilDisBuf: Mov a,#0FilDisBufA:Mov r2,#5FilDisBufr:Mov b,r0ClrDisBufL:Mov r0,a Dec r0 Djnz r2,ClrDisBufL Mov r0,b Ret;-IndectNeg1: ;检测是否负数,是则置1标志位 Mov A,DisBuf+5 Cjne A,#Dark,SNeg1 Sjmp BackIN1SNeg1: Setb NegPress1BackIN1: Ret;-IndectNeg2: Mov A,DisBuf+5 Cjne A,#Dark,SNeg2 Sjmp BackIN2SNeg2: Setb NegPress2BackIN2: Ret;-IndectF: Jnb SumPress,KeyData1 ;未按过+,则继续输入数据 ACall InitDis ;按过键,则先清屏 Clr SumPress ;+复位 Sjmp BackIFKeyData1: Jnb MulPress,KeyData2 ;未按过*,则继续输入数据 ACall InitDis ;按过键,则先清屏 Clr MulPress Sjmp BackIFKeyData2: Jnb SubbPress,KeyData3 ;未按过*,则继续输入数据 ACall InitDis ;按过键,则先清屏 Clr SubbPress Sjmp BackIFKeyData3: Jnb DivPress,KeyData4 ;未按过/,则继续输入数据 ACall InitDis ;按过键,则先清屏 Clr DivPress Sjmp BackIFKeyData4: Jnb EqualPress,BackIF ;等号键位为0,则继续输入数据 Clr EqualPress ;为1这先复位为0并清屏 ACall InitDisBackIF: Ret;=Main: Mov Sp,#SysStack ACall InitDis ACall InitKey ACall InitFunckMainLoop: ACall Display ACall ReadKey Jbc KeyGetF,DoKey AJmp MainLoopDoKey: Mov A,KeyCode Clr C Subb A,#10 Jnc DoFuncKey ;字模大于等于10,跳到功能键 Setb DataPress Acall IndectF ;检测功能键 Mov A,DisBuf+4 ;If Top DisNum=Dark? Cjne A,#Dark,KeyNumQ Mov A,DisBuf+1 Cjne A,#Dark,RLDIS Mov A,DisBuf+0 Jz KeyNum1RLDIS: Mov A,DeciPos ;Left shift DeciP RL A Mov DeciPos,A Mov DisBuf+4,DisBuf+3 ;Key=Number Key Mov DisBuf+3,DisBuf+2 Mov DisBuf+2,DisBuf+1 Mov Dis

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论