Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Bit counting code snippet

Status
Not open for further replies.

AndaMan

Junior Member level 3
Joined
Apr 12, 2004
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
235
This bit counting code snippet idea was took from chip design (VHDL) people and implemented in ST to count number of active switch (arrange in 16 bits word) or to detect co-incident switches. It is fast, no looping, complete in single scan cycle. Can modify to support less or more bits.

FUNCTION_BLOCK BIT_CNT
VAR_INPUT
_w: WORD; (* input 16 contacts *)
END_VAR
VAR_OUTPUT
_cnt: INT; (* number of active contacts *)
END_VAR
VAR
END_VAR

_w := (_w AND 16#5555) + (SHR(_w,1 ) AND 16#5555);
_w := (_w AND 16#3333) + (SHR(_w,2 ) AND 16#3333);
_w := (_w AND 16#0F0F) + (SHR(_w,4 ) AND 16#0F0F);
_w := (_w AND 16#00FF) + (SHR(_w,8 ) AND 16#00FF);
_cnt := WORD_TO_INT(_w);

END_FUNCTION_BLOCK
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top