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:
Unpacked & Packed BCD:
In bytes orientation, Binary Coded Decimal is divided into two systems which are following:
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
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
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:
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.
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.
|Packed BCD||Unpacked BCD||ASCII|
|03H & 05H
0000 0011 & 0000 0101
|33H & 35H
0011 0011 & 0011 0101
|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?
Here is the code first
HB1 EQU 0X06
LB1 EQU 0X07
HB2 EQU 0X32
LB2 EQU 0X33
BCD1 EQU 0X76
BCD2 EQU 0X87
Output on MPLAB
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.
|ASCII||Unpacked BCD||Packed BCD|
|37H & 38H
0011 0111 & 0011 1000
|07H & 08H
0000 0111 & 0000 1000
|39H & 31H
0011 1001 & 0011 0001
|09H & 01H
0000 1001 & 0000 0001
ASCII to packed BCD conversion in PIC on MPLAB
assembly language code
MYBCD EQU 0X20
- What are the conditional and unconditional branches in PIC microcontroller ?
- How to create a first project in PIC microcontroller using assembly language
- How many types of PIC microcontroller are available in market?
- what are the practical applications of embedded systems?
- How to import hex file into PIC microcontroller using PROTEUS
- PIC microcontroller timers programming
- Interfacing of seven segment display with pic16F877A microcontroller
- How to interface LCD with PIC18f4550 microcontroller?
- How to perform serial communication on PIC microcontroller?
- What is the minimum frequency signal that can be generated in PIC18F4550 microcontroller?
- What is the maximum frequency signal that can be generated in PIC18F4550 microcontroller?
- 16 bit timer programming of PIC18 microcontroller
- What is the CCP module of PIC microcontroller?
- What are the timer interrupts of PIC microcontroller?
- How to interface DC motor with PIC microcontroller?
- Briefly write about all pins of LCD for co
- nnecting with the PIC microcontroller
- Interrupt priority of PIC18 microcontroller
- What are the rotate instructions in PIC microcontroller?
- Application of DIV instruction in PIC microcontroller
- How to perform subtraction of unsigned numbers in PIC microcontroller?
- What are the factors that Influence the selection of Embedded Microcontrollers?
- How to program counters in microcontroller?
- How to calculate the time delays in PIC Programming?
- How to differentiate between different types of microcontrollers?
- What is the criteria for choosing a microcontroller?
- How to create a square wave program in PIC using assembly language
- How to interface Stepper Motor with PIC ?
- How to control the direction of DC motor with PIC ?
- what are the assembler directives in PIC?
- How temperature sensor is interfaced with PIC micro controller?
- How keypad is interfaced with PIC Controller?
- Registers that are used for serial communication in PIC
- Embedded Systems Future: Design of optimized Energy Metering Devices