How to Perform Packed BCD to ASCII Conversion?

conversion between ASCII and packed BCD

How to Perform Packed BCD to ASCII Conversion and Vice-Versa?

In this article i have discussed how to perform Packed BCD to ASCII conversion with examples. Many computer systems or devices have Real Time Clock (RTC). This clock is actually used for displaying time (hour, minutes, seconds) and date (year, month, day). Even though the power is ON or OFF, this information is processed but in BCD format. In order to display this information on devices screens, LCDs, or seven segments they need to be converted into the ASCII format.

First a packed BCD number is converted into unpacked BCD, then it is masked with 30H and finally it is converted into ASCII.

Binary Coded Decimal:

How to perform Packed BCD to ASCII conversion and vice versa?  Binary Coded Decimal is an encoding system in which decimal numbers from 0 to 9 are represented in binary form.

decimal numbers
decimal numbers

Unpacked & Packed BCD:

In bytes orientation, Binary Coded Decimal is divided into two systems which are following:

Unpacked BCD:

Unpacked BCD is an encoding system in which every digit has its own byte. This system is commonly used in the modern computers to deal the data accurately.

Example: Unpacked BCD of 12 = 00000001 & 00000010

Unpacked BCD of 27 = 00000010 & 00000111

Also check:

How to determine the overflow of signed and unsigned numbers?

Packed BCD:

Packed BCD is an encoding system in which two digits are encoded in a byte, one digit is the upper nibble and other digit is the lower nibble.

Example: Packed BCD of 12 = 00010010

Packed BCD of 27 = 00100111

ASCII Code:

In computers, all the information is represented in 0s and 1s, binary pattern must assign letters and other symbols or characters. Standard representation of this pattern was established in 1960 who names as ASCII which is stands for American Standard Code for Information Interchange. The great advantage of this system is that it is used by most of the computers therefore information is shared with other computers easily. ASCII code assign binary pattern from 0 to 9 in all alphabets of English both in uppercase and lower case. ASCII system needs seven bits to represent a code.

Example:  100 0010 = B

110 0011 = C

Zero is placed in the most significant bit position to make it eight-bit code. ASCII code from 0 to 9:

ASCII code
ASCII code
ASCII CODE
ASCII CODE

Conversion:

                     Many new microcontrollers have Real Time Clock (RTC), where the data is stored even the power is on or off. These microcontrollers provide data in BCD. Therefore, to display their data on screen we need to convert it into ASCII.

ASCII and BCD
ASCII and BCD

Packed BCD to ASCII Conversion:

Real Time Clock (RTC) provides the time (hours: minutes: seconds) and the date (year: month: day) continuously regardless of the power is on or off in the modern microcontrollers. However, data is provided in in packed BCD format. To represent this data on screen or printed by the printer, it must be in ASCII format. So, we need to convert packed BCD to unpacked BCD first then further convert into ASCII.

For Example:

Packed BCD Unpacked BCD ASCII
35H

0011 0101

03H & 05H

0000 0011 & 0000 0101

33H & 35H

0011 0011 & 0011 0101

49H

0100 1001

04H & 09H

0000 0100 & 0000 1001

34H & 39H

0011 0100 & 0011 1001

 

How to convert packed BCD numbers into ASCII on MPLAB using PIC Microcontroller?

MPLAB Code for packed BCD to ASCII Conversion 

Here is the code first

#INCLUDE<P18F4550.INC>
HB1 EQU 0X06
LB1 EQU 0X07
HB2 EQU 0X32
LB2 EQU 0X33
BCD1 EQU 0X76
BCD2 EQU 0X87

ORG 0X00
MOVLW BCD1
ANDLW 0X0F
IORLW 0X30
MOVWF LB1
MOVLW BCD1
ANDLW 0XF0
SWAPF WREG,W
IORLW 0X30
MOVWF HB1
MOVLW BCD2
ANDLW 0X0F
IORLW 0X30
MOVWF LB2
MOVLW BCD2
ANDLW 0XF0
SWAPF WREG,W
IORLW 0X30
MOVWF HB2
END

Output on MPLAB

packed BCD to ASCII conversion
packed BCD to ASCII conversion

ASCII to Packed BCD Conversion:

                        This is the reverse process of the packed BCD to ASCII. First, we need to convert ASCII into unpacked BCD and then unpacked is converted into packed BCD.

For Example:

ASCII Unpacked BCD Packed BCD
37H & 38H

0011 0111 & 0011 1000

07H & 08H

0000 0111 & 0000 1000

78H

0111 1000

39H & 31H

0011 1001 & 0011 0001

09H & 01H

0000 1001 & 0000 0001

91H

1001 0001

ASCII to packed BCD conversion in PIC on MPLAB

assembly language code

#INCLUDE<P18F4550.INC>
MYBCD EQU 0X20
ORG 0X00
MOVLW A’8′
ANDLW 0X0F
MOVWF MYBCD
SWAPF MYBCD,F
MOVLW A’2′
ANDLW 0X0F
IORWF MYBCD,F
END

ASCII to packed BCD conversion in PIC on MPLAB 
ASCII to packed BCD conversion in PIC on MPLAB

Why we need to perform packed BCD to ASCII conversion?

The following justifies the possibility that a packed BCD to ASCII conversion is required:

Output readable by humans:

It is frequently required to show numerical data in a human-readable format while interacting with the data in applications or systems. Numerical values can be represented as easily interpreted character strings by converting packed BCD to ASCII.

Data Display:

Numerical data is frequently represented in ASCII format in applications such as digital displays, LED or LCD panels, and printed output. Presenting numerical values in an easy-to-understand manner is possible by converting packed BCD to ASCII.

Communication with External Devices:

Using ASCII-encoded data is frequently more practical for interacting with external devices, such as printers, displays, or other systems. This guarantees interoperability and streamlines information sharing between various systems.

User Interfaces: ASCII representation of numeric data is frequently used in user interfaces, whether they are graphical or command-line interfaces. User interfaces can be seamlessly integrated by converting packed BCD to ASCII.

Data Processing and Manipulation:

To process or manipulate data further, it may occasionally be necessary to convert packed BCD to ASCII. For instance, when the numerical data must be handled as a string or utilised in procedures that demand input that is ASCII-encoded.

 

Related Topics

Leave a Reply

Your email address will not be published. Required fields are marked *