Lecture 2 Information Representation by ivz21134

VIEWS: 10 PAGES: 80

									          Lecture 2
 Information Representation

Pradondet Nilagupta
Department of Computer Engineering
Kasetsart University
           Acknowledgement
           This lecture note is modified from lecture
           note of Jim Maxted “Introduction to digital
           design”
               http://www.engineering.uiowa.edu/digital/f2005/digit
               al.htm




204222 Digital System Design      Friday, July 30, 2010               2
           Digital Hardware Systems
        Digital Systems
                 Digital vs. Analog Waveforms

            +5                                         +5
                  1       0    1
            V                                           V
                                   Time                                 T ime
            –5                                         –5


          Digital:                                   Analog:
          only assumes discrete values                values vary over a broad range
                                                      continuously




204222 Digital System Design              Friday, July 30, 2010                        3
           Digital Circuits
              Digital circuit can be represented by a black-box
              with inputs on one side, and outputs on the other.

               inputs              Digital                outputs
                               :   circuit         :


     The input/output signals are discrete/digital in nature,
     typically with two distinct voltages (a high voltage and a
     low voltage).

               High

               Low


     In contrast, analog circuits use continuous signals.
204222 Digital System Design                 Friday, July 30, 2010   4
           Digital Hardware Systems
           Digital Binary System
               Two discrete values:
                 •      yes, on, 5 volts, current flowing, "1"
                 •      no, off, 0 volts, no current flowing, "0”
               Advantage of binary systems:
                 • rigorous mathematical foundation based on logic
                 • it‟s easy to implement           both the door must
                                                                   be open and the car
        IF the garage door is open                                 running before I can
        AND the car is running                                     back out
        THEN the car can be backed out of the garage
       the preconditions must be true to imply the conclusion


204222 Digital System Design               Friday, July 30, 2010                          5
           Digital Circuits
           Advantages of Digital Circuits over Analog Circuits:
             more reliable (simpler circuits, less noise-prone)
             specified accuracy (determinable)
             but slower response time (sampling rate)

           Important advantages for two-valued Digital Circuit:
               Mathematical Model – Boolean Algebra
               Can help design, analyse, simplify Digital Circuits.



204222 Digital System Design       Friday, July 30, 2010               6
           Binary Bit and Group Definitions
           Bit - a single binary digit
           Nibble - a group of four bits
           Byte - a group of eight bits
           Word - depends on processor; 8, 16, 32, or
           64 bits
           LSB - Least Significant Bit (on the right)
           MSB - Most Significant Bit (on the left)



204222 Digital System Design   Friday, July 30, 2010    7
           Binary Representation of Information
           Information divided into groups of symbols
               26 English letters
               10 decimal digits
               50 states in USA
           Digital systems manipulate information as
           1‟s & 0‟s
           The mapping of symbols to binary value is
           known as a “code”
           The mapping must be unique

204222 Digital System Design        Friday, July 30, 2010   8
           Minimum number of bits
           In binary, „r‟ bits can represent n = 2r
           symbols
               e.g. 3 bits can represent up to 8 symbols, 4 for
               16, etc.
               For N symbols to be represented, the minimum
               number of bits required is the lowest integer „r‟
               that satisifies the relationship:
                                     2r  N
                 e.g. if N = 26, minimum r is 5 since
                 24 = 16
                 25 = 32

204222 Digital System Design         Friday, July 30, 2010         9
           Positional Number Systems
           Numeric value is represented by a series of
           digits
               Number of digits used is fixed by radix
               Digits multiplied by a power of the radix
               Digit order determines radix powers
           Very large numbers can be represented
           Can also represent fractional values.



204222 Digital System Design      Friday, July 30, 2010    10
           Positional Integer Number Values

        Given a digit series of
                          An1... A3 A2 A1 A0.(Radix point )
        The full expression for the represented value is

                               n 1
                   An1  r            ... A3  r  A2  r  A1  r        A0  r
                                                3            2          1            0


                   or
                   i  n 1

                    Ai  r
                                 i

                    i 0




204222 Digital System Design                    Friday, July 30, 2010                    11
           Positional Fractional Number Values

        Given a digit series of

                                      (Radix point)    A A A A
                                                          1    2     3       4
                                                                                     ... Am

        The full expression for the represented value is

                             1                 2              3              4                    m
               A r 1
                                   A 2  r  A3  r               A r
                                                                      4
                                                                                      ... A m  r
               or
               i  m

                A r
                                  i
                         i
               i  1




204222 Digital System Design                            Friday, July 30, 2010                              12
           Binary Number System
           Just like decimal numbers except
               The only valid digits are 0 and 1
               The base is 2 instead of 10
           Binary to decimal conversion is just the
           explicit expression of the positional values,
           both integer and fraction
               E.G.            1 0 1
                                       1 x 20 = 1
                                       0 x 21 = 0
                                       1 x 22 = 4

                                         Total = 5

204222 Digital System Design       Friday, July 30, 2010   13
           Decimal to Binary Conversion
           Effectively the reverse of binary to decimal
           conversion
               Integers:
                 • Divide number by two; keep track of remainder
                 • Repeat with dividend = last quotient until zero
                 • First remainder is binary LSB, last is the MSB
               Fractions:
                 • Multiply fraction by two; keep track of integer part
                 • Repeat with multiplier = last product fraction
                 • First integer is MSB, last is the LSB
                 • Conversion may not be exact; a repeated fraction

204222 Digital System Design          Friday, July 30, 2010               14
           Decimal to Binary Conversion (cont.)
       E.G. 13.2 to binary

       Integer                         Fraction

       13 / 2    = 6 R 1 LSB           .2 x 2     = 0.4 MSB
        6/2      =3R0                  .4 x 2     = 0.8
        3/2      =1R1                  .8 x 2     = 1.6
        1/2      = 0 R 1 MSB           .6 x 2     = 1.2
                                       .2 x 2     = 0.4 LSB repeating

       Result is 1101.00110011…...

                  If you’re not sure of the results, convert
                      back to decimal to check yourself.

204222 Digital System Design         Friday, July 30, 2010              15
           Octal and Hexadecimal Number Systems
           Both are positional systems with different
           radix and digits
               Octal:
                 • Radix = 8
                 • Digits = 0,1,2,3,4,5,6,7
               Hexadecimal:
                 • Radix = 16
                 • Digits = 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
           Primary advantage of both is it‟s easy to
           convert to/from binary

204222 Digital System Design          Friday, July 30, 2010   16
           Octal and Hexadecimal Conversions
           To/From decimal is same technique with a
           radix of 8 or 16 instead of 2
           To convert from binary:
               Starting at radix point, go left/right and group
               bits into groups of 3 or 4 bits / group
               Convert each bit group into equivalent octal or
               hex digit
           To convert to binary expand each octal /
           hex digit into equivalent 3 or 4 bit binary
           value.

204222 Digital System Design      Friday, July 30, 2010           17
           Octal, Hex Conversion Example



                  4      E         B       A . 6       2       B      2   8
                  0100111010111010.01100010101100101000

                   4 7         2       7   2 . 3   0       5    3     1   2




204222 Digital System Design                  Friday, July 30, 2010           18
           Numeric Information Representation
           Numeric information has some special
           characteristics which influence the was it is
           represented
               Number set is usually in positional notation
               There is a defined range of numbers
               There is a specified resolution for the set
           In general, numeric representations:
               are in some form of positional binary notation
               have no. of bits determined by range and res.

204222 Digital System Design      Friday, July 30, 2010         19
           Numeric Representations (cont.)
           The number of values in the set of numbers is
           found from the following equation

                                            R
                                                       - R MIN
                               N   VALUES
                                                 MAX
                                                                 1
                                                   RES
                           where RMAX and RMIN are the maximum and
                        minimum range values and RES is the resolution

           The minimum number of bits needed must
           meet the relationship already presented


204222 Digital System Design                Friday, July 30, 2010        20
           Numeric Representations (cont.)
           For example, the set of numbers from -5 to
           +10 with a resolution of 1 has 16 values

                               [+15 -(-5) ] / 1 = 16

              Therefore the minimum number of bits is 4

                                     24 = 16

204222 Digital System Design         Friday, July 30, 2010   21
           Numeric Representations (cont.)
           For the set of numbers from 0 to 100 with
           a resolution of 10 we have 11 values

           0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

           For the set of numbers from 0 to 5 with a
           resolution of 0.1 we have 51 values

                               [(5 - 0) / 0.1] + 1 = 51

204222 Digital System Design            Friday, July 30, 2010   22
           Numeric Representations (cont.)
           The actual representation could be any
           unique binary assignment but is usually of a
           positional form
               binary integer.fraction with sufficient bits to meet
               the range and resolution criteria
               binary integer form where the number of bits is
               as previously defined and the LSB value is the
               desired resolution



204222 Digital System Design      Friday, July 30, 2010               23
           Numeric Representations (cont.)
           EG: Represent 0 to 5, resolution = 0.1
               integer.fraction notation implies 3 bits for the
               integer (6 values) and 4 bits for the fraction (2-4 =
               0.0625) for a total of 7 bits
                2.3 represented by 010.0101 (closest fraction)
               integer * res notation requires 51 values or 6 bits;
               each value in set is represented by the equivalent
               binary integer = value / res
                 2.3 represented by binary 010111 (2.3 / 0.1)


204222 Digital System Design      Friday, July 30, 2010                24
           Numeric Representations (cont.)
           Negative ranges are handled by special
           assignments or negative number
           representations

           These are the most common numeric
           representations BUT they are certainly not
           the only ones!



204222 Digital System Design   Friday, July 30, 2010    25
           Representation of Signed Numbers
           Positive number representation same in
           most systems
               Standard positional binary notation
               MSB is the sign bit; 0 = plus, 1 = minus
           Major differences are in how negative
           numbers are represented
           Three major schemes:
               sign and magnitude
               ones complement
               twos complement
204222 Digital System Design     Friday, July 30, 2010    26
           Negative Number Representation
           Assumptions:
               we'll assume a 4 bit machine word
               16 different values can be represented
               roughly half are positive, half are negative
               sign bit is the MSB; 0 = plus, 1 = minus




204222 Digital System Design      Friday, July 30, 2010       27
                Sign-Magnitude Representation
                            +0                            High order bit is sign: 0 = positive
                      -7           +1                     (or zero), 1 = negative
                           0000
           -6       1111          0001    +2
                1110                 0010                 Three low order bits is the
      -5                                         +3       magnitude: 0 (000) thru 7 (111)
               1101                     0011

    -4     1100                           0100    +4      Number range for n bits = ±2n-1 - 1
           1011                         0101
     -3                                           +5      Two representations for 0
                1010                  0110
          -2       1001           0111
                           1000
                                             +6
                -1                   +7                   The major disadvantage is that
                           -0
                                                          we need separate circuits to
                                                          both add and subtract
                       0 100 = +4
                       1 100 = - 4                        Number magnitudes need to be
                                                          compared to get the right result

204222 Digital System Design                           Friday, July 30, 2010                     28
           Representing -N
           What we really want is -N
               Do A - B as A + (-B)
           We really are working in a closed, modulo
           number system; 0 to 2r-1 values
           Therefore for r bits, 2r  0
           If -N  0 - N then -N  2r - N

                         This is the 2’s complement
                            representation for -N
204222 Digital System Design       Friday, July 30, 2010   29
                Twos Complement Representation


                       -1
                            +0
                                    +1                         Only one representation for 0
                            0000
            -2       1111          0001    +2
                 1110                 0010
        3
                1101                     0011
                                                  +3           One more negative number
            1100                           0100
                                                               than positive number
      -4                                           +4
            1011                         0101
      -5                                           +5
                 1010                  0110                    Generation of the 2‟s
           -6       1001
                            1000
                                   0111       +6               complement as 2r - N implies
                 -7
                            -8
                                      +7
                                                               r + 1 bits available in system




204222 Digital System Design                            Friday, July 30, 2010                   30
           Twos Complement Operations
            Example: Twos complement of 7               24 = 10000
                                                  sub 7         =   0111

                                                                    1001 = repr. of -7

          Example: Twos complement of -7                24 = 10000
                                                 sub -7 =           1001

                                                                    0111 = repr. of 7
                               Shortcut method:
                                 Twos complement = bitwise complement + 1
      N* = 2r - N
                                0111 -> 1000 + 1 -> 1001 (representation of -7)

                                1001 -> 0110 + 1 -> 0111 (representation of 7)

204222 Digital System Design            Friday, July 30, 2010                            31
           Ones Complement Representation
      Ones Complement
         N is positive number, then N is its negative 1's complement
                        n
                  N = (2 - 1) - N                  2 4 = 10000

                                                   -1    = 00001
         Example: 1's complement of 7
                                                                1111

                                                   -7    =      0111
         Shortcut method:
                                                                1000 = -7 in 1's comp.
              simply compute bit wise complement

              0111 -> 1000


204222 Digital System Design            Friday, July 30, 2010                            32
                Ones Complement Representation

                           +0                                 like 2's comp except shifted
                      -0           +1                         one position counter-clockwise
                           0000
           -1       1111          0001    +2
                1110                 0010
      -2
               1101                     0011
                                                 +3             Subtraction implemented by
    -3     1100                           0100    +4
                                                                addition & 1's complement
           1011                         0101
     -4                                           +5
          -5
                1010                  0110                      Still two representations of 0!
                                                                This causes some problems
                   1001           0111       +6
                           1000
                -6                   +7
                           -7

                                                                Some complexities in addition


204222 Digital System Design                           Friday, July 30, 2010                      33
           Addition and Subtraction of Numbers
      Sign and Magnitude
                                             4      0100           -4    1100
     result sign bit is the same as
     the operands„ sign                   +3        0011        + (-3)   1011
                                             7      0111           -7    1111




  when signs differ, operation is            4      0100           -4    1100
  subtract, sign of result depends on
  sign of number with the larger           -3       1011          +3     0011
  magnitude
                                             1      0001           -1    1001


204222 Digital System Design            Friday, July 30, 2010                   34
           Addition and Subtraction of Numbers
      Ones Complement Calculations
                                              4      0100             -4       1011
                                           +3        0011         + (-3)       1100
                                              7      0111             -7      10111
                                                          End around carry
                                                                                    1

                                                                               1000
                                              4      0100             -4     1011
                                            -3       1100            +3      0011
                                              1     10000             -1     1110
                               End around carry
                                                          1

                                                     0001
204222 Digital System Design             Friday, July 30, 2010                          35
           Addition and Subtraction of Numbers

        Ones Complement Calculations

           Why does end-around carry work?
                                              n
               Its equivalent to subtracting 2 and adding 1
                                         n
               M - N = M + N = M + (2 - 1 - N) = (M - N) n 2 - (M > N)
                                                            +  1
                                        n         n
              -M + (-N) = M + N = (2 - M - 1) + (2 - N - 1)                n-1
                                                                      M+N<2
                                      n   n
                                     = 2 + [2 - 1 - (M + N)] - 1
              after end around carry:
                                       n
                                 = 2 - 1 - (M + N)
              this is the correct form for representing -(M + N) in 1's comp!


204222 Digital System Design             Friday, July 30, 2010                   36
           Addition and Subtraction of Numbers
            Twos Complement Calculations
                                       4      0100            -4    1100
                                    +3        0011         + (-3)   1101
     If carry-in to sign =
     carry-out then ignore             7      0111            -7    11001
     carry

     if carry-in differs from          4      0100            -4    1100
     carry-out then overflow
                                     -3       1101           +3     0011
                                       1     10001            -1    1111
      Simpler addition scheme makes twos complement the most common
      choice for integer number systems within digital systems

204222 Digital System Design       Friday, July 30, 2010                    37
           Addition and Subtraction of Numbers
              Twos Complement Calculations
                Why can the carry-out be ignored?
                -M + N when N > M:
                                   n                    n
                    M* + N = (2 - M) + N = 2 + (N - M)
                   Ignoring carry-out is just like subtracting 2 n


                -M + -N where N + M < or = 2 n-1
                                            n         n
                    -M + (-N) = M* + N* = (2 - M) + (2 - N)
                                           n              n
                                        = 2 - (M + N) + 2

           After ignoring the carry, this is just the right twos compliment
           representation for -(M + N)!

204222 Digital System Design             Friday, July 30, 2010                38
           Two’s Complement Overflow
       Consider two 8-bit 2‟s complement numbers. I can represent
       the signed integers -128 to +127 using this representation.

       What if I do (+1) + (+127) = +128.     The number +128 is
       OUT of the RANGE that I can represent with 8 bits. What
       happens when I do the binary addition?
          +127 = 7F
          + +1 = 01
          -------------------
             128 /= 80 (this is actually -128 as a twos
          complement number!!! - the wrong answer!!!)

          How do I know if overflowed occurred? Added two
          POSITIVE numbers, and got a NEGATIVE result.
204222 Digital System Design     Friday, July 30, 2010               39
           Detecting Two’s Complement Overflow

     Two‟s complement overflow occurs is:

         Add two POSITIVE numbers and get a NEGATIVE result
         Add two NEGATIVE numbers and get a POSITIVE result

     I CANNOT get two‟s complement overflow if I add a NEGATIVE
     and a POSITIVE number together.


     The Carry out of the Most Significant Bit means nothing if the
     numbers are two‟s complement numbers.



204222 Digital System Design      Friday, July 30, 2010               40
               Overflow Conditions
                   Add two positive numbers to get a negative number
                   or two negative numbers to get a positive number
                            -1             +0                                                         -1             +0
                   -2             1111   0000           +1                                   -2             1111   0000           +1
                           1110                 0001                                                 1110                 0001
          -3                                                  +2                    -3
                    1101                                                                      1101                                      +2
                                                  0010                                                                      0010
     -4                                                                       -4
                   1100                                0011        +3                        1100                                0011        +3
    -5             1011                                                       -5             1011
                                                       0100    +4                                                                0100    +4
                    1010                                                                      1010
         -6                                       0101                             -6                                       0101
                        1001
                                                              +5                                                                        +5
                                                0110                                              1001                    0110
              -7                1000     0111           +6                              -7                1000                    +6
                                                                                                                   0111
                           -8                   +7                                                   -8                   +7

                                           5 + 3 = -9                           -7 - 2 = +7


204222 Digital System Design                                        Friday, July 30, 2010                                                         41
           Overflow Conditions
                       0111                              1000
            5           0101             -7               1001
            3           0011             -2               1100
           -8           1000              7              10111
                      Overflow                           Overflow

                       0000                              1111
            5           0101             -3               1101
            2           0010             -5               1011
            7             0111           -8          11000
                  No overflow                     No overflow
        Overflow when carry in to sign does not equal carry out
204222 Digital System Design     Friday, July 30, 2010              42
           Comparison ( n=4 )
             Decimal           Signed Magnitude     One’s Complement    Two’s Complement

               -8         -               -              1000
               -7        1111                 1000          1001
               -6        1110                 1001          1010
               -5        1101                 1010          1011
               -4        1100                 1011          1100
               -3        1011                 1100          1101
               -2        1010                 1101          1110
               -1        1001                 1110          1111
                0      0000 or 1000            0000 or 1111      0000
                1        0001                 0001          0001
                2        0010                 0010          0010
                3        0011                 0011          0011
                4        0100                 0100          0100
                5        0101                 0101          0101
                6        0110                 0110          0110
                7        0111                 0111          0111
204222 Digital System Design                   Friday, July 30, 2010                       43
           Floating Point Numbers (I)
           Fixed point numbers have limited range.
           To represent very large or very small
           numbers, we use floating point numbers (cf.
           scientific numbers).
           Examples:
           0.23 x 1023 (very large positive number)
           -0.1239 x 10-10 (very small negative number)


204222 Digital System Design   Friday, July 30, 2010      44
           Floating Point Numbers (II)
           Floating point numbers have three parts:
                  mantissa, base, and exponent
           Base is usually fixed for each number system.
           Therefore, needs only mantissa and
           exponent.


                    mantissa                           exponent

204222 Digital System Design   Friday, July 30, 2010              45
           Floating Point Numbers (III)
           Mantissa is usually in normalised form:
               (base 10) 23 x 1021 normalised to 0.23 x 1023
               (base 10) -0.0017 x 1021 normalised to -0.17 x 1019
               (base 2) 0.01101 x 23 normalised to 0.1101 x 22
           A 16-bit floating point number may have 10-bit
           mantissa and 6-bit exponent.
           More bits in exponent gives larger range.
           More bits for mantissa gives better precision.



204222 Digital System Design        Friday, July 30, 2010            46
           Arithmetic with Floating Point Numbers (I)
           Arithmetic is more difficult for floating point
           numbers.
           MULTIPLICATION
            Steps: (i) multiply the mantissa
                    (ii) add-up the exponents
                    (iii) normalise
           Example:
               (0.12 x 102)10 x (0.2 x 1030)10
               = (0.12 x 0.2)10 x 102+30
               = (0.024)10 x 1032 (normalise)
               = (0.24 x 1031)10

204222 Digital System Design     Friday, July 30, 2010       47
           Arithmetic with Floating Point Numbers
           (II)
          ADDITION
            Steps: (i) equalise the exponents
                           (ii) add-up the mantissa
                          (iii) normalise
     ° Example:
         (0.12 x 103)10 + (0.2 x 102)10
          = (0.12 x 103)10 + (0.02 x 103)10 (equalise exponents)
          = (0.12 + 0.02)10 x 103           (add mantissa)
          = (0.14 x 103)10

     ° Can you figure out how to do perform SUBTRACTION
       and DIVISION for (binary/decimal) floating-point
       numbers? (Hint: remember sometimes, the mantissa
       might need normalizing)
204222 Digital System Design          Friday, July 30, 2010        48
           Weighted and Unweighted Codes
           Most numeric number representations are
           in a class known as “Weighted Codes”
           where              r -1
                               Value   b i  w i
                                        i 0

           Binary integers and fractions are special
           case where weights are powers of 2
           Unweighted codes are codes that cannot be
           assigned a weight value for each bit

204222 Digital System Design           Friday, July 30, 2010   49
           Binary Coded Decimal

           Four bits are used to represent each decimal
           digit
               In each 4-bit group, 6 values are not used
               Many possible codes, natural BCD (equivalent
               binary digits) most common
               BCD is not as efficient as binary
           BCD is easy to convert to/from decimal (it
           really is decimal with different symbols)
           BCD add/subtract circuits are complex
204222 Digital System Design    Friday, July 30, 2010         50
           BCD Code Examples
     Weighted codes                                                  Unweighted code
                               Digit   8421    84-2-1         XS3

                                0      0000     0000          0011
                                1      0001     0111          0100
                                2      0010     0110          0101
                                3      0011     0101          0110
                                4      0100     0100          0111
                                5      0101     1011          1000
                                6      0110     1010          1001
                                7      0111     1001          1010
                                8      1000     1000          1011
                                9      1001     1111          1100
              The 8421 or natural BCD code is the most
                     common BCD code in use
204222 Digital System Design              Friday, July 30, 2010                    51
           BCD Addition
       Case 1:            0001     1             Case 2:           0110     6
                          0101     5                               0101     5
                      (0) 0110 (0) 6                           (0) 1011 (1) 1

                                       WRONG!

       Case 3:            1000     8          Note that for cases 2 and 3,
                          1001     9          adding a factor of 6 (0110)
                      (1) 0001 (1) 7          gives us the correct result.




204222 Digital System Design           Friday, July 30, 2010                    52
           BCD Addition (cont.)
           BCD addition is therefore performed as
           follows
               1) Add the two BCD digits together using normal
               binary addition
               2) Check if correction is needed
                 • a) 4-bit sum is in range of 1010 to 1111
                 • b) carry out of MSB = 1
               3) If correction is required, add 0110 to 4-bit sum
               to get the correct result; BCD carry out = 1


204222 Digital System Design         Friday, July 30, 2010           53
           BCD Negative Number Representation
           Similar to binary negative number
           representation except r = 10.
               BCD sign-magnitude
                 • MSD (sign digit options)
                     – MSD = 0 (positive); not equal to 0 = negative
                     – MSD range of 0-4 positive; 5-9 negative
               BCD 10‟s complement
                 • -N  10r - N; 9‟s complement + 1
               BCD 9;s complement
                 • invert each BCD digit (09, 1  8, 2  7,3  6,
                   …7  2, 8  1, 9  0)

204222 Digital System Design            Friday, July 30, 2010          54
           Mechanical encoder with sequential
           sector numbering




                      At boundaries can get a code
                      different than either adjacent sector


204222 Digital System Design          Friday, July 30, 2010   56
           Gray Codes
           Grey codes are minimum change codes
               From one numeric representation to the next,
               only one bit changes
               Primary use is in numeric input encoding apps.
               where we expect non-random input values
               changes (I.e. value n to either n-1 or n+1)
                 • Milling machine table position
                 • Rotary shaft position




204222 Digital System Design         Friday, July 30, 2010      57
           Rotary Encoder
           an electro-mechanical device used to convert the
           angular position of a shaft or axle to a digital code,
           making it a sort of transducer. These devices are
           used in robotics, in top-of-the-line photographic
           lenses, in computer input devices (such as
           optomechanical mice and trackballs), and in rotating
           radar platforms.




204222 Digital System Design    Friday, July 30, 2010               58
              Standard binary encoding
           Sector      Contact 1      Contact 2       Contact 3          Angle
          1           off           off              off            0° to 45°
          2           off           off              on             45° to 90°
          3           off           on               off            90° to 135°
          4           off           on               on             135° to 180°
          5           on            off              off            180° to 225°
          6           on            off              on             225° to 270°
          7           on            on               off            270° to 315°
          8           on            on               on             315° to 360°

          the drawback that if the disc stops between two adjacent sectors, or the
          contacts are not perfectly aligned, it can be impossible to determine the
          angle of the shaft.



204222 Digital System Design               Friday, July 30, 2010                      59
           Example problem
               off- on- on (starting position)
               on- on- on (first, contact 1 switches on)
               on- on- off (next, contact 3 switches off)
               on- off- off (finally, contact 2 switches off)
           The sequence are 4,8,7,and then 5.
           The shaft appears to have jumped from sector 4 to sector 8,
           then gone backwards to sector 7, then backwards again to
           sector 5, which is where we expected to find it.
           In many situations, this behavior is undesirable and could
           cause the system to fail.
           For example, if the encoder were used in a robot arm, the
           controller would think that the arm was in the wrong
           position, and try to correct the error by turning it through
           180°, perhaps causing damage to the arm.

204222 Digital System Design               Friday, July 30, 2010          60
           Gray encoding
               Sector    Contact 1    Contact 2      Contact 3         Angle
               1         off          off           off             0° to 45°
               2         off          off           on              45° to 90°
               3         off          on            on              90° to 135°
               4         off          on            off             135° to 180°
               5         on           on            off             180° to 225°
               6         on           on            on              225° to 270°
               7         on           off           on              270° to 315°
               8         on           off           off             315° to 360°

        In the previous example, the transition from sector 4 to sector 5, like all other
        transitions, involves only one of the contacts changing its state from on to off or
        vice versa. This means that the sequence of incorrect codes shown in the previous
        illustration cannot happen here

204222 Digital System Design                Friday, July 30, 2010                             61
204222 Digital System Design   Friday, July 30, 2010   62
           Gray Code for decimal Digits
     Gray Code                 A Gray code changes by only 1 bit for
     0 = % 0000                adjacent values. This is also called a
     1 = % 0001                „thumbwheel‟ code because a thumbwheel
     2 = % 0011                for choosing a decimal digit can only
     3 = % 0010                change to an adjacent value (4 to 5 to 6,
     4 = % 0110                etc) with each click of the thumbwheel.
     5 = % 1110                This allows the binary output of the
     6 = % 1010                thumbwheel to only change one bit at a
     7 = % 1011                time; this can help reduce circuit
     8 = % 1001                complexity and also reduce signal noise.
     9 = % 1000



204222 Digital System Design        Friday, July 30, 2010                  63
           Gray Codes (cont.)
             Binary      Grey

             0000        0000
             0001        0001
             0010        0011
             0011        0010
             0100        0110
             0101        0111
             0110        0101
             0111        0100
             1000        1100
             1001        1101
             1010        1111
             1011        1110
             1100        1010
             1101        1011
             1110        1001
             1111        1000

204222 Digital System Design    Friday, July 30, 2010   64
           Binary Code to Gray Code Conversion
           Convert n bit Binary code into n bit Gray Code :
           Bit i of the Gray Code is obtained by comparing bits i and i+1 of the Binay
           Code :
           - Add extra 0 to the left of the Binary code
           - If bits i and i+1 in the Binary code are the same then bit i of the Gray
               code is 0 , else bit i is 1.
           Example
           Binary           10011

                   010011

           Gray Code : 11010
           Exercise : - Convert the Binary Code 11101 into Gray code
                  - Convert the Gray Code 1101 !!
           Answer : - The gray Code is : 10011
                 - The Binary Code is : 1001
204222 Digital System Design             Friday, July 30, 2010                           65
           Alphanumeric Representation
           Binary codes used to represent alphabetic
           and numeric characters
           Two most common are:
               ASCII, 7 bit code, 128 symbols
               EBCDIC, 8 bit code, 256 symbols
           Problems can arise when comparing symbol
           values (collation)
               Comparing „A‟ to „a‟ in ASCII system yields
               different results in an EBCDIC system.

204222 Digital System Design     Friday, July 30, 2010       66
           ASCII CODE
           The ASCII code (American Standard Code for
           Information Interchange) is a 7-bit code for Character
           data. Typically 8 bits are actually used with the 8th bit
           being zero or used for error detection (parity checking).
           8 bits = 1 Byte.

               „A‟ = % 01000001 = $41
                „&‟ = % 00100110 = $26

           7 bits can only represent 27 different values (128). This
           enough to represent the Latin alphabet (A-Z, a-z, 0-9,
           punctuation marks, some symbols like $), but what
           about other symbols or other languages?


204222 Digital System Design      Friday, July 30, 2010                67
           ASCII CODE
                                           c6c5c4

          c3c2c1c0     000     001   010      011       100   101   110   111

           0000        NUL     DLE   SP        0         @    P      ‘     p
           0001        SOH     DC1    !        1         A    Q     a      q

           0010        STX     DC2    ”        2         B    R     b      r
           0011        ETX     DC3    #        3         C    S     c      s
           0100        EQT     DC4    $        4         D    T     d      t
           0101        ENQ     NAK   %         5         E    U     e      u

           0110        ACK     SYN   &         6         F    V      f     v
           0111        BEL     ETB    ’        7         G    W     g     w
           1000        BS      CAN    (        8         H    X     h      x
           1001        HT      EM     )        9         I    Y      I     y
           1010        LF      SUB    *        :         J    Z      j     z
           1011        VT      ESC   +         ;         K     [    k      {
           1100        FF      FS     ,        <         L     \     l     |
           1101        CR      GS     -        =         M     ]    m      }
           1110        S0      RS     .        >         N    ^     n      ~
           1111        S1      US     /        ?         O     _    o     DEL

204222 Digital System Design         Friday, July 30, 2010                      68
           UNICODE
           UNICODE is a 16-bit code for representing alphanumeric
           data. With 16 bits, can represent 216 or 65536 different
           symbols.
           16 bits = 2 Bytes per character.

           $0041-005A     A-Z
           $0061-4007A     a-z
           Some other alphabet/symbol ranges
           $3400-3d2d     Korean Hangul Symbols
           $3040-318F     Hiranga, Katakana, Bopomofo, Hangul
           $4E00-9FFF     Han (Chinese, Japenese, Korean)

           UNICODE used by Web browsers, Java, most software these
           days.

204222 Digital System Design       Friday, July 30, 2010              69
           Error Detection and Error Correction
           Code
           the transmission of data over a channel may
           result in error due to interference, noise, etc.
           hence, data will be corrupted
           some schemes have been devised to detect
           and correct the error
           it is usually accomplished by inserting extra
           bits in the data being transmitted



204222 Digital System Design   Friday, July 30, 2010          70
           Single error detection with parity
         simplest method for error detection
         insert an additional parity bit for transmission
         even parity may be generated by
             Pe  b3  b2  b1  b0 for a 4-bit data
       e.g. for a 4-bit data 0110
                Pe  0  1  1  0  0
           odd parity may be obtained by
                Po  1  Pe
       e.g. Po  1  0  1  1  0  1


204222 Digital System Design             Friday, July 30, 2010   71
           Parity Bit
           ASCII code may have an extra bit appended to
           detect data transmission errors
               P   =   0   if the number of 1s in the character is even, else
               P   =   1   (even parity)
               P   =   0   if the number of 1s in the character is odd, else
               P   =   1   (odd parity)
           If any single bit changes, parity will be wrong at
           receive end
                                         Even parity        Odd parity

               ASCII A = 1000001 01000001                   11000001
               ASCII T = 1010100 11010100                   01010100

204222 Digital System Design              Friday, July 30, 2010                 72
           Vertical and longitudinal parity bits
          used for block data transmission
                                   Pe                data

                               /   0    1   0    1    1     0   0   1

                               |   0    0   1    0    0     1   0   0

                               |   0    1   1    1    0     0   0   1

                   horizontal /    0    1   0    0    1     1   1   0

                   parity bits \   1    0   1    0    1     0   1   0

                               |   1    0   0    1    0     1   1   0

                               |   0    1   0    0    1     1   0   1

                               \   1    1   1    0    0     1   1   1

                                   1    1   0    1    0     1   0   0   vertical

                                                                        parity bit

       • a single error change the parity of the corresponding row and column thus
       the error can be located and corrected
       • a double error occur in the same row, or same column or in different
       column and row may be detected, but not correctable
204222 Digital System Design                    Friday, July 30, 2010                73
           Hamming Code
           only for single error correction
           m : no. of information bit
           p : no. of parity bit
           the relationship is governed by:
                       2  m  p 1
                         p


           e.g.                m = 5,       p=4


204222 Digital System Design       Friday, July 30, 2010   74
           Hamming Code
           Consider the number of information bit = m
           and the number of parity bit = p
            2p, range is 0 ~ (2p-1)
           0 indicate no error
           other combinations (2p-1) indicate which bit is
           in error (total no. of bits = m+p)
           Therefore, 2p-1  m+p or 2p  m+p+1



204222 Digital System Design   Friday, July 30, 2010         75
           Rules for assigning parity bit
       1. convert the no. of position in binary no.
       2. locate the bit whose location in binary no.
          having a 1 in the same position as the parity
          bit
       3. find the parity values (depends on odd or
          even parity is used) including the parity bit
          itself



204222 Digital System Design   Friday, July 30, 2010      76
           bit allocation: parity bits are located in
           position which is power of 2, i.e. 1,2,4,8,...
           and in ascending order
           for m=4, we have the following pattern:

                          P1 P2 M1 P3 M2 M3 M4




204222 Digital System Design      Friday, July 30, 2010     77
           Manchester Coding
           Manchester codes always have a transition at
           the middle of each bit period, and depending
           on the state of the signal, may have a
           transition at the beginning of the period as
           well.




204222 Digital System Design   Friday, July 30, 2010      78
           Non Return to Zero Code
           a non-return-to-zero (NRZ) line code is a binary code in
           which "1s" are represented by one significant condition and
           "0s" are represented by another, with no neutral or rest
           condition, such as a zero amplitude in pulse amplitude
           modulation (PAM), zero phase shift in phase-shift keying (PSK),
           or mid-frequency in frequency-shift keying (FSK).
           For example, NRZ is used in the common RS-232 (TIA-232)
           serial protocol, where start and stop bits are used to indicate
           the beginning and end of a frame.




204222 Digital System Design        Friday, July 30, 2010                    79
           Return to Zero Code
           Return-to-zero (RZ) describes a line code used in
           telecommunications signals in which the signal drops
           (returns) to zero between each pulse. This takes place even
           if a number of consecutive zeros or ones occur in the signal.
           This means that a separate clock does not need to be sent
           alongside the signal. The signal is self-clocking.




204222 Digital System Design       Friday, July 30, 2010                   80
           Other Information Representation
           ALL information must be encoded before we
           can design circuits to process it
           You can assign any code to any information
               E.G. 00 - north, 01 - east, 11 - south, 10 - west
           If the information goes somewhere else, the
           user has to have access to your definition
           Standards are best if available
               Already published and easily available
               Allows your system to work with many others
204222 Digital System Design      Friday, July 30, 2010            81

								
To top