0
\$\begingroup\$

I have 8 wires, and I want to count how many of these are in a high state and convert this to a 3-bit binary signal. If the count of inputs that are high is greater than 4, I don't care what exact amount of inputs are on beyond that point, I just need to know whether more than 4 are high. So I only need 3 bits of output, if that makes sense.

i.e. if 1 wire is on, output 001, if 2 wires on on, output 010, etc. and if 7 or 8 wires are on output 111.

Is there an IC that can help me do this easily?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You statements are contradictory. First, you say that you don't care exactly what number are high if 4 or more are high, then you say the output should be 111 of 7 or 8 are high. Please provide a chart of inputs high vs. output bits. \$\endgroup\$
    – DoxyLover
    Commented Mar 27, 2014 at 19:48
  • \$\begingroup\$ Sorry for the confusion. Well, since I need to know whether the output is greater than 4, I need 3 bits of output anyways. So really anything signaling greater than 4 works, I figured the most likely way to do it would be just count normally until 8 is reached, and when that happens just count that as a 7. \$\endgroup\$
    – Gus
    Commented Mar 27, 2014 at 19:56
  • 2
    \$\begingroup\$ The answer you accepted is wrong. This is a good example of why you should wait a little while before accepting something. You want to give various people a chance to chime in, and for the peer review process to tell you which answers are right and which ones are wrong. \$\endgroup\$ Commented Mar 27, 2014 at 20:42

3 Answers 3

7
\$\begingroup\$

This is a simple job for a microcontroller. Spehro correctly points out that it can be accomplished with a simple lookup. If you can tolerate lower speed then counting the bits in a loop will do it too, but will take less code space.

Another way is to do this in analog. Put a 100 kΩ resistor in series with each digital output, then feed that into a comparator. If 4 out of 8 are high, then you get 0.50 of the supply voltage. If 3 out of 8 are high, then you get 0.38 of the supply voltage. Set the comparator threshold to 0.44 of the supply and it will distinguish between 4 or more, and 3 or less.

\$\endgroup\$
1
  • \$\begingroup\$ If there's a spare ADC input that would work too. \$\endgroup\$
    – Andy aka
    Commented Mar 27, 2014 at 21:12
7
\$\begingroup\$

A microcontroller could do it very easily. Since there are only 256 possible combinations, a simple lookup table would be the fastest way. (I did the below outputting 8 for all high for consistency, you can change the last entry to 7 if you want).

The code could look something like this:-

unsigned char lut[256] = {0x0, 0x1, 0x1,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, 0x3, 0x4, 0x4, 0x5, 0x4, 0x5, 0x5, 0x6, 0x4, 0x5, 0x5, 0x6, 0x5, 0x6, 0x6, 0x7, 0x3, 0x4, 0x4, 0x5, 0x4, 0x5, 0x5, 0x6, 0x4, 0x5, 0x5, 0x6, 0x5, 0x6, 0x6, 0x7, 0x4, 0x5, 0x5, 0x6, 0x5, 0x6, 0x6, 0x7, 0x5, 0x6, 0x6, 0x7, 0x6, 0x7, 0x7, 0x8}; 

    <some initialization stuff>

    for(;;) out_port= lut[in_port];  // do this forever

I don't know of any logic chip that has this exact functionality. Priority encoders do something a bit different.

If you needed really fast response time, you could program a nonvolatile memory with the lookup table, or use programmable logic.

\$\endgroup\$
0
1
\$\begingroup\$

Shouldn't a 4-bit full adder do the trick?

http://nl.farnell.com/texas-instruments/cd74hc283e/logic-4bit-binary-full-adder-16dip/dp/1750337

\$\endgroup\$
1
  • \$\begingroup\$ Good idea, but...no. The OP wants to count bits, so for example the result from 1010 would be 2 (0011). The adder adds; it doesn't count. \$\endgroup\$
    – gbarry
    Commented Mar 31, 2014 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.