logo

Select Sidearea

Populate the sidearea with useful widgets. Itโ€™s simple to add images, categories, latest post, social media icon links, tag clouds, and more.
[email protected]
+1234567890
 

16 Bit Hex to BCD Conversion for 8051 Microcontroller

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This routine is for 16 bit Hex to BCD conversion;;;;;;;;;;;;;;;;;;;;;
;Accepts a 16 bit binary number in R1,R2 and returns 5 digit BCD in
;R7,R6,R5,R4,R3(upto 64K )
           
Hex2BCD:
           MOV R1,#0fFH ; MSByte
           MOV R2,#0FFH ; LSByte
           
           MOV R3,#00D
           MOV R4,#00D
           MOV R5,#00D
           MOV R6,#00D
           MOV R7,#00D
           
           MOV B,#10D
           MOV A,R2
           DIV AB
           MOV R3,B ; 
           MOV B,#10 ; R7,R6,R5,R4,R3
           DIV AB
           MOV R4,B
           MOV R5,A
           CJNE R1,#0H,HIGH_BYTE ; CHECK FOR HIGH BYTE
           SJMP ENDD
           
HIGH_BYTE:
           MOV A,#6
           ADD A,R3
           MOV B,#10
           DIV AB
           MOV R3,B
           ADD A,#5
           ADD A,R4
           MOV B,#10
           DIV AB
           MOV R4,B
           ADD A,#2
           ADD A,R5
           MOV B,#10
           DIV AB
           MOV R5,B
           CJNE R6,#00D,ADD_IT
           SJMP CONTINUE
ADD_IT:
           ADD A,R6
CONTINUE:
           MOV R6,A
           DJNZ R1,HIGH_BYTE
           MOV B, #10D
           MOV A,R6
           DIV AB
           MOV R6,B
           MOV R7,A
ENDD: SJMP $