SECTION 6.5: BCD, ASCII, AND OTHER APPLICATION PROGRAMS
In this section we provide some real-world examples on how to use arithmetic and logic instructions. We will see their applications in real-world devices covered in future chapters. For example, many newer microcontrollers have a real time clock (RTC), where the time and date are kept even when the power is off. These microcontrollers provide the time and date in BCD. However, to display them they must be converted to ASCII. Next, we show the application of logic and rotate instructions in the conversion of BCD and ASCII.
Table 6-5: ASCII Code for Digits 0 – 9
On ASCII keyboards, when the key “0″ is activated, “Oil 0000″ (30H) is provided to the computer. Similarly, 31H (011 0001) is provided for the key “1″, and so on, as shown in Table 6-5.
It must be noted that although ASCII is standard in the United States (and many other countries), BCD numbers are universal. Since the keyboard, printers, and monitors all use ASCII, how does data get converted from ASCII to BCD, and vice versa? These are the subjects covered next.
Packed BCD to ASCII conversion
Many systems have what is called a real-time clock (RTC). The RTC provides the time of day (hour, minute, second) and the date (year, month, day) continuously, regardless of whether the power is on or off (see Chapter 16). However, this data is provided in packed BCD. For this data to be displayed on a device such as an LCD, or to be printed by the printer, it must be in ASCII format.
To convert packed BCD to ASCII, it must first be converted to unpacked BCD. Then the unpacked BCD is tagged with Oil 0000 (30H). The following demonstrates converting from packed BCD to ASCII. See also Example 6-34.
ASCII to packed BCD conversion
To convert ASCII to packed BCD, it is first converted to unpacked BCD (to get rid of the 3), and then combined to make packed BCD. For example, for 4 and 7 the keyboard gives 34 and 37, respectively. The goal is to produce 47H or “0100 0111″, which is packed BCD. This process is illustrated next.
After this conversion, the packed BCD numbers are processed and the result will be in packed BCD format. As we saw earlier in this chapter, a special instruction, “DA A”, requires that data be in packed BCD format.
Using a look-up table for ASCII
In some applications it is much easier to use a look-up table to get the ASCII character we need. This is a widely used concept in interfacing a keyboard to the microcontroller. This is shown in Example 6-35.
Assume that the lower three bits of PI are connected to three switches. Write a program to send the following ASCII characters to P2 based on the status of the switches.
Checksum byte in ROM
To ensure the integrity of the ROM contents, every system must perform the checksum calculation. The process of checksum will detect any corruption of the contents of ROM. One of the causes of ROM corruption is current surge, either when the system is turned on or during operation. To ensure data integrity in ROM, the checksum process uses what is called a checksum byte. The checksum byte is an extra byte that is tagged to the end of a series of bytes of data. To calculate the checksum byte of a series of bytes of data, the following steps can be taken.
Add the bytes together and drop the carries.
Take the 2′s complement of the total sum; this is the checksum byte, which
becomes the last byte of the series.
To perform the checksum operation, add all the bytes, including the checksum byte. The result must be zero. If it is not zero, one or more bytes of data have been changed (corrupted). To clarify these important concepts, see Example 6-36.
Assume that we have 4 bytes of hexadecimal data: 25H, 62H, 3FH, and 52H. (a) Find the checksum byte, (b) perform the checksum operation to ensure data integrity, and (c) if the second byte 62H has been changed to 22H, show how checksum detects the error.
Checksum program in modules
The checksum generation and testing program is given in modular form. We have divided the program into several modules (subroutines or subprograms) Dividing a program into several modules (called functions in C programming) allows us to use its modules in other applications. It is common practice to divide a program into several modules, test each module, and put them into a library. The checksum program shown next has three modules: It (a) gets the data from code ROM, (b) calculates the checksum byte, and (c) tests the checksum byte for any data error. Each of these modules can be used in other applications.
Binary (hex) to ASCII conversion
Many ADC (analog-to-digital converter) chips provide output data in binary (hex). To display the data on an LCD or PC screen, we need to convert it to ASCII. The following code shows the binary-to-ASCII conversion program. Notice that the subroutine gets a byte of 8-bit binary (hex) data from PI and converts it to decimal digits, and the second subroutine converts the decimal digits to ASCII digits and saves them. We are saving the low digit in the lower address location and the high digit in the higher address location. This is referred to as the Little-Endian convention, that is, low-byte to low-location and high-byte to high-location. All Intel products use the Little-Endian convention.
Binary-to-ASCII Conversion Program
This chapter discussed arithmetic instructions for both signed and unsigned data in the 8051. Unsigned data uses all 8 bits of the byte for data, making a range of 0 to 255 decimal. Signed data uses 7 bits for data and 1 for the sign bit, making a range of-128 to +127 decimal.
Binary coded decimal (BCD) data represents the digits 0 through 9. Both packed and unpacked BCD formats were discussed. The 8051 contains special instructions for arithmetic operations on BCD data.
In coding arithmetic instructions for the 8051, special attention has to be given to the possibility of a carry or overflow condition.
This chapter also defined the logic instructions AND, OR, XOR, and complement. In addition, 8051 Assembly language instructions for these functions were described. Compare and jump instructions were described as well. These functions are often used for bit manipulation purposes.
The rotate and swap instructions of the 8051 are used in many applications such as serial devices. This chapter also described checksum byte data checking, BCD and ASCII formats, and conversions.