Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
Скачиваний:
475
Добавлен:
12.08.2013
Размер:
4.99 Mб
Скачать

72

Chapter 4

teenth digit but also a carry out. Carry in and carry out of the sign bit is one of the conditions for no overflow in the flowchart of Figure 4-7.

4.6 Data Format Conversions

Quite often code needs to convert data into and from different numeric formats; for example, to display ASCII digits in an output device, or to convert numeric keyboard input in ASCII into binary or BCD encodings for processing. In this section we consider the logic for the following cases:

1.BCD digits to ASCII decimal

2.Binary to string of ASCII decimal digits

3.String of ASCII decimal digits to binary

4.Binary to string of ASCII hexadecimal digits

As in the previous cases, implementation of these conversions is de- vice-dependent and varies in the different hardware.

4.6.1 BCD Digits to ASCII Decimal

Packed BCD digits are encoded in one digit-per nibble, as shown in Section 4.2.2. Thus, each digit is a binary value in the range 0 to 9. Converting each digit to ASCII consists of isolating each nibble and then changing the binary into an ASCII representation. Note in Table 3.1 that the numeric ASCII digits start at 30H for the digit zero and extend to 39H for the digit 9. For this reason converting a numeric digit from binary into ASCII consists of adding 30H. By the same token, subtracting 30H converts a single ASCII digit to binary.

Assume four packed BCD digits in two consecutive memory bytes, labeled A and

B, where A holds the two low-order digits; also, a four-digit storage buffer to which the variable P is a pointer. The conversion algorithm can be described as follows:

1.Initialize buffer pointer P to the first storage location.

2.Copy digit A to temporary digit T.

3.Mask out four high-order bits of T.

4.Add 30H to value in T and store in buffer by pointer P.

5.Bump buffer pointer to next digit storage.

6.Copy digit A to temporary digit T.

7.Mask out four low-order bits in T.

8.Shift four high-order bits to the right by 4 bits.

9.Add 30H to value in T and store in buffer by pointer P.

10.Bump buffer pointer to next digit.

11.Proceed with digit B in the same manner.

Digital Logic, Arithmetic, and Conversions

73

4.6.2 Unsigned Binary to ASCII Decimal Digits

Often we hold an unsigned binary number in memory or a machine register and need to display its value to some ASCII-based output device. The process requires converting the binary value to a string of ASCII decimal digits. The number of decimal digits depends on the number of bits in the binary representation. A one-byte unsigned binary requires three ASCII decimal digits since the value ranges from 0 to 255. A two-byte unsigned binary requires a string of five ASCII decimal digits since the range of a two-byte representation is from 0 to 65,535, and so on. The storage area for the ASCII digits is sometimes referred to as a buffer.

The process of converting binary to ASCII decimal consists of dividing the binary by 10 to obtain each decimal digit, then adding 30H to the remainder in order to turn the digit into ASCII. The process continues until the original dividend is reduced to zero, as shown in the flowchart of Figure 4-9.

START

B = BINARY NUMBER

R (REMAINDER) = 0

BUFFER to HOLD ASCII DIGITS

P = BUFFER POINTER

INIT TO LOWEST SIGNIFICANT DIGIT

 

YES

B = 0

DONE

?

NO

B = B / 10

DIGIT = R + 30H

STORE DIGIT BY P

UPDATE P TO NEXT DIGIT

Figure 4-9 Unsigned Binary to ASCII Decimal String

4.6.3 ASCII Decimal String to Unsigned Binary

Another conversion operation frequently needed in software is the transformation of a string of ASCII decimal digits into binary. This type of conversion typically arises when the program needs to receive input which must later be processed by the device. For example, the user enters a numeric value from a keyboard and the application must process this data in binary form.

In designing the conversion routine we must first delimit the value range of the input data so as to allocate a sufficiently large binary format to store the result. For example, code can store in a single unsigned byte a binary in the range 0 to 255, but it requires two bytes to store one in the range 0 to 65,535. Once the binary storage size is determined, the conversion logic is based on converting each ASCII digit to binary, high-to-low, and adding its value to a previous sum multiplied by 10. The following flowchart describes the conversion logic.

74

Chapter 4

START

B = BINARY NUMBER

A = ASCII DECIMAL DIGIT

BUFFER HOLDS ASCII DIGITS

P = BUFFER POINTER

INIT TO HIGHEST SIGNIFICANT DIGIT

A = DIGIT => BY P

END OF

YES

ASCII STRING

DONE

?

 

NO

A = A - 30H

B = (B * 10) + A

UPDATE P TO NEXT DIGIT

Figure 4-10 Decimal String to Unsigned Binary

The logic in the flowchart of Figure 4-10 assumes that there is some way of detecting the end of the string of ASCII digits. This could be a terminator character embedded in the string or a counter for the number of digits. Here again we use a buffer pointer that is initialized to the least significant digit in the ASCII string. The ASCII digit is converted to binary by subtracting 30H and then added to the previous sum, multiplied by 10. For example, assume the ASCII string of the decimal digits 564.

STRING = ‘564’

FIRST ITERATION:

STEP 1: B =

0

 

 

P => HIGH DIGIT IN

STRING ‘564’

STEP 2: A =

‘5’

 

 

STEP 3: END

OF STRING?

 

NO

 

 

 

STEP 4: A =

4 (‘5’ –

30H =

5)

B =

(0 * 10)

+ A =

5

P TO NEXT LOWER DIGIT

SECOND ITERATION:

 

 

STEP 2: A =

‘6’

 

 

STEP 3: END

OF STRING?

 

NO

 

 

 

STEP 4: A =

6 (‘6’ –

30H =

6)

B =

(5 * 10)

+ A =

56

P TO NEXT LOWER DIGIT

THIRD ITERATION:

STEP 2: A = ‘4’

STEP 3: END OF STRING?

NO

STEP 4: A = 4 (‘4’ – 30H = 4)

B = (56 * 10) + A = 564

Digital Logic, Arithmetic, and Conversions

75

P TO NEXT LOWER DIGIT

FOURTH ITERATION:

STEP 2: A = ??

STEP 3: END OF STRING?

YES

RESULT: B = 564

4.6.4 Unsigned Binary to ASCII Hexadecimal Digits

Converting a binary number to a string of ASCII hex digits is quite similar to converting from binary to an ASCII decimal string, as described in Section 4.6.2. Here again, the digit space to allocate for the ASCII string depends on the size of the binary operand. An 8-bit binary is represented in two ASCII hex digits, a 16-bit binary into four ASCII hex digits, and so on.

The process of converting binary to ASCII hexadecimal consists of dividing the binary by 16 to obtain each hex digit. If the remaining hexadecimal digit is in the range 0 to 9 we add 30H to turn it into the corresponding ASCII digit. If it is in the range A to F then we must add 40H to convert into ASCII. The process continues until the original dividend is reduced to zero, as shown in the flowchart of Figure 4-11.

START

B = BINARY NUMBER

R (REMAINDER) = 0

BUFFER TO HOLD ASCII HEX DIGITS

P = BUFFER POINTER

INIT TO LOWEST SIGNIFICANT DIGIT

PERFORM B = B / 16

R = REMAINDER

 

 

 

 

 

YES

 

B = 0

 

 

 

 

DONE

 

 

 

 

 

 

?

 

 

 

 

 

 

 

 

 

NO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

YES

 

 

 

 

0 =< R =< 9

 

 

R = R + 30H

 

 

 

 

 

 

 

 

 

 

 

?

NO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

R = R + 40H

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

STORE R (DIGIT) BY P

UPDATE P TO NEXT DIGIT

Figure 4-11 Unsigned Binary to ASCII Hexadecimal String

76

Chapter 4

4.6.6 Signed Numerical Conversions

Conversion routines that use signed operands are usually a variation of the unsigned ones described in previous sections. Although logic can be developed that directly encodes to and from two’s complement format, the more convenient approach is to determine the sign of the operand then use unsigned conversion for the digit values. For example, to convert a signed binary in two’s complement form into a string of ASCII decimal digits the logic first determines if the binary operand is negative or positive. If a positive number, then the unsigned conversion routine can be used directly. If the binary operand is a negative number, the minus sign is placed in the storage buffer. Then the two’s complement binary is converted to an unsigned number so that the ASCII digits can be obtained with the conversion routine described in Section 4.6.2.