Data Conversion In 8051
Data is stored in the form of binaries in the memory of any microcontroller or microprocessor. However, this data stored in the memory has to be converted in data formats such as packed BCD or unpacked BCD, ASCII(American standard code for information interchange) or Unicode. Microcontrollers do not deal much with the data in Unicode formats but they most often deal with packed BCD, unpacked BCD or ASCII. In case you don't know much about packed BCD or unpacked BCD we will briefly discuss them with examples in this blog.
Much newer microcontroller has built-in real-time counter(RTC) inside them.RTC keeps track of time and date independent of the power supply to the microcontroller. If the microcontroller is off still RTC keeps track of time and data. It stores time in hours, minutes and seconds and dates in year, month, and date. RTC store date and time in packed BCD. In case we want to display these values on the LCD screen we would have to convert those values from packed BCD to ASCII.
I will be discussing conversion programs and how to perform checksum and it's application in data integrity.
Decimal | Hex | Packed BCD(hex) | Unpacked BCD(hex) | ASCII |
58 | 0x3A | 0x58 | 0x05, 0x08 | 0x35, 0x38 |
64 | 0x40 | 0x64 | 0x06, 0x04 | 0x36, 0x34 |
Decimal to ASCII conversions
Decimal | ASCII (hexadecimal) |
0 | 0x30 |
1 | 0x31 |
2 | 0x32 |
3 | 0x33 |
4 | 0x34 |
5 | 0x35 |
6 | 0x36 |
7 | 0x37 |
8 | 0x38 |
9 | 0x39 |
Let's write an algorithm for a program for converting packed BCD to ASCII and displaying it on any port.
Let's take the packed BCD 0x65 for conversion.
steps
We will have to extract the upper 4 bits and the lower 4 bits separately.
We will mask the lower 4 and upper 4 bits using binary anding operation.
unsigned char a = 0x65 & 0xF0 ; // a = 0x60
unsigned char b = 0x65 & 0x0F ; // b = 0x05
We will perform a right-shift operation on
a
to convert it to the desired unpacked format.a = a >> 4
Let's convert these variables into ASCII format using binary OR operation.
a = a | 0x30 ;
b = b | 0x30;
Output this to ports P1 and P2
P1 = a
P2 = b
I will provide a link for my GitHub repo where you will find all codes used in this blog.
Now let's write a program to convert ASCII to packed BCD and display it on any port.
Let the ASCII characters be '4' and '9'.
steps
Store these values in a local variable.
unsigned char a = '4';
unsigned char b = '9';
Mask the upper 4 bits in
a
andb
a = a & 0x0F;//masking 3, a = 0x04
b = b & 0x0F;//masking 3, b = 0x09
We have successfully converted
a
andb
into packed BCD. In this step, we will convert them to packed BCD. For this, we will first perform left shift operation ona
and then binary-or operation ona
andb
and store the result in new variable.a = a << 4;
unsigned char bcd = a | b; // bcd = 0x49
Finally, output the value on port P1.
P1 = bcd;
CHECKSUM
To ensure the integrity of ROM every system performs a checksum operation on the data. A checksum byte generated from a series of data is stored at the end of this data in the memory. The system periodically performs this operation to check if the data is error-free or not.
Steps to calculate the checksum
Add bytes together and drop carriers.
Take 2's complement of the result obtained in the above operation.
Example
Find the checksum byte for the sequence of data 84H, A0H, 1DH, and 3AH.
| Data sequence | | | --- | --- | | 0x84 | | | 0xA0 | | | 0x1D | | | 0x3A | | | 0x8D | Result |
Let's find the 2's complement of 0xFF
2's complement of 0x8D = 0x73
Hence the checksum of the above sequence of data is 0x73
Below are some of the GitHub links where you can refer to the code