04 chapter03 02_numbers_systems_student_version_fa14

48
Chapter 03 Sect. 3-1 to 3-3 Supplement and Reinforcement of Number Systems

Upload: john-todora

Post on 24-Jun-2015

391 views

Category:

Engineering


2 download

DESCRIPTION

Chpater

TRANSCRIPT

Page 1: 04 chapter03 02_numbers_systems_student_version_fa14

Chapter 03 Sect. 3-1 to 3-3

Supplement and Reinforcement of Number Systems

Page 2: 04 chapter03 02_numbers_systems_student_version_fa14

Why Do I Have To Learn About Number Systems

If you are taking a trip to a foreign country you would probably want to learn some of the language that is spoken in that country. After all, it would be nice to be able to order a nice dinner or ask for the location of a restroom.

Working with computers and PLCs is like visiting a foreign country. The language spoken in these countries is numbers, therefore it is a good idea to learn some of the language.

Page 3: 04 chapter03 02_numbers_systems_student_version_fa14

Number Systems

Decimal Binary Octal Hexadecimal

These are the number systems we will be studying

Learn them well, because they WILL keep coming back to haunt you.

Page 4: 04 chapter03 02_numbers_systems_student_version_fa14

Decimal System

The decimal number system is the most common numbering system. It’s the number system we use everyday.

Number systems have a base or radix. The radix determines the total number of different symbols, or digits used by the system.

Decimal is a Base-10 numbering system, therefore there are 10-digits starting with the number zero.

Valid numbers in the decimal system are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Base-10 numbers can be written; as an example: 410 or 4d

Page 5: 04 chapter03 02_numbers_systems_student_version_fa14

Decimal System

Each digit in a number system has a weight value assigned to it.

Decimal is a Base-10 number system, therefore the weight of each digit of a number is 10 raised to the power of the digit position.

The first digit on the right is position zero, the next digit to the left is position one, to the left again is position two, etc.

The value of the decimal number is calculated by multiplying the digit value of each position by10 to the power of that position number and summing the products.

What was just said???

Page 6: 04 chapter03 02_numbers_systems_student_version_fa14

Decimal System

5 3 1 93 2 1 0

9 * 100 9 * 1 = 91 * 101 1 * 10 = 103 * 102 3 * 100 = 300

5 * 103 5 * 1000 = 5000531910Sum of the products

The base (radix) of the number system. For Base-10 it is normally not shown. It is shown here as an example.

Position Number

Base or Radix is 10

Decimal Number

Page 7: 04 chapter03 02_numbers_systems_student_version_fa14

Binary System

The binary number system is used by computers, PLCs, PACs and any other digital and µP controlled device.

Bi means two, therefore the valid numbers in the binary system are: 0 and 1.

One digit of a binary number is called a Bit (BOOL data type). A group of 4-Bits is called a Nibble (term is not often used). A group of 8-Bits is called a Byte (SINT or BYTE data type). A group of 16-Bits is called a Word (WORD or Integer (INT) data type. A group of 32-Bits is called a Double Word or DWORD. (DWORD or

DINT data type). A group of 64-Bits is called a Quad Word or QWORD. A group of 128-Bits is called a Double Quad Word or DQWORD The binary number system is used in computers and PLCs because bits

can be represented as voltage levels within the computer. A zero = 0VDC or GND, a one = +VDC.

Page 8: 04 chapter03 02_numbers_systems_student_version_fa14

Binary System

Each bit in the binary system has a weight assigned to it. Binary is a Base-2 number system, therefore the weight

of each bit is a power of 2. (2 raised to the power of the bit position)

Base-2 numbers can be written, as and example: 01012 or 0101b

The bit on the right is position zero, the next bit to the left is position one, to the left again is position two, etc.

The weight value of the bit in each position is the base of the number system raised to the power of the position.

The value of the binary number is then calculated by calculating the sum of products of the bit position weight value(s).

Page 9: 04 chapter03 02_numbers_systems_student_version_fa14

Binary System

1 0 0 13 2 1 0

1 * 20 1 * 1 = 10 * 21 0 * 2 = 00 * 22 0 * 4 = 01 * 23 1 * 8 = 8

20110Sum of the products

1 0 06 5 4

17

0 * 24 0 * 16 = 00 * 25 0 * 32 = 01 * 26 1 * 64 = 641 * 27 1 * 128 = 128

2

The base (radix) is 2

Position Number

Page 10: 04 chapter03 02_numbers_systems_student_version_fa14

Binary System

Two voltage levels are easy to distinguish. These two voltage levels produce a square wave or digital signal with the voltage levels of the square wave being 0 VDC or GND and +VDC.

The voltage levels can be related to the binary digits 1 and 0.

Page 11: 04 chapter03 02_numbers_systems_student_version_fa14

The Binary Word (16-bit)

A single binary digit is a bit (BOOL). Four bits is a nibble. Eight bits is a byte (Byte or SINT). Sixteen bits is a word (WORD or INT) Thirty-two bits is a double word (DWORD or DINT)

1 1 1 0 1 1 0 10 1 0 1 1 0 0 0

Byte

Word

Bits

MSB – Most Significant Bit LSB – Least Significant Bit

Page 12: 04 chapter03 02_numbers_systems_student_version_fa14

Binary System

The binary system uses two digits, 1 and 0. Each digit or bit can only go through two changes and then a 1 is carried to the position immediately to the left.

Numbers expressed in the binary system require many more digits than in the decimal system. Most PLCs use 16-digits, better known as 16-bits to represent decimal numbers. Most PACs use 32-bits to represent decimal numbers.

The table shows the decimal numbers along with the binary equivalent for the first 10 decimal digits.

Decimal 4-Bit Binary

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

Page 13: 04 chapter03 02_numbers_systems_student_version_fa14

Octal Number System

Octal is a Base-8 number system. Base-8 numbers can be written, as an example:

48 or 4o Some PLCs use the octal number system for I/O

addressing and inputting parameter information to internal instruction. (Automation Direct is one of these manufacturers)

There are no number 8’s or number 9’s in the octal number system.

Base-8 means that there are eight valid numbers. Starting with zero they are:

0, 1, 2, 3, 4, 5, 6, 7

Page 14: 04 chapter03 02_numbers_systems_student_version_fa14

Hexadecimal Number System

Hexadecimal, simply referred to as Hex, is a Base-16 number system.

Base-16 numbers can be written, as an example: 416 or 4h

Some PLCs/PACs use the hexadecimal number system for inputting information to internal instructions.

Base-16 means that there are 16-valid numbers. Starting at zero they are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, FWhere:

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Why are letters used for the numbers 10-15?

Page 15: 04 chapter03 02_numbers_systems_student_version_fa14

Number Weighting

1 1 1 0 1 1 0 10 1 0 1 1 0 0 0

215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20

Binary Weighting

Octal Weighting Hexadecimal Weighting

5,3348

83 82 81 80

5C8A16

163 162 161 160

Page 16: 04 chapter03 02_numbers_systems_student_version_fa14

Number Conversions

Decimal to: Binary Octal Hexadecimal BCD

Binary to: Decimal Octal Hexadecimal BCD

Octal to: Decimal Binary Hexadecimal BCD

Hexadecimal to: Decimal Binary Octal BCD

To be fluent in PLCs/PACs we need to learn how toconvert numbers from one system to another.

We will be learning to convert -

Page 17: 04 chapter03 02_numbers_systems_student_version_fa14

Mathematically Converting from Decimal To Binary

Convert 20010 to binary Divide the decimal number to be converted by the

base of the number system you wish to convert to. After each division, write down the remainders until the quotient (result of the division) is equal to zero

Page 18: 04 chapter03 02_numbers_systems_student_version_fa14

Mathematically Converting from Decimal To Octal

Convert 20010 to octal

Divide the decimal number to be converted by the base of the number system you wish to convert to. After each division, write down the remainders. Keep dividing the resultant quotient by the base of the system being convert to until the quotient (the result of the division) is equal to zero

3 1 0Base

810 310200 8

Base of number system converting to

8

200

8

25

Decimal number being converted

Quotients

Remainders

25

01

3

8

3

0

3

Page 19: 04 chapter03 02_numbers_systems_student_version_fa14

C 8

Base of number system converting to

Base

1610 8200 C

16

200

16

12

Decimal number being converted

Quotients

Remainders

12

812 = C

0

16

Mathematically Converting from Decimal To Hex

Convert 20010 to hexadecimal

Divide the decimal number to be converted by the base of the number system you wish to convert to. After each division, write down the remainders. Keep dividing the resultant quotient by the base of the system being converted to until the quotient (the result of the division) is equal to zero

Page 20: 04 chapter03 02_numbers_systems_student_version_fa14

Binary Weighting Conversion Helper

1 1 1 0 1 1 0 10 1 0 1 1 0 0 0

32,7

6816

,384

8,19

24,

096

2,04

81,

024

256

512

64128

32 16 48 2 1

The mathematical method is fine, but is prone to math errors.

There is a much easier method that only requires addition and subtraction. Simply convert all non-binary numbers to

binary. Once a number is in binary it is easy to convert to any other number system.

The following conversion helper can then be used.

Page 21: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Binary To Decimal

Convert 1100 10002 to decimal

To convert a binary number to decimal: Write down the

weight of all the bit position where there is a 1.

Calculate the sum of the products of the weight values.

The resultant sum is equivalent to the Decimal number.

To Decimal

1 1 0 0 1 0 0 020 * 0 = 021 * 0 = 022 * 0 = 023 * 1 = 824 * 0 = 025 * 0 = 026 * 1 = 6427 * 1 = + 128 200

Page 22: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Binary To Octal

Convert 1100 10002 to Octal. Converting from binary to other number systems is very

simple. To convert to octal:

How many bits of binary are required to represent an octal digit? Remember, the octal numbers are 0 – 7.

Answer → It take 3-bits to represent the numbers 0 – 7. Starting at the right side of the binary number and working to the

left, group the bits in groups of three. Then perform 3-bit binary to decimal conversions on each group.

The result is the octal equivalent. To Octal

0 1 1 0 0 1 0 0 0

3108Octal Equivalent

Binary to Convert

Page 23: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Binary To Hexadecimal

Convert 1100 10002 to Hexadecimal. Converting from binary to other number systems is very

simple. To convert to hexadecimal:

How many bits of binary does it take to represent a hexadecimal digit? Remember, the hexadecimal numbers are 0 – 9, A - F.

Answer → It take 4-Bits to represent the numbers 0 – 9, A - F. Starting at the right side of the binary number and working to the

left, group the bits in groups of four. Then perform 4-bit binary to decimal conversions on each group.

The result is the hexadecimal equivalent.To Hexadecimal

1 1 0 0 1 0 0 0

C816Hexadecimal Equivalent

Binary to Convert

12 8

Page 24: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Decimal to Binary (non-mathematic method)

Refer to the Binary Weighting Conversion Helper. Convert 12,578 to binary:

Starting at the MSB of a binary word, look for a weight value that is less than or equal to the decimal number being converted.

If the weight value is greater than the decimal number being converted place a zero in that position and than proceed to the next bit to the right.

If the weight value is less than or equal to the decimal number being converted place a 1 in that position, then subtract the weight value from the decimal number being converted.

Using the difference, continue to the right in the binary word placing 0’s in the positions that are greater than the decimal number being converted and 1’s in the position that are less than or equal to the binary number being converted, subtracting that weight value and moving to the right using the difference.

Continue until the difference is equal to zero, then place zero’s in any remaining bit positions.

Page 25: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Decimal to Binary (non-mathematic method)

Convert 12,578 to binary:

Position 15 (Bit-15) (MSB): 32,768 > 12,578 – Place a zero in that position Position 14 (Bit-14): 16,384 > 12,578 – Place a zero in that position Position 13 (Bit-13): 8,192 < 12,578 – Subtract; 12,578 – 8,192 = 4,386; place a 1 in this position Position 12 (Bit-12): 4,096 < 4,386 – Subtract; 4,386 – 4,096 = 290; place a 1 in this position Positions 11, 10 & 9 (Bits-11, 10 & 9) are greater than 290; place 0’s in these positions Position 8 (Bit-8): 256 < 290 – Subtract; 290 – 256 = 34; place a 1 in this position Positions 7 & 6 (Bits-7 & 6) are greater than 34; place 0’s in these positions Position 5 (Bit-5): 32 < 34 – Subtract; 34 – 32 = 2; place a 1 in this position Positions 4, 3, & 2 (Bits-4, 3 & 2) are greater than 2; place 0’s in these positions Position 1 (Bit-1): 2 = 2 – Subtract; 2 – 2 = 0; place a 1 in this position. The difference is equal to zero therefore the conversion is finished. Place 0’s in any remaining bit positions.

0 0 0 0 0 0 0 00 0 0 0 0 0 0 0

32,7

6816

,384

8,19

24,

096

2,04

81,

024

256

512

64128

32 16 48 2 1

0 0 1 0 0 0 1 00 0 1 1 0 0 0 1

Converted Word

Page 26: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Octal To:

Convert 3108 to binary, decimal & hexadecimal

Convert the octalnumber to binary by

assigning a 3 bitbinary number toeach digit of the

octal number.

0 1 1 0 0 1 0 0 0

3108

Then use the binary numberand convert to decimal as before.

1 1 0 0 1 0 0 0 = 20010

Convert the binary number tohexadecimal by grouping the binary

word in groups of 4 bits.

1 1 0 0 1 0 0 0C816

Page 27: 04 chapter03 02_numbers_systems_student_version_fa14

Convert Hexadecimal To:

Convert C816 to binary, decimal & octal

Convert the hexadecimalnumber to binary

by assigning a 4 bitbinary number toeach digit of the

hexadecimal number.

1 1 0 0 1 0 0 0

C816

Then use the binary numberand convert to decimal as before.

1 1 0 0 1 0 0 0 = 20010

Convert the binary number tooctal by grouping the binary

word in groups of 3 bits.

0 1 1 0 0 1 0 0 0

3108

12

Page 28: 04 chapter03 02_numbers_systems_student_version_fa14

Binary Representation of Data

Most PLCs use 16-bit binary words to represent numbers, and most PACs use 32-bit binary words.

PLCs/PACs can use unsigned or signed numbers. This depends upon the manufacturer; however, most are signed.

If a PLC is using an unsigned number system there are no negative numbers. Using a16-bit word, what is the largest unsigned decimal number that can be represented?

Answer → The largest unsigned number would be 65,535; when all 16-bits are set to a 1. The smallest number would be zero when all 16-bits are set to a 0.

But we live in a world that uses negative numbers. These number must also be represented in binary using 1’s and 0’s.

Page 29: 04 chapter03 02_numbers_systems_student_version_fa14

Negative Numbers

It is not possible to use positive and negative symbols to represent the polarity of a number in binary.

One method of representing a binary number as a positive or negative value is to use the Most Significant Bit (MSB) of the word, (bit-15), as the sign bit.

If the sign bit is set to a zero the number is positive. If it is set to a one the number is negative.

1 1 1 0 1 1 0 10 1 0 1 1 0 0 0

Sign Bit

Page 30: 04 chapter03 02_numbers_systems_student_version_fa14

Negative Numbers

There are at least three methods for representing negative decimal numbers in binary.

A sign bit with no complement A sign bit and one’s complement A sign bit and two’s complement

Following are examples of each No complement

+7 = 0111 -7 = 1111 One’s complement

+7 = 0111 -7 = 1000 Two’s complement

+7 = 0111 -7 = 1001

Most PLCs/PACs use:

Two’s Compliment

Page 31: 04 chapter03 02_numbers_systems_student_version_fa14

Signed Binary Conversion using 2’s Complement

If the sign bit is a zero, the binary to decimal conversion is done as usual.

If the sign bit is a one, the conversion takes place by performing a 2’s Complement and then a binary to decimal conversion as usual.

When using a 16-bit signed binary number system, the range of decimal values that can be represented is:

-32,768 to 32,767

Page 32: 04 chapter03 02_numbers_systems_student_version_fa14

Performing a 2’s Complement

Converting a negative binary number to decimal requires that a 2’s Complement be performed first.

To perform a 2’s complement: Invert all the bits in the negative binary number. (1’s

become 0’s, 0’s become 1’s). Binary add 1 to the inverted binary number. (We’ll need to

learn how to add in binary). Perform a 16-bit Binary to decimal conversion. Apply a minus sign to the decimal number. This

negative decimal number is equal to the original negative binary number.

But first we need to know how to add in binary.

Page 33: 04 chapter03 02_numbers_systems_student_version_fa14

Binary Addition

In decimal, what is 1 + 1 = ?. This is not a loaded or trick question.

Yes…the answer is: 2 1 + 1 = 2 What is 1 in binary?

1 in binary is: 012

What is 2 in binary? 2 in binary is: 102

When adding in binary there are four conditions that can occur:

10110

1010

1100

ofcarryawith

2

2

2

10

01

01

1

:

ThereforeCarry 1

Page 34: 04 chapter03 02_numbers_systems_student_version_fa14

Binary Addition

When adding larger binary numbers, the resulting carry of 1 is carried into higher-order columns.

111

10

101

5 + 2 = 7

= 7

1000

11

1011

111

5 + 3 = 8

1’s get carried 3-times

= 8

Page 35: 04 chapter03 02_numbers_systems_student_version_fa14

2’s Complement Example

Convert 1000 1100 0101 00102 to decimal

Signed number: 1 0 0 0 1 1 0 0 0 1 0 1 0 0 1 0Invert all the bits: 0 1 1 1 0 0 1 1 1 0 1 0 1 1 0 1 Binary add 1: + 1 Total: 0 1 1 1 0 0 1 1 1 0 1 0 1 1 1 0

Now perform a 16-bit binary to decimal conversionon the total

Decimal Equiv: -29,614

Page 36: 04 chapter03 02_numbers_systems_student_version_fa14

Negative Decimal to Binary

Follow these steps to covert a negative decimal number to binary:

Change the sign of the decimal number from negative to positive.

Perform a decimal to 16-bit binary conversion on the positive number.

Invert all the bits in the binary number. (1’s become 0’s, 0’s become 1’s).

Binary add 1 to the inverted binary number The resultant binary number is the binary equivalent

of the original negative decimal number.

Page 37: 04 chapter03 02_numbers_systems_student_version_fa14

Example – Negative Decimal to Binary

Convert: -4,783 to binary

Change the sign: 4,783 and perform a decimal to binary conversion

4,783 = 0001 0010 1010 1111

Perform a 2’s complimentInvert all the bits: 1110 1101 0101 0000Add binary 1: +1

1110 1101 0101 0001

Therefore: -4,783 = 1110 1101 0101 00012

Page 38: 04 chapter03 02_numbers_systems_student_version_fa14

Something to Remember

When converting numbers in a 16-bit signed number system. If: The number is >32,767 it can not be

converted because the number is too large to fit into 16-bits.

The number < -32,768 it can not be converted because the number is too small to fit into 16-bits.

Page 39: 04 chapter03 02_numbers_systems_student_version_fa14

FYI - Number Ranges

Number system value ranges (Signed): 16-Bit system

-32,768 to 32,767 (INT data type) 32-Bit system

-2,147,483,648 to 2,147,483,647 (DINT data type) 64-Bit system (very small and very large numbers)

-18,446,744,073,709,551,616 to

18,446,744,073,709,551,615 128-Bit system (very small and very large numbers)

-3.4028237e-38 to 3.4028236e38

Page 40: 04 chapter03 02_numbers_systems_student_version_fa14

Binary Coded Decimal (BCD)

The Binary Coded Decimal (BCD) number system provides a convenient way for humans to input or output data from a PLC.

The conversion from binary to decimal and decimal to binary is a time consuming task. The BCD number system provides a means for easily converting decimal (the system humans are comfortable with) to code that is easily handled by computers and PLCs (binary).

BCD uses 4-bits of binary to represent the decimal numbers 0 – 9.

There are no single digits greater than 9, therefore the maximum decimal number that a 16-bit binary BCD word can represent is: 9,999 1001 1001 1001 1001

Page 41: 04 chapter03 02_numbers_systems_student_version_fa14

Decimal to BCD and BCD to Decimal

Converting a decimal number to BCD is as simple as merely converting each digit of the decimal number to a 4-bit binary number. Converting BCD to decimal is the same as converting hexadecimal to decimal. Break up the binary into groups of 4-bits and perform 4-bit binary to decimal conversions on each group.

5 3 1 9

0 0 1 10 1 0 1 0 0 0 1 1 0 0 1

Decimal Number

BCD Number

4-bits for each Decimal Number

Page 42: 04 chapter03 02_numbers_systems_student_version_fa14

BCD Thumbwheel Switch Interface – BCD Input

The output of the thumbwheel switch has one connection for each bit weight plus a common.

The thumbwheel switch output is the equivalent 4-bit BCD code for the decimal value shown on the switch.

1s Input = 0

2s Input = 0

4s Input = 0

8s Input = 1

Page 43: 04 chapter03 02_numbers_systems_student_version_fa14

BCD Display – BCD Output

The PLC can output the number 9371 to displayon a BCD display.

1001 0011 0111 0001

Page 44: 04 chapter03 02_numbers_systems_student_version_fa14

Comparison of Numeric Values Between Systems

Chart comparing some numeric values in:

Decimal Binary BCD Hexadecimal

Page 45: 04 chapter03 02_numbers_systems_student_version_fa14

Gray Code

Gray code is a special type of binary code that does not use position weighting.

The code is setup such that as it progresses from one number to the next only one bit changes. Because of this the Gray code is considered to be an error-minimizing code.

The speed of bit transition for Gray code is much faster than that of codes like BCD because only one bit changes at a time.

Page 46: 04 chapter03 02_numbers_systems_student_version_fa14

Gray Code

Some position encoders use Gray code for accurate control of motion. Encoders are used to report the position of a robots joints, machine tool axis and servomechanisms.

An encoder disk is attached to a rotating shaft. As the shaft rotates the disk rotation generates a digital Gray code that can than be interpolated to a shaft position.

Typical Encoder Disk

Page 47: 04 chapter03 02_numbers_systems_student_version_fa14

ASCII Code

The American Standard Code for Information Interchange (ASCII), phonetically pronounced “Ask-key” is an alphanumeric code used to represent the upper and lower case letters of the alphabet, numbers, punctuation marks and non-printable characters such as the backspace, linefeed, carriage return, etc.

Each keystroke on the keyboard of a computer is converted directly to an ASCII code that is than processed by the computer.

Page 48: 04 chapter03 02_numbers_systems_student_version_fa14

ASCII Code

The ASCII conversion table shown is the first 128-characters with the matching ASCII code in decimal.

ASCII codes are 6 or 7-bit codes. The extended ASCII set (128 – 255) is used for special graphics characters.

Decimal Character Decimal Character Decimal Character Decimal Character0 NUL 33 ! 66 B 99 c1 SOH 34 " 67 C 100 d2 STX 35 # 68 D 101 e3 ETX 36 $ 69 E 102 f4 EOT 37 % 70 F 103 g5 ENQ 38 & 71 G 104 h6 ACK 39 ' 72 H 105 i7 BEL 40 ( 73 I 106 j8 BS 41 ) 74 J 107 k9 HT 42 * 75 K 108 l10 LF 43 + 76 L 109 m11 VT 44 , 77 M 110 n12 FF 45 - 78 N 111 o13 CR 46 . 79 O 112 p14 SO 47 / 80 P 113 q15 SI 48 0 81 Q 114 r16 DLE 49 1 82 R 115 s17 DC1 50 2 83 S 116 t18 DC2 51 3 84 T 117 u19 DC3 52 4 85 U 118 v20 DC4 53 5 86 V 119 w21 NAK 54 6 87 W 120 x22 SYN 55 7 88 X 121 y23 ETB 56 8 89 Y 122 z24 CAN 57 9 90 Z 123 {25 EM 58 : 91 [ 124 |26 SUB 59 ; 92 \ 125 }27 ESC 60 < 93 ] 126 ~28 FS 61 = 94 ^ 127 DEL29 GS 62 > 95 _30 RS 63 ? 96 `31 US 64 @ 97 a32 SP 65 A 98 b

ASCII CONVERSION TABLE