Sign in to follow this  
Followers 0
Marco8037

Is it possible to set/reset each bit individually of one word?

7 posts in this topic

Hello everyone, I'm new on Twincat and expert on Omron. I'm trying to do on twincat some things that were easily possible to do on Omron, but on Twincat I don't know if I can do the same by a similar way. On the examples, you don't need to be an expert on Omron because I explain the meaning of each code. Let's go to the examples: Example 1: Please see this code on Omron: On this code, "condition" is of type BOOL. When "condition" is TRUE, the MOV instruction moves the value 0 to the variable with name "variable". "variable" is of type WORD, by the way it has 16 bits. On Omron systems, each variable of type word have not only a name, but also a number. On this case, the variable with name "variable" is of WORD type an has the number 20. Since the variable with the number 20 is of WORD type, and have 16 bits, I can consider the following bits: 20.00 20.01 20.02 20.03 ... ... 20.14 20.15 If I need on my program 16 variables type bool, and if I want to reset all of them, I only need to move the value 0 to the correspondent word. On Twincat, the only one way that I know to do the same is the following one: On this case, When the "condition" is TRUE, I make reset to all of the 16 bits (Bit_0; Bit_1; Bit_3 ... to Bit_15 are variables of type BOOL). But, to do that, I have to write the same code 16 times (Each time for each bit), because the Reset instruction resets only one bit. My question is: Is there a diferent way on Twincat to reset all the bits at the same time? I think it could mabe be possible, using the MOV instruction, to move the value 0 to a specified variable of WORD type. But, How can I use each bit of that variable as a type BOOL variable? Example 2: Please see this code on Twincat: On this code, all the variables are of WORD type. I move the value on "variavel_20" to "variavel_50", the value on "variavel_21" to "variavel_51", the value on "variavel_22" to "variael_52" and the value on "variavel_23" to "variavel_53". To do that, I need to use 4 MOVE instructions. The following code on Omron makes the same, with only one instruction: On Omron, since each variable have not only a name, but also a number, I can use this instruction to move 4 consecutive words, while the first source WORD is the word number 20 (with the name "variable_20"), and the first destination word is the word number 50 (with the name "variable_50"). Since in Omron I use not the variable names, but its number, I can use unstructions like this. My question is: If I need to move 100 words (100 variables of WORD type) on Twincat, Will I need to write the same instruction 100 times? Or, Is there a different way to do this? Since on Twincat the variables have only a name to identify them, I think it could be possible if it was possible to define a buffer with some consecutive variables. But How can I do that? Example 3: Please see the following Twincat code: All the variables are of WORD type. On this code, I move the value on the variable "variavel_10" to all the variables with the names from "variavel_20" to "variavel_50". On Omron, I use only one instruction to do the same:

Share this post


Link to post
Share on other sites
If you declare a word (or any other variable that consists of multiple bits) you can manipulate each bit very similar to Omron like this: The other problem sounds like an array is your solution: VAR ArrayVariabe : ARRAY [0..1000] of WORD; END_VAR You can then easily create function block that manipulate the values. Edited by arj3090

Share this post


Link to post
Share on other sites
You need to get used to using arrays for the functions you need. If you declare a variable array with BOOLEAN properties then the state of each bit in the array can be easily changed. Likewise if you declare a variable array with WORD properties than the value of each word can be changed. If you are an expert in OMRON then you would have used symbol variables in CX-Programmer with 'advanced' properties set to ARRAY. CoDeSys works the same way but offers more advanced methods of ARRAY declaration as well as STRUCTURES. This allows multi-dimensional arrays consisting of different variable properties [bOOLEAN,WORD,INT,STRING etc. etc.] to be used in your application. Very powerful but a bit confusing to the Newbie.

Share this post


Link to post
Share on other sites
Thank you very much. I have understood very well how to set / reset each bit individually of an WORD. When I told I am expert in Omron, it was only to say that I now much more about Omron than Twincat. However, I have never used arrys in Omron, since I can make what I intend using just a special instructions that manipulate some variables by a specific order, as in the examples I have shown in the post. By the way, I am not familiarized with using of arrays. About the arrays declarations, I was looking at it and it seems not very dificult. But about making a function block to minipulate an array, it seems a little bit more complicated for me, because the great way to manipulate it is perhaps using a Cycle FOR, but I am not familiar with C programming. Making it on Ladder, I think is nat applicable. Could you please give me an example code of a function block to manipulate an array? Let's supose the following example: VAR ArrayVariabe : ARRAY [0..10] of WORD; END_VAR In the programme I know I identify each word of the array like this: ArrayVariabe [0] ArrayVariabe [1] ArrayVariabe [2] ... ArrayVariabe [9] ArrayVariabe [10] If I try to make a FB in Ladder, I can have an input variable type WORD with the name "value". In Ladder I can move the variable "value" to each part of the array 10 times. But how can I make a code to move it only once, moving the same value to all the array parts? Thank you very much

Share this post


Link to post
Share on other sites
Here are two examples of creating functions to fill an array. You have to be cautious because they do not check array bounds, so if you specify more elements than in the array, it can crash the program. Edited by arj3090

Share this post


Link to post
Share on other sites
hello, Thank you very much by your quick answer. Unfortunately I have been a lot of work to do and I could not reply earlier. I could reproduce your code and it works perfectly. However, I have made a change on the function FillArray2, like the following picture: On this case, the variable "value" is not of WORD type but of Array of WORD type. By the way, I don't move the same value for all of the array elements, but I move each element value of "array_origin" ARRAY to the "array_destini" ARRAY. After it, I tried to use the same function to move element values of an array OF UINT to another array OF UINT. Since this function only works with arrays OF WORD, I had no success. Of course I could make another similar function, but to move elements of arrays OF UINT. But I have tried something like the following example: However, when I make "rebuild" it gives me an error: Of course: the BOX UINT_TO_WORD only converts a UINT variable to a WORD variable, and it cannot convert an array of UINT to an array OF WORD. My question is the following one: Is there any way to convert the elements type of an array? Could you also tell me, if you know the difference between an UINT type variable and an WORD type variable? In my opinion it seems there are no difference because the two ones have 16 bits and can have a decimal value from 0 to 65535. Can you see some other aspect that can distinguish the two ones? Thank you very much.

Share this post


Link to post
Share on other sites
As far as I know there is no difference between a WORD and UINT in TwinCAT. To convert an array, you will need to create another function. It will be very similar to the FillArray2 function, but use the UINT_TO_WORD conversion within the For-Next loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0