EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

66
EE345 - Micro- Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija

Transcript of EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

Page 1: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

EE345 - Micro-ControllersDigital Systems and Binary Numbers

Prof. Ahmad Abu-El-Haija

Page 2: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023Digital System Design 2

Acknowledgement

This presentation is a modified version of lecture notes prepared by Dr. Pradondet Nilagupta, Kasetsart University. The latter is also a modified version based upon presentations by Prof. Maciej Ciesielski and Prof. Tilman Wolf, University of Massachusetts Amherst, and original slides from the publisher.

Page 3: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 3

Digital Systems

Digital vs. Analog Waveforms

Analog: values vary over a broad range continuously

Digital: only assumes discrete values

+5

V

–5

1 0 1

T ime

+5

V

–5

T ime

Digital Hardware Systems

Page 4: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 4

Digital Circuits

Digital circuit can be represented by a black-box with inputs on one side, and outputs on the other.

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

In contrast, analog circuits use continuous signals.

Digital circuit

inputs outputs: :

High

Low

Page 5: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 5

the preconditions must be true to imply the conclusion

IF the garage door is openAND the car is runningTHEN the car can be backed out of the garage

IF the garage door is openAND the car is runningTHEN the car can be backed out of the garage

both the door must be open and the carrunning before I canback out

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

Page 6: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 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)

Page 7: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 7

Binary Representation of Information

Information divided into groups of symbols 26 English letters 28 Arabic letters 10 decimal digits 22 Arab countries

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

Page 8: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 8

Digital Systems Digital systems operate on discrete elements of

information Numbers (e.g., pocket calculator) -> “digits” -> “digital” Letters (e.g., word processor) Pictures (e.g., digital cameras)

For a digital systems to operate on a continuous data, it needs to quantize (digitize) that data first Covert data into digital representation

Topics: How are numbers represented in digital systems How computer performs basic arithmetic operations

Page 9: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 9

Numbers

Common numbering system is “base10” Why?

Numbers in base 10 Ten different digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Number is represented by a sequence of digits: an an-1 … a1 a0

Value of number is: an×10n+an-1×10n-1+…+a1×101+a0×100

Positional notation General equation: May contain a decimal point Negative index for digits after decimal point

Examples 1234.56 – if ambiguous, write (1234.56)10

Leading zeros cause no problems: 00001234.56

i

iia 10

Page 10: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 10

Number Systems

General form, with base r :

Base r is also called radix In decimal system r = 10; in binary r = 2

Coefficients in positional notation are: 0,1,…, r-1.

What is the range of values of an n-bit number in radix r ? Minimum value: 0 Maximum value: rn-1 Number of different values: rn

i

ii ra

Page 11: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 11

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.

Page 12: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 12

Positional Integer Number Values

Given a digit series of

The full expression for the represented value is

. )pointRadix (01231... AAAAAn

1

0

0

0

1

1

2

2

3

3

1

1...

ni

i

i

i

n

n

rA

rArArArArAor

Page 13: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 13

Positional Fractional Number Values

Given a digit series of

The full expression for the represented value is

AAAAA m...

4321 point)(Radix

mi

i

i

i

m

m

rA

rArArArArAor

1

4

4

3

3

2

2

1

1...

Page 14: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 14

Binary Numbers

Base 2 number use only two digits: 0, 1 Why?

Digits need to be represented in a system Electronic systems typically use voltage levels Representing 10 different voltages

reliably is difficult Binary decision is much easier (On, Off)

Binary representation is ideal Minimal number of digits Easily represented in voltages

Page 15: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 15

Examples for Binary Numbers

What value is represented by (01001)2? Leading zero makes no difference (1001)2 translates into 1×23+0×22+0×21+1×20=8+0+0+1=(9)10

Same process for numbers with decimal point What is the value of (1001.1001)2?

(1001.1001)2 = 1×23+0×22+0×21+1×20+1×2-1+0×2-2+0×2-3+1×2-4=8+0+0+1+1/2+0+0+1/16=(9.5625)10

Important: it’s NOT (9.9)10!

Can you count binary? How far can you count with 10 fingers?

Page 16: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 16

Binary Number Terminology

Base is also called “radix” Binary numbers are made of binary digits (bits) Groups of four bits are called “nibbles”

E.g., (1101)2

Groups of eight bits are called “bytes” E.g., (01001101)2

What is the range of values of an n-bit binary number? Minimum value: 0 Maximum value: 2n-1 Number of different values: 2n

Powers of 2 are important

Page 17: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 17

Powers of 2

You must memorize all powers of 2 up to 216!

Other important powers of 2:

Trick to simplify estimation: 210=1024≈1000=103

Example: 232=4×230≈4×109=4 billion

Prefixes: kilo (103 ≈ 210), Mega (106 ≈ 220), Giga (109 ≈ 230), Tera (1012 ≈ 240), … Computer systems are typically based on powers of 2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536

8 16 24 32 64

256 65536 16777216 4294967296 18446744073709551616

Page 18: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 18

Other Number Systems

Octal number system Digits 0, 1, … 7 Aggregates 3 digits of binary system (i.e., 3-bit number) E.g., (57)8 = (47)10

Hexadecimal number system 16 digits require 6 new digit symbols: 0, … 9, A, B, C, D, E, F Aggregates 4 digits of binary system (1 nibble or half byte) E.g., (1F)16 = (31)10

Conversion between system:

decimal (base 10)

octal (base 8)

binary (base 2)

hexadecimal (base16)

Page 19: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 19

Number Base Conversions

Conversion from base r to decimal Expansion to power series and addition of terms

Conversion from decimal to base r Divide number and successive quotients by r Sequence of remainders is base r number

Example: convert (41)10 to binary (r=2) 41 = 20 2 + 1 20 = 10 2 + 0 10 = 5 2 + 0 5 = 2 2 + 1 2 = 1 2 + 0 1 = 0 2 + 1

i

ii ra

a0 = 1a1 = 0a2 = 0a3 = 1a4 = 0a5 = 1

(101001)2

Page 20: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 20

Number Base Conversions

Conversion to/from octal and hexadecimal Easier if done via binary 3 or 4 bit sequences correspond to digit

Example: (2414)10=(100101101110)2

(4 5 5 6)8

(9 6 E)16

Page 21: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 21

Number Systems - Summary Base r numbers

Binary Octal Hexadecimal

Conversion between number systems Summation of power series Division with remainders

Powers of 2 Computer systems based on powers of 2 kilo = 1024 Mega = 1024 × 1024 Giga = 1024 × 1024 × 1024

Next: Computer arithmetic Signs, complements

Page 22: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 22

Computer Arithmetic

Computer arithmetic Addition, subtraction Multiplication Signed numbers Complements

Page 23: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 23

Binary Addition Binary addition works like “normal” addition

Stay within {0,1} Carries as usual

Example:

111101+ 10111

1010100

Carry: 111111

Addition of multiple numbers possible Carry gets a bit more difficult

augend: addend:

Page 24: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 24

Binary Subtraction

Subtraction same as “normal” subtraction Borrows as usual

Example:

111101- 10111

0 1 1 0 0 1

0 10

– –

100 –– minuend:

subtrahend:

Page 25: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 25

Binary Multiplication

Binary multiplication Same as “normal” multiplication Multiplication a lot easier in binary domain

Example: 111101× 1010 000000

111101 000000

111101

1001100010

multiplicand multiplier

Page 26: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 26

Signed Numbers

How are signed numbers handled in base 10? Plus or minus sign placed in front of number

Can we do that for binary numbers? Sign needs to be represented in digital system Only choice are ‘0’ and ‘1’

‘0’ indicates ‘+’ ‘1’ indicates ‘–’

Examples on five-bit numbers: 01101 11101 00000 10000 + 13 – 13 + 0 – 0

Signed Magnitude representation

Page 27: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 27

Arithmetic with Signed Magnitude

Addition example 1: Plus signs are leading zeros -> no problem

Augend: 01001 (9)10

Addend: + 00010 + (2)10

01011 (11)10

Addition example 2: What happens with negative numbers?

01001 (9)10

+ 10010 + (–2)10

11011 ??? (7)10

Problem: negative numbers Sign can turn addition into subtraction

Page 28: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 28

Signed Magnitude Representation

Arithmetic with signed magnitude is difficult Two representations of zero Different cases for addition and subtraction

1000

00010000

0100

0111

0010

0011

0101

0110

1001

1010

1011

1100

1101

1110

1111

+0+1

+2

+3

+4

+5

+6

-0 +7

-6-7

-5

-4

-3

-2-1

Page 29: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 29

Complements

“Complements” allow easier arithmetic Representation of negative numbers a bit more involved

Two types of complements Radix complement: r’ s complement

Decimal: 10’s complement Binary: 2’s complement

Diminished radix complement: (r–1)’s complement Decimal: 9’s complement Binary: 1’s complement

Page 30: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 30

Diminished Radix Complement

Given a number N in base r having n digits, the (r–1)’s complement of N is defined as:

(r n –1) – N Example for 6-digit decimal numbers:

9’s complement is (r n–1)–N = (106–1)–N = 999999–N 9’s complement of 546700 is 999999–546700 = 453299

Example for 7-digit binary numbers: 1’s complement is (r n –1) – N = (27–1)–N = 1111111–N 1’s complement of 1011000 is 1111111–1011000 = 0100111

Observation: Subtraction from (rn–1) will never require a borrow Diminished radix complement can be computed digit-by-digit For binary: 1 – 0 = 1 and 1 – 1 = 0

Flips 0’s to 1’s and 1’s to 0’ (bit complementation)

Page 31: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 31

One’s Complement Representation

1’s complement is simple to compute Bit complementation Still, two representations of zero

1000

00010000

0100

0111

0010

0011

0101

0110

1001

1010

1011

1100

1101

1110

1111

+0 +1+2

+3

+4

+5

+6

-7 +7

-1-0

-2

-3

-4

-5-6

Page 32: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 32

Radix Complement The r’s complement of an n-digit number N in base r is defined as

r n – N for N ≠ 0 and 0 for N = 0 Radix complement is diminished radix complement + 1:

(r n –1) – N +1 = r n – N

Example for 6-digit decimal numbers: 10’s complement is r n – N = 106–N = 1000000–N 10’s complement of 546700 is 1000000–546700= 453300 Rule: Leave least significant 0’s unchanged, subtract first nonzero least

significant digit from 10, subtract all higher significant digits from 9.

Example for 7-digit binary numbers: 2’s complement is r n–N = 27–N = 10000000–N 2’s complement of 1011000 is 10000000–1011000 = 0101000 Rule: “Leave least significant 0’s and first 1 unchanged, replace 1’s with

0’s and 0’s with 1’s in all higher significant digits.”

Page 33: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 33

Two’s Complement Representation

Arithmetic with 2’s complement is most efficient Bit complementation + 1 Single representation of zero!

1000

00010000

0100

0111

0010

0011

0101

0110

1001

1010

1011

1100

1101

1110

1111

0 +1+2

+3

+4

+5

+6

-8 +7

-2-1

-3

-4

-5

-6-7

Page 34: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 34

Complements - Summary

Complement of complement is original number Diminished radix complement:

(r n–1)–((r n–1)–N) = r n–1–r n+1 + N = N Radix complement:

r n–(r n–N) = r n–r n+ N = N

Representation of zero Radix complement: 0 Diminished radix complement: r n –1 and it’s complement 0

Radix points Remove radix point Compute complement Put radix point back at same relative position

Page 35: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 35

Signed Binary Numbers Comparison ( n=4 )

-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

Decimal Signed Magnitude signed One’s Complement

Signed Two’s Complement

Page 36: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 36

Subtraction with Complements

Subtraction M – N Add minuend M to r’s complement of subtrahend N M – N = M + (rn – N) = M – N + r n

What about the additional r n ? If M N

Then M – N + r n r n

With n bits, only numbers < r n can be expressed n+1st digit is ignored (drop the “carry out” bit)

If M < N Then M – N + r n = r n – (N – M) = r ’s complement of (N-M) r ’s complement signifies negative number So, – (N – M) = M – N

Page 37: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 37

Arithmetic with Radix Complement

Subtraction works as addition of complement

Addition/subtraction of signed numbers Negative numbers are expressed as r ’s complement Simple addition yields correct result No need to distinguish different cases

Addition/subtraction of unsigned numbers Can be performed on same hardware as signed numbers

Most modern digital systems use 2’s complement

Page 38: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 38

Two’s Complement OverflowConsider two 8-bit 2’s complement numbers. We can represent the signed integers -128 to +127 using this representation.

What if we do (+1) + (+127) = +128. The number +128 is OUT of the RANGE that we can represent with 8 bits. What happens when we do the binary addition?

+127 = 7F

+ 1 = 01------------------- 128 ≠ 80 (this is actually -128 as a two’s complement number!!! - the wrong answer!!!)

How do we know if overflow occurred? Added two POSITIVE numbers, and got a NEGATIVE result.

Page 39: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 39

Detecting Two’s Complement Overflow

Two’s complement overflow occurs when we:

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

We CANNOT get two’s complement overflow if we 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.

Page 40: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 40

Weighted and Unweighted Codes

Most numeric number representations are in a class known as “Weighted Codes” where

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

1-r

0iii w b Value

Page 41: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 41

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 is really decimal with different symbols)

BCD add/subtract circuits are complex

Page 42: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 42

Other Decimal Codes

Decimal digit

BCD 8421 2421 Excess-3 8 4 -2 -1

0 0000 0000 0011 0000

1 0001 0001 0100 0111

2 0010 0010 0101 0110

3 0011 0011 0110 0101

4 0100 0100 0111 0100

5 0101 1011 1000 1011

6 0110 1100 1001 1010

7 0111 1101 1010 1001

8 1000 1110 1011 1000

9 1001 1111 1100 1111

Weighted codes Unweighted code

The 8421 or natural BCD code is the most common BCD code in use

Page 43: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 43

BCD Addition

Case 1: Case 2:

Case 3:

0001 1 0101 5(0) 0110 (0) 6

0110 6 0101 5(0) 1011 (1) 1

1000 8 1001 9(1) 0001 (1) 7

WRONG!

Note that for cases 2 and 3, adding a factor of 6 (0110) gives us the correct result.

Page 44: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 44

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

Page 45: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 45

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 (09, 1 8, 2 7,3 6, …7 2, 8 1, 9 0)

Page 46: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 47

Decimal Arithmetic

Everything needs to be 4-bit aligned ‘+’ represented by 0 (=‘0000’) ‘–’ represented by 9 (=‘1001’)

Signed magnitude representation or complements Signed magnitude hardly used 10’s complement most common

Example: 375 + (–240) Negative numbers represented by 10’s complement 10’s complement of 240 is 104 – 240 = 9760 Addition of all digits and

discard of end carry:

Sign of result automatically correct

0 375 + 9 760

0 135

Page 47: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 48

Gray Codes

Gray codes are minimum change codes From one numeric

representation to the next, only one bit changes

Primary use is in numeric input encoding applications where we expect non-random input values changes (i.e. value n to either n-1 or n+1)

Gray Code Dec. Equiv.

0000 00001 10011 20010 3

0110 4

0111 50101 60100 71100 81101 91111 101110 111010 121011 131001 141000 15

Page 48: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 49

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.

Page 49: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 50

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 (128) different values. This is 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 (e.g., Arabic)?

Page 50: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 51

ASCII CODE

b6b5b4

b3b2b1b0000 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

Page 51: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 52

UNICODE

UNICODE is a 32-bit code for representing alphanumeric data. Most often, only 16 bits are used. With 16 bits, we can represent 216 or 65,536 different symbols.16 bits = 2 bytes per character.

$0041-005A A-Z$0061-4007A a-z

In UNICODE, Arabic characters are coded by the following: Arabic basic characters: $0600 – $06FF Arabic supplement: $0750 – $077F Arabic presentation forms: $FB50 – $FDFF & $FE70 –

$FEFF

UNICODE is used by Web browsers, Java, and most software

Page 52: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 53

Error Detection and Error Correction Codes

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

Page 53: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 54

Single error detection with parity

simplest method for error detection insert an additional parity bit for transmission even parity may be generated by

for a 4-bit datae.g. for a 4-bit data 0110

odd parity may be obtained by

e.g.

0123 bbbbPe

00110 eP

ePP 1o

101101o P

Page 54: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 55

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 endEven parity Odd parity

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

Page 55: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 56

Parity Code Example

Concatenate a parity bit to the ASCII code for the characters 0, X, and = to produce both odd-parity and even-parity codes.

Character ASCII Odd-Parity ASCII

Even-Parity ASCII

0 0110000 10110000 00110000

X 1011000 01011000 11011000

= 0111100 10111100 00111100

Page 56: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 57

Binary Storage and Registers

How is information stored in a digital system? Bits are stored in “binary cells” Binary cell can have two stable states: ‘0’ and ‘1’

Binary cells are grouped into registers n cells make up n-bit register

Size of registers is typically predefined Simple microcontroller: 8 bits = 1 byte Pentium: 32 bits = 4 bytes Mac G5: 64 bits = 8 bytes

Digital system can usually process entire registers “Register transfer” operation specify processing

Page 57: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 58

Binary Data Storage• Binary cells store individual bits of data

• Multiple cells form a register.

• Data in registers can indicate different values

• Hex (decimal)

• BCD

• ASCII

Binary Cell

0 0 1 0 1 0 1 1

Page 58: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 59

Register Transfer

Data can move from register to register. Digital logic used to process data We will learn to design this logic

Register A Register B

Register C

Digital Logic Circuits

Page 59: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 60

Register Example

Data input at keyboard Shifted into place Stored in memory

Page 60: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 61

Register Transfer Operations

We need processing We need storage We need communication

You will learn to use and

design these components.

Designing digital logic is focus of rest of course

Page 61: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 62

Binary Logic

Binary logic uses two possible values ‘1’ and ‘0’ ‘yes’ and ‘no’ ‘true’ and ‘false’ Can be represented by variables: A, B, C, x, y, z, …

Logic functions modify input values What is the minimum number of inputs?

1 input -> uninteresting, inverter or wire only 2 inputs -> basic logic functions

What are possible logic functions? NOT, AND, OR Others can be derived from those (NAND, XOR, etc.)

Page 62: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 63

NOT Function

Complement operation Single input Inverts value of input Symbolized by prime x’ or overbar

Truth table Input combinations on the left Output of function on the right

Graphic symbol NOT gate Little circle indicates inversion

x x'

0 1

1 0

xinput output

Page 63: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 64

AND Function

Operation to check if two conditions are met Two inputs Output = 1 if and only if both inputs are 1 Symbolized by dot or absence of operator

x·y or xy

Truth table Needs to consider 22 = 4 input combinations

Graphic symbol AND gate 111

001

010

000

zyx

inputs output

Page 64: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 65

OR Function

Operation to check if at least one condition is met Two inputs Output = 1 if any one or both inputs are 1 Symbolized by “plus” sign: x + y

Truth table

Graphic symbol OR gate

x y z

0 0 0

0 1 1

1 0 1

1 1 1

Page 65: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 66

Comparison of Functions

Timing diagrams (waveforms) Horizontal axis is time ( t ) Vertical axis shows signals, each with two different voltage levels

t

Page 66: EE345 - Micro-Controllers Digital Systems and Binary Numbers Prof. Ahmad Abu-El-Haija.

April 21, 2023EE345 – Micro-Controllers 67

Multiple Inputs

Two inputs might not be enough 3-input AND gate:

4-input OR gate:

What are the rules for aggregating functions? Boolean algebra