8051 program to convert Ascii value to Hexadecimal value....

Hi guys...In this article let's learn conversion.The ascii to Hex conversion logic is very simple.The hexadecimal numbers from 31H-39H are 0-9 numbers in ascii and
41H to 46H are A to F numbers. So we should first compare the number with 40H if it is lesser subtract with 30H and it is greater subtract with 37H.
Asciivalue            Hexadecimal value
31H            01H
32H            02H
33H            03H
34H            04H
35H            05H
36H            06H
37H            07H
38H            08H
39H            09H
41H            0AH
42H            0BH
43H            0CH
44H            0DH
45H            0EH
46H            0FH


 1)Initialize R0 with number which is required to find equivalent Hexadecimal number code.
 2)Compare it with 40H and jump to label1 if it is equal.
3)Compare the carry bit to find which is greater and lesser.
4)If the carry bit is not set(it implies it is greater)jump to label2.
 5)If it is lesser subtract the number with 30H.
6)If it is greater subtract the number with 37H.

Code goes here:

ORG 0000h
ORG 40h
MAIN:  MOV R0,#41H         ; move the numbers to be converted
       MOV A,R0
       CJNE A,#40H,LABEL1  ; compare the number with 40H
LABEL1:JNC LABEL2          ;If the number is greater than 40H jump to label2
       CLR c
       SUBB A,#30H         ; If the number is lesser than 40H subtract with with 30H
       SJMP STOP
        SUBB A,#37H        ;If the number is greater than 40H subtract with 37H 

No comments:

Powered by Blogger.