Automated Manufacturing Systems

numbers



13. NUMBERS AND DATA

 

13.1 INTRODUCTION

Base 10 (decimal) numbers developed naturally because the original developers (probably) had ten fingers, or 10 digits. Now consider logical systems that only have wires that can be on or off. When counting with a wire the only digits are 0 and 1, giving a base 2 numbering system. Numbering systems for computers are often based on base 2 numbers, but base 4, 8, 16 and 32 are commonly used. A list of numbering systems is give in Figure 169 Numbering Systems. An example of counting in these different numbering systems is shown in Figure 170 Numbers in Decimal, Binary, Octal and Hexadecimal.

 

Figure 169 Numbering Systems

 

Figure 170 Numbers in Decimal, Binary, Octal and Hexadecimal

The effect of changing the base of a number does not change the actual value, only how it is written. The basic rules of mathematics still apply, but many beginners will feel disoriented. This chapter will cover basic topics that are needed to use more complex programming instructions later in the book. These will include the basic number systems, conversion between different number bases, and some data oriented topics.

13.2 NUMERICAL VALUES

13.2.1 Binary

Binary numbers are the most fundamental numbering system in all computers. A single binary digit (a bit) corresponds to the condition of a single wire. If the voltage on the wire is true the bit value is 1. If the voltage is off the bit value is 0. If two or more wires are used then each new wire adds another significant digit. Each binary number will have an equivalent digital value. Figure 171 Conversion of a Binary Number to a Decimal Number shows how to convert a binary number to a decimal equivalent. Consider the digits, starting at the right. The least significant digit is 1, and is in the 0th position. To convert this to a decimal equivalent the number base (2) is raised to the position of the digit, and multiplied by the digit. In this case the least significant digit is a trivial conversion. Consider the most significant digit, with a value of 1 in the 6th position. This is converted by the number base to the exponent 6 and multiplying by the digit value of 1. This method can also be used for converting the other number system to decimal.

 

Figure 171 Conversion of a Binary Number to a Decimal Number

Decimal numbers can be converted to binary numbers using division, as shown in Figure 172 Conversion from Decimal to Binary. This technique begins by dividing the decimal number by the base of the new number. The fraction after the decimal gives the least significant digit of the new number when it is multiplied by the number base. The whole part of the number is now divided again. This process continues until the whole number is zero. This method will also work for conversion to other number bases.

 

Figure 172 Conversion from Decimal to Binary

Most scientific calculators will convert between number bases. But, it is important to understand the conversions between number bases. And, when used frequently enough the conversions can be done in your head.

Binary numbers come in three basic forms - a bit, a byte and a word. A bit is a single binary digit, a byte is eight binary digits, and a word is 16 digits. Words and bytes are shown in Figure 173 Bytes and Words. Notice that on both numbers the least significant digit is on the right hand side of the numbers. And, in the word there are two bytes, and the right hand one is the least significant byte.

 

Figure 173 Bytes and Words

Binary numbers can also represent fractions, as shown in Figure 174 A Binary Decimal Number. The conversion to and from binary is identical to the previous techniques, except that for values to the right of the decimal the equivalents are fractions.

 

Figure 174 A Binary Decimal Number
13.2.1.1 - Boolean Operations

In the next chapter you will learn that entire blocks of inputs and outputs can be used as a single binary number (typically a word). Each bit of the number would correspond to an output or input as shown in Figure 175 Motor Outputs Represented with a Binary Number.

 

Figure 175 Motor Outputs Represented with a Binary Number

We can then manipulate the inputs or outputs using Boolean operations. Boolean algebra has been discussed before for variables with single values, but it is the same for multiple bits. Common operations that use multiple bits in numbers are shown in Figure 176 Boolean Operations on Binary Numbers. These operations compare only one bit at a time in the number, except the shift instructions that move all the bits one place left or right.

 

Figure 176 Boolean Operations on Binary Numbers
13.2.1.2 - Binary Mathematics

Negative numbers are a particular problem with binary numbers. As a result there are three common numbering systems used as shown in Figure 177 Binary (Integer) Number Types. Unsigned binary numbers are common, but they can only be used for positive values. Both signed and 2s compliment numbers allow positive and negative values, but the maximum positive values is reduced by half. 2s compliment numbers are very popular because the hardware and software to add and subtract is simpler and faster. All three types of numbers will be found in PLCs.

 

Figure 177 Binary (Integer) Number Types

Examples of signed binary numbers are shown in Figure 178 Signed Binary Numbers. These numbers use the most significant bit to indicate when a number is negative.

 

Figure 178 Signed Binary Numbers

An example of 2s compliment numbers are shown in Figure 179 2s Compliment Numbers. Basically, if the number is positive, it will be a regular binary number. If the number is to be negative, we start the positive number, compliment it (reverse all the bits), then add 1. Basically when these numbers are negative, then the most significant bit is set. To convert from a negative 2s compliment number, subtract 1, and then invert the number.

 

Figure 179 2s Compliment Numbers

Using 2s compliments for negative numbers eliminates the redundant zeros of signed binaries, and makes the hardware and software easier to implement. As a result most of the integer operations in a PLC will do addition and subtraction using 2s compliment numbers. When adding 2s compliment numbers, we don't need to pay special attention to negative values. And, if we want to subtract one number from another, we apply the twos compliment to the value to be subtracted, and then apply it to the other value.

Figure 180 Adding 2s Compliment Numbers shows the addition of numbers using 2s compliment numbers. The three operations result in zero, positive and negative values. Notice that in all three operation the top number is positive, while the bottom operation is negative (this is easy to see because the MSB of the numbers is set). All three of the additions are using bytes, this is important for considering the results of the calculations. In the left and right hand calculations the additions result in a 9th bit - when dealing with 8 bit numbers we call this bit the carry C. If the calculation started with a positive and negative value, and ended up with a carry bit, there is no problem, and the carry bit should be ignored. If doing the calculation on a calculator you will see the carry bit, but when using a PLC you must look elsewhere to find it.

 

Figure 180 Adding 2s Compliment Numbers

The integers have limited value ranges, for example a 16 bit word ranges from -32,768 to 32,767 whereas a 32 bit word ranges from -2,147,483,648 to 2,147,483,647. In some cases calculations will give results outside this range, and the Overflow O bit will be set. (Note: an overflow condition is a major error, and the PLC will probably halt when this happens.) For an addition operation the Overflow bit will be set when the sign of both numbers is the same, but the sign of the result is opposite. When the signs of the numbers are opposite an overflow cannot occur. This can be seen in Figure 181 Carry and Overflow Bits where the numbers two of the three calculations are outside the range. When this happens the result goes from positive to negative, or the other way.

 

Figure 181 Carry and Overflow Bits

These bits also apply to multiplication and division operations. In addition the PLC will also have bits to indicate when the result of an operation is zero Z and negative N.

13.2.2 Other Base Number Systems

Other number bases are typically converted to and from binary for storage and mathematical operations. Hexadecimal numbers are popular for representing binary values because they are quite compact compared to binary. (Note: large binary numbers with a long string of 1s and 0s are next to impossible to read.) Octal numbers are also popular for inputs and outputs because they work in counts of eight; inputs and outputs are in counts of eight.

An example of conversion to, and from, hexadecimal is shown in Figure 182 Conversion of a Hexadecimal Number to a Decimal Number and Figure 183 Conversion from Decimal to Hexadecimal. Note that both of these conversions are identical to the methods used for binary numbers, and the same techniques extend to octal numbers also.

 

Figure 182 Conversion of a Hexadecimal Number to a Decimal Number

 

Figure 183 Conversion from Decimal to Hexadecimal

13.2.3 BCD (Binary Coded Decimal)

Binary Coded Decimal (BCD) numbers use four binary bits (a nibble) for each digit. (Note: this is not a base number system, but it only represents decimal digits.) This means that one byte can hold two digits from 00 to 99, whereas in binary it could hold from 0 to 255. A separate bit must be assigned for negative numbers. This method is very popular when numbers are to be output or input to the computer. An example of a BCD number is shown in Figure 184 A BCD Encoded Number. In the example there are four digits, therefore 16 bits are required. Note that the most significant digit and bits are both on the left hand side. The BCD number is the binary equivalent of each digit.

 

Figure 184 A BCD Encoded Number

Most PLCs store BCD numbers in words, allowing values between 0000 and 9999. They also provide functions to convert to and from BCD. It is also possible to calculations with BCD numbers, but this is uncommon, and when necessary most PLCs have functions to do the calculations. But, when doing calculations you should probably avoid BCD and use integer mathematics instead. Try to be aware when your numbers are BCD values and convert them to integer or binary value before doing any calculations.

13.3 DATA CHARACTERIZATION

13.3.1 ASCII (American Standard Code for Information Interchange)

When dealing with non-numerical values or data we can use plain text characters and strings. Each character is given a unique identifier and we can use these to store and interpret data. The ASCII (American Standard Code for Information Interchange) is a very common character encryption system is shown in Figure 185 ASCII Character Table and Figure 186 ASCII Character Table. The table includes the basic written characters, as well as some special characters, and some control codes. Each one is given a unique number. Consider the letter A, it is readily recognized by most computers world-wide when they see the number 65.

 

Figure 185 ASCII Character Table

 

Figure 186 ASCII Character Table

This table has the codes from 0 to 127, but there are more extensive tables that contain special graphics symbols, international characters, etc. It is best to use the basic codes, as they are supported widely, and should suffice for all controls tasks.

An example of a string of characters encoded in ASCII is shown in Figure 187 A String of Characters Encoded in ASCII.

 

Figure 187 A String of Characters Encoded in ASCII

When the characters are organized into a string to be transmitted and LF and/or CR code are often put at the end to indicate the end of a line. When stored in a computer an ASCII value of zero is used to end the string.

13.3.2 Parity

Errors often occur when data is transmitted or stored. This is very important when transmitting data in noisy factories, over phone lines, etc. Parity bits can be added to data as a simple check of transmitted data for errors. If the data contains error it can be retransmitted, or ignored.

A parity bit is normally a 9th bit added onto an 8 bit byte. When the data is encoded the number of true bits are counted. The parity bit is then set to indicate if there are an even or odd number of true bits. When the byte is decoded the parity bit is checked to make sure it that there are an even or odd number of data bits true. If the parity bit is not satisfied, then the byte is judged to be in error. There are two types of parity, even or odd. These are both based upon an even or odd number of data bits being true. The odd parity bit is true if there are an odd number of bits on in a binary number. On the other hand the Even parity is set if there are an even number of true bits. This is illustrated in Figure 188 Parity Bits on a Byte.

 

Figure 188 Parity Bits on a Byte

Parity bits are normally suitable for single bytes, but are not reliable for data with a number of bits.

 

13.3.3 Checksums

Parity bits are suitable for a few bits of data, but checksums are better for larger data transmissions. These are simply an algebraic sum of all of the data transmitted. Before data is transmitted the numeric values of all of the bytes are added. This sum is then transmitted with the data. At the receiving end the data values are summed again, and the total is compared to the checksum. If they match the data is accepted as good. An example of this method is shown in Figure 189 A Simplistic Checksum.

 

Figure 189 A Simplistic Checksum

Checksums are very common in data transmission, but these are also hidden from the average user. If you plan to transmit data to or from a PLC you will need to consider parity and checksum values to verify the data. Small errors in data can have major consequences in received data. Consider an oven temperature transmitted as a binary integer (1023d = 0000 0100 0000 0000b). If a single bit were to be changed, and was not detected the temperature might become (0000 0110 0000 0000b = 1535d) This small change would dramatically change the process.

13.3.4 Gray Code

Parity bits and checksums are for checking data that may have any value. Gray code is used for checking data that must follow a binary sequence. This is common for devices such as angular encoders. The concept is that as the binary number counts up or down, only one bit changes at a time. Thus making it easier to detect erroneous bit changes. An example of a gray code sequence is shown in Figure 190 Gray Code for a Nibble. Notice that only one bit changes from one number to the next. If more than a single bit changes between numbers, then an error can be detected.

 

 

Figure 190 Gray Code for a Nibble

13.4 SUMMARY

· Binary, octal, decimal and hexadecimal numbers were all discussed.

· 2s compliments allow negative binary numbers.

· BCD numbers encode digits in nibbles.

· ASCII values are numerical equivalents for common alphanumeric characters.

· Gray code, parity bits and checksums can be used for error detection.

13.5 PRACTICE PROBLEMS

(Note: Problem solutions are available at http://sites.google.com/site/automatedmanufacturingsystems/)

1. Why are binary, octal and hexadecimal used for computer applications?

2. Is a word is 3 nibbles?

3. What are the specific purpose for Gray code and parity?

4. Convert the following numbers to/from binary

 

5. Convert the BCD number below to a decimal number,

 

6. Convert the following binary number to a BCD number,

 

7. Convert the following binary number to a Hexadecimal value,

 

8. Convert the following binary number to a octal,

 

9. Convert the decimal value below to a binary byte, and then determine the odd parity bit,

10. Convert the following from binary to decimal, hexadecimal, BCD and octal.

11. Convert the following from decimal to binary, hexadecimal, BCD and octal.

12. Convert the following from hexadecimal to binary, decimal, BCD and octal.

13. Convert the following from BCD to binary, decimal, hexadecimal and octal.

14. Convert the following from octal to binary, decimal, hexadecimal and BCD.

15.

a) Represent the decimal value thumb wheel input, 3532, as a Binary Coded Decimal (BCD) and a Hexadecimal Value (without using a calculator).

i) BCD

ii) Hexadecimal

b) What is the corresponding decimal value of the BCD value, 1001111010011011?

16. Add/subtract/multiply/divide the following numbers.

17. Do the following operations with 8 bit bytes, and indicate the condition of the overflow and carry bits.

18. Consider the three BCD numbers listed below.

a) Convert these numbers to their decimal values.

b) Convert the decimal values to binary.

c) Calculate a checksum for all three binary numbers.

d) What would the even parity bits be for the binary words found in b).

19. Is the 2nd bit set in the hexadecimal value F49?

20. Explain where grey code occurs when creating Karnaugh maps.

21. Convert the decimal number 1000 to a binary number, and then to hexadecimal.

13.6 ASSIGNMENT PROBLEMS

1. Why are hexadecimal numbers useful when working with PLCs?

 

Site Links


Engineer On A Disk Main Page

- PLC Book Book Page