Hide Transcript
View Transcript
It’s widely understood that computers –including PLCs – do all the remarkable things they can do using only 1s and 0s. How exactly do they do this? Well, that’s an ambitious question for a short video like this, but let’s look at one aspect of that – data storage. If we could look at a physical representation of data stored in a modern PLC, we’d see an expansive string or array of millions of 2-position (binary) switches. A switch in the on position is generally represented as a 1, and in the off position a 0, and each of these switches we call a bit. So one bit can reflect or record a single true/false condition. Something like an input signal being present or not.
To handle more sophisticated information, a computer needs to group bits into standardized chunks or blocks. The most commonly known standard data chunk, the byte, contains 8 bits.
Back to our visualization of stored data, let’s think of these millions of bits divided into bytes. The very first of these is, in programming fashion, referred to as Byte 0, followed by Byte 1, and so on. This is a little simplified – the data is more organized than this implies, but essentially this is how the CPU locates the data it needs - by this numerical byte address. But once it finds the chunk of bits its looking for, how does the CPU interpret them? This is where Data Types come in. Each piece of data stored in the PLC's memory has a data type associated with it.
Let’s take a look at the elemental data types a PLC programmer will use in Do-more.
We mentioned the single bit, sometimes called a Boolean data type.
In Do-more, bytes are available as either signed or unsigned variables.
A chunk of 2 bytes – 16 bits – is known as a word. A word generally represents an integer (whole number) value in a binary format, either signed or unsigned.
Comprising 32 bits, double words can be used to represent much larger numbers.
Real numbers likewise use 32 bits, but represent a fractional value in a decimal format, or at least a close approximation thereof. With these data types, we can store and manipulate all the information needed for any process a PLC could be tasked with. Let’s take a look at a commonly used chunk of data like a date and time. The year can be stored as an unsigned word, the month as an unsigned byte, and the day as an unsigned byte, while the whole date in numerical format could be stored as a double word. Likewise, the time can be broken into elements that can be stored in individual bytes or stored in a concatenated form as a double word. Data in the PLCs memory is segmented with different data types assigned to specific allocated blocks, so the elements of a time and date stamp could be scattered throughout the memory, each with its own address. Certainly doable, but is there a more efficient way?
Enter data structures, commonly known as structs. A struct is essentially an amalgamation of related data points that is stored and accessed as a single data type. As an example, let's take a look at the counter struct built into Do-more. To explore the memory configuration in Do-more, drop down the PLC menu and select Memory Configuration. In the window that opens, you’ll find three tabs. For now let’s skip to the third tab, structs.
The default view is meant to display user data types. Unless you’re looking at a project in which some of these have previously been implemented, you’ll see a blank table here. Select Built-in Structs with the radio button here labeled Show. The table now populates with all of Do-more's built-in data structures. Double-click the counter struct to open its detail window. You’ll see here all of the elements, or fields, that make up the structure.
The structure uses 2 double words as we can see here and comprises these 5 different data fields. The first of these is called .Acc – it’s the accumulator that shows the counter’s current count as a signed double word. The remaining fields are all bit elements that indicate different statuses of the timer. In the layout column, we can see how these different elements are arranged within the struct. .Acc is double word 1 whereas the bits are all contained within double word 0, the lower word of the struct. The “Done” bit, for instance, is dw0:0, meaning that it is the rightmost or least significant bit of double word 0. For a visual representation of the struct, View Memory Layout. While .Acc is read-write, the status bits are read-only. Display format determines how the CPU will by default represent the data contained in that field. To get a handle on this concept, let’s open a Data window in Do-more Designer. For this, the programming environment must be connected to either a live PLC or to the Do-more simulator. This is a good time to point out that we are discussing a struct as a Data Type, or a definition of what a variable of this type will contain – we aren’t looking at an actual variable. In the data window, or in ladder logic, there is no way to access the data type – we can only look at an instance of a variable of that type. Just for this example, we’ll look at a V variable – a single unsigned word.
Here we see the value in “Native” display format. In the case of an unsigned word, “Native” means displayed as a numerical value, the way humans generally think of numbers. This same information can be displayed as a binary number, closer to the way computers handle things. Other format options include hexadecimal - octal for old-school programmers - as well as more specialized forms like timer values or IP addresses. Each ASCII character is stored as one byte, so a word encapsulates 2 characters – in this case, “hi.” Same data, same data type, but different ways to represent it visually – for the benefit of us humans. While we’re at the data window, let's cover that last column in the struct details window – the data view detail. Again, we’re going to look at an instance of a variable of the data type we’re discussing, in this case, the counter struct. We’ll just pick counter CT0. In the data window or in a ladder logic program, elements of the struct can be accessed individually, addressed like this: CT0.Acc, for instance, or CT0.Done. In a data window, you can also access the entire struct, with a few choices of display styles. Notice that some fields show up in the long format but not the short. This is what this last column refers to. Now we have all of our elemental data types and we see the larger data types known as structs are essentially combinations of those basic types.
In the next video, we’ll delve into the variables created with these data types and how they are organized in the PLCs memory with Heap Items and Memory Blocks, along with the powerful possibilities of User-Defined Data Types. Click here for that video.
Click here for more Do-more videos. Click here to subscribe to Automation Direct’s YouTube channel.