• No se han encontrado resultados

7. ESTACIONES DE SIMULACIÓN

7.2. ESTACIÓN DISTRIBUIDORA

I hope I have convinced you of the usefulness of using Gray codes for inputs when you are illustrating how digital electronic logic functions respond to inputs. I must point out, however, that Gray codes were originally created for a much different function – they were designed for use in position sensors as the single changing bit allowed hardware to be designed to respond to a single changing bit and not the potentially several bits of a binary sequence. By only changing one bit at a time, absolutely precise positioning of marking sensors (causing all changing bits being sensed at the exact same instant) was not required.

Gray codes were invented by Frank Gray of Bell Labs in the mid 1950s and has a ‘‘hamming value’’ of 1. The hamming value is the number of bits that change between one value and the next. A four bit binary number can have all four bits change as it increments or decrements; a Gray code never has more than one bit change during incrementing or decrementing operations.

Chances are, you would not have any trouble coming up with a two bit Gray code (b’00’, b’01’, b’11’ and b’10’) and in a pinch, you would be able to come up with a three bit Gray code (b’000’, b’001’, b’011’, b’010’, b’110’, b’111’, b’101’ and b’100’). I suspect that if you were given the task of coming up with any more bits than this, you would be stumped.

In trying to come up with a way of explaining how Gray codes worked, I noticed that when a new most significant bit was set, the previous values were ORed with this bit, but written out in reverse order. In some texts, this property is recognized by calling Gray codes a binary reflected code. Looking at Table 4-6, you can see that I created a four bit

Table 4-6 Building Gray codes from previous value.

Binary value Gray code Comments

B’0000’ B’0000’ 1 bit Gray code, Gray code¼binary value B’0001’ B’0001’ 1 bit Gray code, Gray code¼binary value

B’0010’ B’0011’ 2 bit Gray code, last value of bit 1 (B’1’) list ORed B’0011’ B’0010’ 2 bit Gray code, first value of bit 1 (B’0’) list Used B’0100’ B’0110’ 3 bit Gray code, last value of bit 2 (B’10’) list ORed B’0101’ B’0111’ 3 bit Gray code, next last value of bit 2 bit (B’11’)

list ORed

B’0110’ B’0101’ 3 bit Gray code, next last value of bit 2 (B’01’) list ORed B’0111’ B’0100’ 3 bit Gray code, first value of bit 2 (B’00’) list ORed B’1000’ B’1100’ 4 bit Gray code, last value of bit 3 (B’100’) list ORed B’1001’ B’1101’ 4 bit Gray code, next last value of bit 3 (B’101’) list ORed B’1010’ B’1111’ 4 bit Gray code, next last value of bit 3 (B’111’) list ORed B’1011’ B’1110’ 4 bit Gray code, next last value of bit 3 (B’110’) list ORed B’1100’ B’1010’ 4 bit Gray code, next last value of bit 3 (B’010’) list ORed B’1101’ B’1011’ 4 bit Gray code, next last value of bit 3 (B’011’) list ORed B’1110’ B’1001’ 4 bit Gray code, next last value of bit 3 (B’001’) list ORed B’1111’ B’1000’ 4 bit Gray code, first value of bit 3 (B’000’) list ORed

Gray code by taking the eight values of the three bit code, reversing them and setting bit 3.

This could be written out as a computer program algorithm as:

for ði¼1; i<#Bits; iþ þÞ ==Loop Around for Each Code ifð1¼¼iÞ ==Trivial Initial Case

f

GrayCode½0 ¼0; GrayCode½1 ¼1;

gelsef ==Copy Previous Codes with ==New MSB Set

forðj¼ ð1<<ði1ÞÞ,k¼ ðð1<<ði1ÞÞ 1Þ; j<ð1<<iÞ; jþ þ,k Þ

GrayCode½j ¼ ð1<<ði1ÞÞ þGrayCode½k;

g ==fi

This code demonstrates how Gray codes are produced, but is not the optimal method for producing Gray codes (it is actually an ‘‘order n2’’

algorithm, which means that every time the number of bits is doubled, the amount of time required to produce the values is quadrupled). Along with this, it is not easy to create digital logic hardware that will create these codes.

Fortunately, individual binary codes can be converted to Gray codes using the circuit shown in Fig. 4-2, which simply implements the formula:

Gray Code¼Binary ^ðBinary>>1Þ

Going the other way (from Gray code to binary) is a bit more complex and while it uses n1 (where ‘‘n’’ is the number of bits) XOR gates, like convert- ing binary codes to Gray codes, the output of each XOR gate is required as an input to the next least significant bit, as shown in Fig. 4-3. The output of the circuit is not correct until the most significant bit has passed through each of the XOR gates to the least significant bit.

To perform the data conversion a simple formula cannot be used. Instead the following algorithm is required:

GrayCode¼Binary; Shift¼1; WhileððGrayCode>>ShiftÞ!¼0Þ

f

GrayCode¼GrayCode ^ðGrayCode>>ShiftÞ; Shift¼Shift2;

g == elihw

I find it very difficult to explain exactly how this code works, except to say that with each iteration of the while loop, the ‘‘Gray code’’ value gets shifted down more and more to move the most significant bits into position for XORing with the less significant bits. To convince yourself that the algorithm works, you might want to perform a ‘‘thought experiment’’ on it and list the changing value of ‘‘Gray code’’ as I have done in Table 4-7.

In this chapter, more than anywhere else in the book, I have used sample computer programs to show how different values can be produced. This is a somewhat different approach to explaining how multi-bit binary data conversions are implemented and one that takes advantage of the ubiquity of the personal computer and the ability of most technical students to perform even rudimentary programming.

Using computer code to help demonstrate how the conversions are done should also give you another method for processing binary values as well as of testing formulas and optimizations. I always find it useful to have a number of different ways to solve a problem, or test a potential solution,

and I suggest that along with the various tools and computer algorithms presented in this book that you try to come up with methods for yourself that will help you design and test digital electronic circuits more efficiently.

Quiz

1. If you had a number system that was base 5, the most significant value in a digit would be:

(a) 6 (b) 10

(c) 4 (d) 5

2. The eight bit binary equivalent to decimal 47 is: (a) 0010 1111

(b) B’0010 1111’

Table 4-7 Working through the shifting values of the Gray code convention algorithm.

Initial bit values Shift¼1 bit values Shift¼2 bit values Shift¼4 bit values

B7 B7 B7 B7 B6 B6 ^ B7 B6 ^ B7 B6 ^ B7 B5 B5 ^ B6 B5 ^ B6 ^ B7 B5 ^ B6 ^ B7 B4 B4 ^ B5 B4 ^ B5 ^ B6 ^ B7 B4 ^ B5 ^ B6 ^ B7 B3 B3 ^ B4 B3 ^ B4 ^ B5 ^ B6 B3 ^ B4 ^ B5 ^ B6 ^ B7 B2 B2 ^ B3 B2 ^ B3 ^ B4 ^ B5 B2 ^ B3 ^ B4 ^ B5 ^ B6 ^ B7 B1 B1 ^ B2 B1 ^ B2 ^ B3 ^ B4 B1 ^ B2 ^ B3 ^ B4 ^ B5 ^ B6 ^ B7 B0 B0 ^ B1 B0 ^ B1 ^ B2 ^ B3 B0 ^ B1 ^ B2 ^ B3 ^ B4 ^ B5 ^ B6 ^ B7

(c) 101111 (d) 1011 11

3. The third most significant digit in the decimal number ‘‘1234’’ is: (a) The hundreds column

(b) 3 (c) 1

(d) No digit can be the third most significant

4. To verbally tell somebody the hex number value 0x04AC you would say:

(a) ‘‘Four-Able-Charlie’’

(b) ‘‘Hexadecimal Four-Eh-See’’

(c) ‘‘Hexadecimal Four-Apple-Charlie’’ (d) ‘‘Hexadecimal Four-Able-Charlie’’ 5. The decimal number ‘‘123’’ in hexadecimal is:

(a) 0x0123 (b) B’0111 1011’

(c) 7B (d) 0x07B

6. The four bit hexadecimal number 0x01234 expressed in decimal is: (a) 1,234

(b) 4,660

(c) B’0001 0010 0011 0100’ (d) 0x04D2

7. Binary coded decimal is defined as:

(a) Ten bits providing ten different values

(b) Four bits providing ten numeric values and six control codes (c) Four bits providing ten numeric values

(d) Five bits with each bit providing two values for a total of 10 8. BCD should:

(a) Never be used

(b) Used with circuits that operate with base 10 numbers (c) Only be used when you’ve run out of binary chips (d) Used when values are not expected to exceed 9

9. B’0110’ in binary, using the formula Gray code¼binary ^ (binary 1) can be converted to the Gray code:

(a) B’1010’ (b) B’0110’

(c) B’0101’ (d) B’0111’

10. The Gray code B’0010’ corresponds to the binary value: (a) B’0011’

(b) Unknown because more data is required (c) B’1101’

CHAPTER

Binary Arithmetic

Documento similar