CrazyEngineers
  • pranitamm
    pranitamm

    MemberNov 2, 2008

    hex to bcd?

    in my project i require bcd at port 0 and 3 of microcontroller 89s52 since there are three 7447 connected
    (i.e p0.0-p0.3 to 7447(1) lsb,
    p0.4-p0.7 to7447(1),
    p3.0-p3.3 to7447(3)msb)
    which drive three 7 segment displays
    now suppose i want to display (240)d then in the accumulator there is its hex equivalent i.e (F0)h which is proper but now when i convert it to bcd using the instruction DA A i get 50 in the accumulator with a carry.now this is the problem instead of having 240 at the o/p there comes 150
    please can anybody help me to splve this problem??????
    please its very urgent!!!!!!
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • binu_ji

    MemberNov 2, 2008

    Use this subroutine to convert HEX to BCD
    Hex2BCD:
    MOV R1,#00H        ;  MSByte
            MOV R2,#0F0H       ;  LSByte
     
            MOV R3,#00D
            MOV R4,#00D
            MOV R5,#00D
            MOV R6,#00D
            MOV R7,#00D
            CALL H2B
            RET
            
    H2B:        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:   RET 
    Just call this routine by loading the value into the R2 and you will get the result like this
    R3=0
    R4=4
    R5=2
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register