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.

DecimalHexPacked BCD(hex)Unpacked BCD(hex)ASCII
580x3A0x580x05, 0x080x35, 0x38
640x400x640x06, 0x040x36, 0x34

Decimal to ASCII conversions

DecimalASCII (hexadecimal)
00x30
10x31
20x32
30x33
40x34
50x35
60x36
70x37
80x38
90x39

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

  1. 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

  2. We will perform a right-shift operation on a to convert it to the desired unpacked format.

    a = a >> 4

  3. Let's convert these variables into ASCII format using binary OR operation.

    a = a | 0x30 ;

    b = b | 0x30;

  4. 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

  1. Store these values in a local variable.

    unsigned char a = '4';

    unsigned char b = '9';

  2. Mask the upper 4 bits in a and b

    a = a & 0x0F;//masking 3, a = 0x04

    b = b & 0x0F;//masking 3, b = 0x09

  3. We have successfully converted a and b into packed BCD. In this step, we will convert them to packed BCD. For this, we will first perform left shift operation on a and then binary-or operation on a and b and store the result in new variable.

    a = a << 4;

    unsigned char bcd = a | b; // bcd = 0x49

  4. 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

  1. Add bytes together and drop carriers.

  2. Take 2's complement of the result obtained in the above operation.

Example

  1. Find the checksum byte for the sequence of data 84H, A0H, 1DH, and 3AH.

    | Data sequence | | | --- | --- | | 0x84 | | | 0xA0 | | | 0x1D | | | 0x3A | | | 0x8D | Result |

  2. Let's find the 2's complement of 0xFF

    2's complement of 0x8D = 0x73

  3. 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

  1. ASCII TO BCD.

  2. Packed BCD to ASCII.

  3. CHECKSUM PROGRAM.