Motorolla MC68HC11 Microcontroller-to convert a Hex digit.

Discussion in 'Embedded Systems and Microcontrollers' started by NiCeBoY, Oct 28, 2008.

  1. NiCeBoY

    Thread Starter Active Member

    Aug 20, 2008
    59
    0
    Hello,
    I am require to write a program module to convert a Hex digit passed to it in the lower nibble of Accumulator A to its ASCII character code. The upper nibble should be treated as "don't care".
    Return the ASCII code in Accumulator A and save it in a suitable memory location.
    All other CPU register values are to remain unchanged.


    Thanks in advance for your help...

    Regards...
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Use a lookup table with the hex value as the offset into the table.
     
  3. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    "Unchanged" is not the same as "unused." Push & pop any registers you need to use for indexing.

    OR, study the ASCII values for '0'..'9', 'A'..'F', especially how $9 and $A are related to '9' and 'A'.

    --Rich
     
  4. NiCeBoY

    Thread Starter Active Member

    Aug 20, 2008
    59
    0
    yeah i got the steps to proceed :

    First of all, clear the upper bits by doing an AND with immediate data 0x0F.
    Then compare the remaining number with 9. If it's less than or equal to 9, then do an OR with 0x30H and that is the ASCII code.

    If the number is greater than 9, then subtract 9 and then do an OR with 0x40H to get the ASCII code.


    now i am working on the codes...
     
  5. NiCeBoY

    Thread Starter Active Member

    Aug 20, 2008
    59
    0
    here is my code:

    The problem is that I must use the instruction "CMPA" to compare instead of doing the substraction..


    Can anyone help me out of this please?

    the instruction set can be found @:
    http://www.ele.uri.edu/Courses/ele205/6811-Instructions/index.html


    Thanks
     
  6. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Its many many years since I've had anything to do with the 68 series of processors so don't expect working code, but the obvious way (at least to me ) would be to compare your input to 9 after masking off the high nibble. If equal to or less than 9, add $30, if greater than 9, add $37.
     
  7. NiCeBoY

    Thread Starter Active Member

    Aug 20, 2008
    59
    0
    Hello,
    why we should add $30 and $37 please?

    because someone told me if when comparing the result is less than or equal to 9, then do an OR with $30 and that is the ASCII code.

    If the number is greater than 9, then subtract 9 and then do an OR with $40.


    Thats a bit confusing me....
    thanks..
     
  8. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Ascii 0 - 9 = $30 - $39 so the masked low nibble plus $30 will give the correct ascii value for numbers 9 or less.
    Ascii "A" = $41, Ascii "B" = $42 etc
    $0A + $37 = $41
    Ascii B = $42
    $0B + $37 =$42
    etc.

    Actually a simpler method of getting the same result would be to mask the high nibble and add $30 (this will give the right result for numbers between 0 and 9).

    Next test result of the addition.

    If the result of the addition is $39 or less then don't do anything.

    If result is higher than $39 it means that the number was more than 9 so add another $07 to the answer to bump it up into the Ascii A to F range. If
     
  9. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Yes, that will work, but you are working too hard. Remember, 'A' - '9' = 7.
    So clear the upper bits, and compare the remaining number with 9. If it's greater than 9, add 7. In all cases, add $30, or '0'. ('0' will make the code easier to understand, later.)

    --Rich
     
  10. NiCeBoY

    Thread Starter Active Member

    Aug 20, 2008
    59
    0
    Yeah thats allrite... thanks...


    Now i got another question...

    Can anyone help me out with this one, just give me the steps and i will proceed with the code writing......

    by the way what is the difference between this one and the first one...


    thanx..
     
Loading...