Converting 2-Digit Hex to Decimal ASCII Code for Display

Some times it is desired to convert a 2-bit hex code into decimal ASCII code.

For example, MCU processes hex number. If the user wishes to display the value code of a counter in a MCU, a conversion to decimal ASCII code will be required. Other wise, when the counter goes up to "11", the actual value in the MCU would be 0x0A, and the corresponding ASCII characters that will be displayed are "0 :"

The following code converts a hex value to decimal values, and put it in 3 different registers as decimal values. From there the user can display them as ASCII characters:
; *************************************************************
; HexToDec: Converts an Hexa in W in 3 decimals.
; Args: W with Hexa
; Returns:
; Ones
; W Tens from 0 to 25
; Tens
; Hund
; *************************************************************
HexToDec:
MOVWF
Ones
CLRF Tmp2

; Get the Units
HexToDec1
:
MOVLW
.10
SUBWF
Ones,W
BTFSS _carry
GOTO HexToDec2
MOVWF
Ones
INCF Tmp2
,F
GOTO HexToDec1

HexToDec2
:
MOVF Tmp2
,W
MOVWF
Tens
CLRF
Hund
HexToDec3
:
MOVLW
.10
SUBWF
Tens,W
BTFSS _carry
GOTO HexToDec4
MOVWF
Tens
INCF
Hund,F
GOTO HexToDec3

HexToDec4
:
MOVF Tmp2
,W

RETURN


For more conversions, visit:

http://www.piclist.com/techref/microchip/math/radix/index.htm

Views: 153

Comment

You need to be a member of Personal Mechatronics Lab to add comments!

Join Personal Mechatronics Lab

© 2024   Created by PML.   Powered by

Badges  |  Report an Issue  |  Terms of Service