Data Inside PLC
Think of PLC memory as a cabinet with drawers called Registers. Data is held in these Registers.These Registers come in different sizes, and hold different kinds of data.
Bits :Can have values 1 or 0. 1 typically represents On while 0 represents Off. Bits are the building block for all other types of data.
Integers :Whole numbers (no decimal points). Called: Characters (char), Integers (int), long Integers (long int) or Words. Look for the bit size and whether they are signed or unsigned. Unsigned are positive numbers, while signed are positive or negative.
Floating point numbers : Numbers with decimal points, and can be positive or negative. They are called floating point numbers (Float), with their larger variety called double floats.
Type | # bits | Signed/Unsigned | Min Value | Max Value |
---|---|---|---|---|
Bit | 1 | 0 | 1 | |
Int | 8 | Signed | -128 | 127 |
Unsigned | 0 | 255 | ||
16 | Signed | -32,768 | 32,767 | |
Unsigned | 0 | 65,535 | ||
32 | Signed | -2,147,483,648 | 2,147,483,647 | |
Unsigned | 0 | 4,294,967,295 | ||
Floating Point | 32 | 1.175 Eâ38 | 3.403 E+38 | |
64 | 2.225 Eâ308 | 1.798 E+308 |
Addresses/Tags : The Registers are all stacked side by side in the PLCâs memory. Each location has an Address that lets the PLC know what data youâre talking about. Older PLC software requires the user to refer to data by this Address (example âx1023â could mean the 1023âd register). Some newer software makes the Addresses transparent.
The user gives a piece of data a name (example âOven2Temperatureâ), and the PLC keeps track of where the register is located. If the software uses Addresses to refer to data itâs called âAddress Basedâ, if it uses named data itâs called âTag Basedâ. Some programming packages are Address Based, but allow Addresses to have âNicknamesâ or something similar.
Their benefits over standard Address Based systems tend to be limited. Advantages to Tag based systems become evident as programs grow, and remembering whatâs stored in x1023 becomes difficult. If your program is going to have any complexity at all using a Tag Based System simplifies the design.
Diving Deeper
Inside the Data Types : As stated earlier, all Data Types are made of Bits (1â˛s and 0â˛s). An 8 bit number is written like bbbbbbbb (where âbâ can represent a 0 or 1), so you may have 00000000, 11111111 or 01011110âŚ. any combo will do. What these Bits mean is determined by the Data Type.
Unsigned Integers : The least valuable bit is the rightmost bit and double in value each position you move left. The right-most bit is 1 itâs worth 1 next bit to the left 2 4 8 16 32 64 128 256 âŚâŚ (this goes on for as many bits as the Data Type calls for). Hereâs what its looks like for an Unsigned 8-bit Integer:
If all 8 bits are 0 (00000000) then we get 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 = 0
If all 8 bits are 1 (11111111) then we get 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
So the range of an 8 bit unsigned Integer is 0 â 255
An example of something in between (00110101) 0 + 0 + 32 + 16 + 0 + 4 + 0 + 1 = 53
Signed Integers : Theyâre a tad more complicated check out wikipediaâs article on 2âs compliment for a good understanding.
Hexadecimal Format (Hex) : Instead of writing every bit out, it is common to group sets of 4 bits together. Each group can have a value of 0 â 15, which causes a problem since our number system goes from 0 â 9, so we also use A, B, C, D, E and F to get a total of 16 values. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9 , A, B, C, D, E, F)
Example 1001 0011 1111 0010 in Hex would be 93F2
To be clear that weâre using Hex format we tack on a â0xâ the the start of the number so our last example would be written 0x93F2
Characters: (typically 8 bit unsigned integers) are often used to represent letter or symbols. Example: you may use them to display text on an operator panel. Use an ASCII table to see how values are mapped to symbols.
What about the bits in Floating Point numbers, how do they break out? It is somewhat complicated, I donât really want to get into that in this Post. It is unlikely as a PLC user that you will ever need to know how their bits translate to values, so donât worry about it.
The Basics - Maths
Addition , Subtraction , Multiplication and Division are what youâd expect. If you havenât done math in a programming language before, there are a couple of things you should be aware of.
¡ Overflow : Integers and Floating Point Numbers have size limits. When you go beyond their size youâll cause yourself problems.
Example: 8 bit Unsigned Int with value 255 in bits looks like 11111111. If we add 1 to it we get 100000000. There isnât storage for a 9â˛th bit, so weâre left with 00000000 or 0. So in this case 255 + 1 = 0
Another Example: 8 bit Unsigned Int with value 0 in bits looks like 00000000. If we subtract 1 to it we get 11111111. So, we can get the opposite problem 0 â 1 = 255
¡ Rounding : Floating Point numbers arenât perfect. For this reason, you shouldnât check to see if Floating point numbers are equal to a value, you should check to see if itâs within a range of numbers.
Example: 2 * 3 performed with Floating Point numbers may not result in exactly 6.
It could be 5.9999999999998 or something similar. So instead of asking is Example = 6, ask is Example greater than 5.9 and less than 6.1. Donât worry about how to ask these questions, weâll talk about that later in the tutorial
Boolean Math
Some programming packages allow bit manipulation by using Boolean Math . The main operations are:
OR (symbol â|â ): A = B | C. If either B is 1 or C is 1, then A is 1. If both B and C are 0, then A is 0.
AND (symbol â&â): A = B & C. If both B and C are 1, then A is 1. If either B of C are 0, then A is 0.
Exclusive OR (symbol âââ): A = B â C. If either B is 1 or C is 1, but not both, then A is 1. If both B and C are 1, or both B and C are 0 then A is 0.