SCL Progam

Berra

Member
Join Date
Mar 2007
Location
Sweden
Posts
137
Hi

I whant to check out if the first bit ( can be changed to second bit) in a woord is set to one,
I have a db with 100 words.

case first bit OF
1:
for INDEX := StartAdressDB TO StoppAdressDB

IF StartAdressDB OR StoppAdressDB = W#160000_0001 Then
OutPut = 1;

else

End_if

End_Case

But it says ERROR: You most use a Bool in a IF statment, Are they some other way to compare bit in woord in a FOR LOOP????



// Berra
 
Change
IF StartAdressDB OR StoppAdressDB = W#160000_0001 Then

to
IF (StartAdressDB = W#160000_0001) OR (StoppAdressDB = W#160000_0001) Then

It will make that particular error message go away. Dont know if the rest of the code is OK.

I dont know what you are trying to do, but it does not look pretty.
 
He says he wants to check first bit in a word if its 1. But that code wont do anything like that...

This one would do it:

Code:
FUNCTION CheckFirstBitInWord : BOOL

VAR_INPUT
    WordToCheck : WORD;
END_VAR

    IF ((WordToCheck AND w#16#0001) <> w#16#0)
    THEN
        CheckFirstBitInWord := True;
    ELSE
        CheckFirstBitInWord := False;
    END_IF;
    
END_FUNCTION
 
Last edited:
Yes TurpoUrpo thats right, but i also whant to loop from db100.dw0 to db100.dbw200 and check evrey word.

So i think i must have a for loop with some Index, ot to you have a better idea ???

// Berra
 
What result do you require, an array of 100 bits with TRUEs and FALSEs in it indicating if the WORD meets the test ?
 
hrm, the first bit in evrey word is a alarm bit, so if the first word has the first bit set i whant to show that as 1 in a output. And so if the first bit in the first word is false and the first bit in db100.dbw200 is true. I whant the same output to show one.

I hope you understand

regards

// Berra
 
If the first bit in any of the 100 words in the DB is true then the function should return true ?
 
Here another one, with example call with for loop (i assumed you could do it yourself with first function I introduced)

Code:
FUNCTION CheckBitInWord : BOOL

VAR_INPUT
    WordToCheck : WORD;
    BitToCheck : INT;
END_VAR
VAR_TEMP
    WordTemp : WORD;
    BitsInWord AT WordTemp : ARRAY[0..15] OF BOOL;
END_VAR

WordTemp := WordToCheck;
CheckBitInWord := BitsInWord[BitToCheck];
    
END_FUNCTION


FUNCTION BitInWords : VOID

VAR_TEMP
    i : INT;
END_VAR

FOR i := 0 TO 99 DO
    "AlarmBits".AlarmArray[i] := CheckBitInWord(WordToCheck := DB5.dbw[i*2], BitToCheck := 0);   
END_FOR;
 

END_FUNCTION
 
This one does it too, if you need to check least significant bit in word

Code:
FUNCTION CheckBitInWords : VOID

VAR_TEMP
    i : INT;
END_VAR

FOR i := 0 TO 99 DO
    "AlarmBits".AlarmArray[i] := WORD_TO_BOOL(DB5.DBW[i*2]);  
END_FOR;
 

END_FUNCTION
If you need to check other bits, you can just use SHR to have correct bit in place of least significant bit.

Like this :

Code:
FUNCTION CheckBitInWords : VOID

VAR_TEMP
    i : INT;
END_VAR

FOR i := 0 TO 99 DO
    "AlarmBits".AlarmArray[i] := WORD_TO_BOOL(SHR(IN:=DB5.DBW[i*2],N:=8));  
END_FOR;
 

END_FUNCTION
I came with this idea righ after preceding post. I think this one is lot better.
 
Last edited:
yes thats right L D [AR2,P#0.0], but i think i can use TurpoUrpo code and modificate it.

Thanks TurpoUrpo


// Berra
 
Code:
FUNCTION CheckBitInWords : BOOL;

VAR_TEMP
    i : INT;
END_VAR

FOR i := 0 TO 99 DO
    IF (WORD_TO_BOOL(DB5.DBW[i*2]) = True)
    THEN
        CheckBitInWords := True;
    END_IF;
END_FOR;

END_FUNCTION
I think ill do tomorrow version that accepts byte, word or dword input and checks designated bit. Its nice execrice for me.

It might work like this also, Ill test it too tomorrow.

Code:
FUNCTION CheckBitInWords : BOOL;

VAR_TEMP
    i : INT;
END_VAR

FOR i := 0 TO 99 DO
    IF (WORD_TO_BOOL(DB5.DBW[i*2]))
    THEN
        CheckBitInWords := True;
    END_IF;
END_FOR;

END_FUNCTION
 
You forgot this:

Code:
FUNCTION CheckBitInWords : BOOL;

VAR_TEMP
    i : INT;
END_VAR
[COLOR=Red]CheckBitInWords := False;[/COLOR]
FOR i := 0 TO 99 DO
    IF (WORD_TO_BOOL(DB5.DBW[i*2]) = True)
    THEN
        CheckBitInWords := True;
    END_IF;
END_FOR;

END_FUNCTION
 

Similar Topics

HI i would like to know how to get a variable that will store the amount of times a program has been executed. The issue is I have 3 DBs for 1 FB...
Replies
2
Views
79
Hi, I have an intermediate-advance knowledge of programming with TIA Porta in Ladder, would you recommend me to start learning SCL for it to...
Replies
11
Views
559
Hello nice to meet you, im new in here, I'm currently trying to convert code written in STL for a S7-400 to SCL for an S7-1500, because when i run...
Replies
5
Views
317
Hi everyone, I am new to this amazing world of PLC, well... I mean, in practice, since I already knew electronics, programming languages, IT, and...
Replies
7
Views
652
Hi all, This is my first post; I am new to PLC Controls stuff and English is not my native language, so I apologize in advance if something is...
Replies
4
Views
512
Back
Top Bottom