[?+]: To count number of bits

SIMATIC S7-200/300/400, Step7, PCS7, CFC, SFC, PDM, PLCSIM,
SCL, Graph, SPS-VISU S5/S7, IBHsoftec, LOGO ...
Post Reply
Scarch
Posts: 103
Joined: Fri Aug 31, 2007 8:17 pm
Location: Europe

[?+]: To count number of bits

Post by Scarch » Wed May 25, 2011 7:52 pm

I have an operator panel with 130 posible bits settings (machine off-on for 130 machines.)
The adresses are from DB10.DBX0.0 to DB10.DBX130.0
I need to know how many machines have been set.
How can I know how many bits are set to one?
I mean, If I have a word, how can i extract the number of bits equal to 1?

pwkoller
Posts: 15
Joined: Sun Dec 04, 2005 9:06 am
Location: Europe

Re: [?]: To count number of bits

Post by pwkoller » Wed May 25, 2011 10:27 pm

Hello,

As i understand your question: the following code counts every true bit number .0
in db10 for the bytes 1 to 130

how it works ? -> see help on stl-statements

OPN DB 10; // your DB 10
L 0;
T MW 200; // zero BIT Counter
L 129; // how many -1 <130>

loop: T MW 100; // store BYTE Counter
SLD 3; // make pointer
LAR1 ; // load pointer in ar1
L DBB [AR1,P#1.0]; // Read bytes down to Byte 1
L 1; // mask for bit 0
AW ; // get it
L MW 200; // load BIT counter
+I ; // count
T MW 200; // store BIT counter
L MW 100; // load Byte counter
LOOP loop; // loop if greater 0

L MW 200; // sum of TRUE bits


be happy,
pwkoller

Scarch
Posts: 103
Joined: Fri Aug 31, 2007 8:17 pm
Location: Europe

Re: [?]: To count number of bits

Post by Scarch » Thu May 26, 2011 10:16 pm

I'll try it soon
THANKS A LOT

Scarch
Posts: 103
Joined: Fri Aug 31, 2007 8:17 pm
Location: Europe

Re: [?]: To count number of bits

Post by Scarch » Mon May 30, 2011 5:18 am

Sorry, it is not working. The program is executing always the loop. MW200 can not be read

PLC_ITA
Posts: 263
Joined: Sat May 27, 2006 6:46 am
Location: Europe

Re: [?]: To count number of bits

Post by PLC_ITA » Mon May 30, 2011 8:40 pm

hello, 130 bits or 130 byte ?

Your address db10.dbx0.0 to db10.dbx130.0 = 130 x 8 = 1040 bits

please tell me if you want to read all the bit ar only one bit for every byte.

If you want to read one bit for every byte, please indicate the bit number.

in 2-3 days, i send a code to count bit

:roll:

Yury
Posts: 65
Joined: Thu Nov 01, 2007 9:41 pm
Location: Belarus

Re: [?]: To count number of bits

Post by Yury » Mon May 30, 2011 8:59 pm

another example...

Code: Select all

  L     0
      T     MW     0
      OPN   DB    10
      LAR1  P#DBX 0.0
      L     130
next: T     MW     2
      L     DBB [AR1,P#0.0]
      L     2#100 // for bit 2 (2#00000100). Change here !!!
      AW    
      T     MW     4
      L     MW     0
      +I    
      T     MW     0
      +AR1  P#1.0
      L     MW     2
      LOOP  next
      L     MW     0
      L     2#100  // for bit 2 (2#00000100). Change here !!!
      /I    
      T     MW     8
you need to change the bit to the necessary in 2 places (with comments)

Scarch
Posts: 103
Joined: Fri Aug 31, 2007 8:17 pm
Location: Europe

Re: [?]: To count number of bits

Post by Scarch » Tue May 31, 2011 4:42 am

Sorry, I meant 130 bits :?
The adress 130.0 is obviously wrong. Last one will be around 16.2
I need to know how many bits have been set to one (bit set to one=machine selected). The settings come from an operator panel and there are 130 bits to be set, starting from DBX0.0 of any DB.

Thanks again

Yury
Posts: 65
Joined: Thu Nov 01, 2007 9:41 pm
Location: Belarus

Re: [?]: To count number of bits

Post by Yury » Tue May 31, 2011 9:19 pm

another example...

Code: Select all

      L     0
      T     MW     0
      OPN   DB    10
      LAR1  P#DBX 0.0
      L     130
next: T     MW     2
      A     DBX [AR1,P#0.0]
      S     M      5.0
      L     MW     0
      L     MW     4
      +I    
      T     MW     0
      +AR1  P#0.1
      CLR   
      =     M      5.0
      L     MW     2
      LOOP  next
      NOP   0
      L     MW     0
      T     MW     6
result in mw6

pwkoller
Posts: 15
Joined: Sun Dec 04, 2005 9:06 am
Location: Europe

Re: [?]: To count number of bits

Post by pwkoller » Wed Jun 01, 2011 5:21 am

Sorry, it is not working. The program is executing always the loop. MW200 can not be read
more for your attention:

Since the loop makes all its iterations in one cycle you will not see exactly the moment when it is finished
if you place the load mw200 in the next network and just watch this -> you will see the total of bits set.
or you place the code in a fc, call it and then load mw200
or you use the debugger with breakpoints in step7 to go over the loop step bey step -> you will see it works

anyway it runs over 130 bytes from byte 1 counting all the first bits only.

as i see you want to count the bits in an array of bits from 0.0 to 16.1 = 130 therefor the scan-part has to be adjusted
also a part to catch Bit 0.0 has to be added ( its not a good choice to start with data at byte 0 in DBs)

German mnemonic

Code: Select all

      AUF   DB    10                    // your DB 10
      L     0
      T     MW   200                    // BIT Counter
      L     129                         // how many -1 <130>

loop: T     MW   100                    // store BIT Counter
      LAR1                              // load pointer in ar1
      U     DBX [AR1,P#0.0]             // Read bits down to Bit 0.1
      SPBN  NoB                         // if 0 do not count
      L     MW   200                    // load BIT counter
      +     1                           // count
      T     MW   200                    // store BIT counter
NoB:  L     MW   100                    // load Byte counter
      LOOP  loop                        // loop if greater 0
      LAR1  P#0.0
      U     DBX [AR1,P#0.0]             // Read Bit 0.0
      SPBN  NoB2                        // if 0 do not count
      L     MW   200                    // load BIT counter
      +     1                           // count
      T     MW   200                    // store BIT counter
NoB2: NOP   0

      L     MW   200                    // sum of TRUE bits



greetings from pwkoller

Scarch
Posts: 103
Joined: Fri Aug 31, 2007 8:17 pm
Location: Europe

Re: [?]: To count number of bits

Post by Scarch » Fri Jun 03, 2011 10:43 am

Works OK!
Many thanks to both of you (pgood)

Post Reply