MCS51 microcontroller digital system conversion program

Publisher:本人在Latest update time:2023-08-23 Source: elecfansKeywords:MCS51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

;Convert BCD decimal to binary decimal (2 digits)
; Entry: R0 (low first address), R7
; Occupied resources: ACC, B, R5
; Stack requirement: 2 bytes
; Exits: R3, R4


PDTB:CLR A
          MOV R3,A
          MOV R4,A
PDB1:MOV A,R3
          MOV B,#9AH
          MUL AB
          MOV R5,B
          XCH A,R4
          MOV B,#19H
          MUL AB
          ADD A,R4
          MOV A,R5
          ADDC A ,B
          MOV R5,A
          MOV A,@R0
          MOV B,#9AH
          MUL AB
          ADD A,R5
          MOV R4,A
          CLR A
          ADDC A,B
          XCH A,R3
          MOV B,#19H
          MUL AB
          ADD A,R4
          MOV R4, A
          MOV A,B
          ADDC A,R3
          MOV R3,A
          MOV A,@R0
          MOV B,#19H
          MUL AB
          ADD A,R3
          MOV R3,A
          DEC R0
          DJNZ R7,PDB1
          RET

;Convert BCD decimal to binary decimal (N bits)
; Entry: R1 (low first address), M, N
; Occupied resources: ACC, B, R2, R3, R7
; Stack requirement: 2 bytes
; Exit: R0

PDTBMN :MOV A,R0
          MOV R2,A
          MOV A,R1
          MOV R3,A
          MOV B,N
          CLR A
PDBMN1 :MOV @R0,A
          INC R0
          DJNZ B,PDBMN1
          MOV A,N
          SWAP A
          RR A
          MOV R7,A
PDBMN2 :MOV A,R2
          MOV R0,A
          MOV A,R3
          MOV R1,A
          MOV B,M
          CLR C
PDBMN3 :MOV A,@R1
          ADDC A,@R1
          DA A
          JNB ACC.4,PDBMN4
          SETB C
          CLR ACC.4
PDBMN4 :MOV @R1,A
          INC R1
          DJNZ B,PDBMN3
          MOV B,N
PDBMN5 :MOV A,@R0
          RLC A
          MOV @R0,A
          INC R0
          DJNZ B,PDBMN5
          DJNZ R7,PDBMN2
          MOV A,R2
          MOV R0,A
          RET

;Convert BCD integer to binary integer (1 bit)
; Entry: R0 (high address), R7
; Occupied resources: ACC, B
; Stack requirement: 2 bytes
; Exit: R4

IDTB1 :CLR A
          MOV R4,A
IDB11 :MOV A,R4
          MOV B,#0AH
          MUL AB
          ADD A,@R0
          INC R0
          MOV R4,A
          DJNZ R7,IDB11
          RET

;Convert BCD integer to binary integer (2 bits)
; Entry: R0 (high address), R7
; Occupied resources: ACC, B
; Stack requirement: 2 bytes
; Exit: R3, R4

IDTB2:CLR A
          MOV R3,A
          MOV R4,A
IDB21:MOV A,R4
          MOV B,#0AH
          MUL AB
          MOV R4,A
          MOV A,B
          XCH A,R3
          MOV B,#0AH
          MUL AB
          ADD A,R3
          MOV R3 ,A
          MOV A,R4
          ADD A,@R0
          INC R0
          MOV R4,A
          CLR A
          ADDC A,R3
          MOV R3,A
          DJNZ R7,IDB21
          RET

;Convert BCD integer to binary integer (3 bits)
; Entry: R0 (high address), R7
; Occupied resources: ACC, B
; Stack requirement: 2 bytes
; Exit: R2, R3, R4

IDTB3:CLR A
          MOV R2,A
          MOV R3,A
          MOV R4,A
IDB31:MOV A,R4
          MOV B,#0AH
          MUL AB
          MOV R4,A
          MOV A,B
          XCH A,R3
          MOV B,#0AH
          MUL AB
          ADD A ,R3
          MOV R3,A
          CLR A
          ADDC A,B
          XCH A,R2
          MOV B,#0AH
          MUL AB
          ADD A,R2
          MOV R2,A
          MOV A,R4
          ADD A,@R0
          INC R0
          MOV R4,A
          CLR A
          ADDC A ,R3
          MOV R3,A
          CLR A
          ADDC A,R2
          MOV R2,A
          DJNZ R7,IDB31
          RET

;Convert BCD integer to binary integer (N bits)
; Entry: R1 (high address), M, N
; Occupied resources: ACC, B, R2, R7, NCNT, F0
; Stack requirement: 2 bytes
; Exit: R0

IDTBMN :MOV A,R0
          MOV R2,A
          MOV B,N
          CLR A
IDBMN1 :MOV @R0,A
          INC R0
          DJNZ B,IDBMN1
          MOV A,R2
          MOV R0,A
          MOV A,M
          MOV NCNT,A
IDBMN2 :MOV R7, N
          CLR A
          CLR F0
IDBMN3 :XCH A,@R0
          MOV B,#0AH
          MUL AB
          MOV C,F0
          ADDC A,@R0
          MOV F0,C
          MOV @R0,A
          INC R0
          MOV A,B
          DJNZ R7,IDBMN3
          MOV A, R2
          MOV R0,A
          MOV A,@R1
          INC R1
          ADD A,@R0
          MOV @R0,A
          DJNZ NCNT,IDBMN2
          RET

;Convert binary decimal (2 digits) to decimal decimal (separated BCD code)
; Entry: R3, R4, R7
; Occupied resources: ACC, B
; Stack requirement: 3 bytes
; Exit: R0

PBTD:MOV A,R7
          PUSH A
PBD1:MOV A,R4
          MOV B,#0AH
          MUL AB
          MOV R4,A
          MOV A,B
          XCH A,R3
          MOV B,#0AH
          MUL AB
          ADD A,R3
          MOV R3,A
          CLR A
          ADDC A,B
          MOV @R0,A
          INC R0
          DJNZ R7,PBD1
          POP A
          MOV R7,A
          MOV A,R0
          CLR C
          SUBB A,R7
          MOV R0,A
          RET

;Convert binary decimal (M digits) to decimal decimal (separated BCD code)
; Entry: R1, M, N
; Occupied resources: ACC, B, R2, R3, R7, NCNT
; Stack requirement: 2 bytes
; Exit: R0

PBTDMN :MOV A,R0
          MOV R2,A
          MOV A,R1
          MOV R3,A
          MOV A,N
          MOV NCNT,A
PBDMN1 :MOV R7,M
          CLR A
          CLR F0
PBDMN2 :XCH A,@R1
          MOV B,#0AH
          MUL AB
          MOV C,F0
          ADDC A,@R1
          MOV F0,C
          MOV @R1,A
          INC R1
          MOV A,B
          DJNZ R7,PBDMN2
          ADDC A,#00H
          MOV @R0,A
          INC R0
          MOV A,R3
          MOV R1,A
          DJNZ NCNT ,PBDMN1
          MOV A,R2
          MOV R0,A
          RET

;Convert binary integer (2 digits) to decimal integer (separated BCD code)
; Entry: R3, R4
; Occupied resources: ACC, R2, NDIV31
; Stack requirement: 5 bytes
; Exit: R0, NCNT

IBTD21 :MOV NCNT,#00H
          MOV R2,#00H
IBD211 :MOV R7,#0AH
          LCALL NDIV31
          MOV A,R7
          MOV @R0,A
          INC R0
          INC NCNT
          MOV A,R3
          ORL A,R4
          JNZ IBD211
          MOV A,R0
          CLR C
          SUBB A,NCNT
          MOV R0,A
          RET

;Convert binary integer (2 digits) to decimal integer (combined BCD code)
; Entry: R3, R4
; Occupied resources: ACC, B, R7
; Stack requirement: 3 bytes
; Exit: R0

IBTD22 :MOV A,R0
          PUSH A
          MOV R7,#03H
          CLR A
IBD221 :MOV @R0,A
          INC R0
          DJNZ R7,IBD221
          POP A
          MOV R0,A
          MOV R7,#10H
IBD222 :PUSH A
          CLR C
          MOV A,R4
          RLC A
          MOV R4,A
          MOV A,R3
          RLC A
          MOV R3,A
          MOV B,#03H
IBD223 :MOV A,@R0
          ADDC A,@R0
          DA A
          MOV @R0,A
          INC R0
          DJNZ B,IBD223
          POP A
          MOV R0, A
          DJNZ R7,IBD222
          RET

;Convert binary integer (3 digits) to decimal integer (separated BCD code)
; Entry: R2, R3, R4
; Occupied resources: ACC, R2, NDIV31
; Stack requirement: 5 bytes
; Exit: R0, NCNT

IBTD31 :CLR A
          MOV NCNT,A
IBD311 :MOV R7,#0AH
          LCALL NDIV31
          MOV A,R7
          MOV @R0,A
          INC R0
          INC NCNT
          MOV A,R2
          ORL A,R3
          ORL A,R4
          JNZ IBD311
          MOV A,R0
          CLR C
          SUBB A,NCNT
          MOV R0,A
          RET

;Convert binary integer (3 digits) to decimal integer (combined BCD code)
; Entry: R2, R3, R4
; Occupied resources: ACC, B, R7
; Stack requirement: 3 bytes
; Exit: R0

IBTD32 :MOV A,R0
          PUSH A
          MOV R7,#04H
          CLR A
IBD321 :MOV @R0,A
          INC R0
          DJNZ R7,IBD321
          POP A
          MOV R0,A
          MOV R7,#18H
IBD322 :PUSH A
          CLR C
          MOV A,R4
          RLC A
          MOV R4,A
          MOV A,R3
          RLC A
          MOV R3,A
          MOV A,R2
          RLC A
          MOV R2,A
          MOV B,#04H
IBD323 :MOV A,@R0
          ADDC A,@R0
          DA A
          MOV @R0,A
          INC R0
          DJNZ B,IBD323
          POP A
          MOV R0,A
          DJNZ R7,IBD322
          RET

;Convert binary integer (M bits) to decimal integer (combined BCD code)
; Entry: R1, M, N
; Occupied resources: ACC, B, R2, R3, R7
; Stack requirement: 2 bytes
; Exit: R0

IBTMN:MOV A,R0
          MOV R2,A
          MOV A,R1
          MOV R3,A
          MOV B,N
          CLR A
IBDMN1:MOV @R0,A
          INC R0
          DJNZ B,IBDMN1
          MOV A,M
          SWAP A
          RR A
          CLR C
          MOV R7, A
IBDMN2 :MOV A,R2
          MOV R0,A
          MOV A,R3
          MOV R1,A
          MOV B,M
IBDMN3 :MOV A,@R1
          RLC A
          MOV @R1,A
          INC R1
          DJNZ B,IBDMN3
          MOV B,N
IBDMN4 :MOV A,@R0
          ADDC A,@R0
          DA A
          JNB ACC.4,IBDMN5
          SETB C
          CLR ACC.4
IBDMN5 :MOV @R0,A
          INC R0
          DJNZ B,IBDMN4
          DJNZ R7,IBDMN2
          MOV A,R2
          MOV R0,A
          RET


Keywords:MCS51 Reference address:MCS51 microcontroller digital system conversion program

Previous article:MCS51 microcontroller search and search program
Next article:Analysis of the structure and principle of 51 microcontroller memory

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号