DATA CONVERSION PROGRAMS IN 8051 C

SECTION 7.4: DATA CONVERSION PROGRAMS IN 8051 C
Recall that BCD numbers were discussed in Chapter 6. As stated there, many newer microcontrollers have a real-time clock (RTC) where the time and date are kept even when the power is off. Very often the RTC provides the time and date in packed BCD. However, to display them they must be converted to ASCII. In this section we show the application of logic and rotate instructions in the conversion of BCD and ASCII.
ASCII numbers
On ASCII keyboards, when the key “0″ is activated, “Oil 0000″ (30H) is provided to the computer. Similarly, 31H (Oil 0001) is provided for the key “1″, and so on, as shown in Table 7-4.
Table 7-4: ASCII Code for Digits 0 – 9

Packed BCD to ASCII conversion
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. However, this data is provided in packed BCD. 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 7-24.



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, 4 and 7 on the keyboard give 34H and 37H, respectively. The goal is to produce 47H or “0100 0111″, which is packed BCD.





After this conversion, the packed BCD numbers are processed and the result will be in packed BCD format Chapter 16 discusses the RTC chip and uses the BCD and ASCII conversion programs shown in Examples 7-24 and 7-25.
Example 7-25


Example 7-24


Checksum byte in ROM
To ensure the integrity of 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.
  1. Add the bytes together and drop the carries.
    1. 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 7-26.
Example 7-26
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.
Solution:


Example 7-27
Write an 8051 C program to calculate the checksum byte for the data given in Example 7-26.
Solution:


Single-step the above program on the 8051 simulator and examine the contents of PI and P2. Notice that each byte is put on PI as they are added together.
Example 7-28
Write an 8051 C program to perform step (b) of Example 7-26. If data is good, send ASCII character ‘G’ to PO. Otherwise send ‘B’ to PO.
Solution:


Binary (hex) to decimal and ASCII conversion in 8051 C
The printf function is part of the standard I/O library in C and can do many things, including converting data from binary (hex) to decimal, or vice versa. But printf takes a lot of memory space and increases your hex file substantially. For this reason, in systems based on the 8051 microcontroller, it is better to write your own conversion function instead of using printf.
One of the most widely used conversions is the binary to decimal conversion. In devices such as ADC (Analog-to-Digital Conversion) chips, the data is provided to the microcontroller in binary. In some RTCs, data such as time and dates are also provided in binary. In order to display binary data we need to convert it to decimal and then to ASCII. Since the hexadecimal format is a convenient way of representing binary data we refer to the binary data as hex. The binary data 00 – FFH converted to decimal will give us 000 to 255. One way to do that is to divide it by 10 and keep the remainder, as was shown in Chapter 6. For example, 11111101 or FDH is 253 in decimal. The following is one version of an algorithm for conversion of hex (binary) to decimal:


Example 7-29 shows the C program for that algorithm.
Example 7-29
Write an 8051 C program to convert 11111101 (FD hex) to decimal and display the digits on PO, PI, and P2.
Solution:

Next post:

Previous post: