I’m working on a project and I’m stuck with a switching problem.
Imagine a box with 4 momentary switches (SW1 - SW4) and 4 leds (A - D). What I want is, when I press and release a switch, only the corresponding LED to turn on and stay on until another switch is pressed.
So if I press SW1, LED A will be on. If I press SW3, LED A will turn off and LED C will turn on, etc.
The inputs will not be used together. Only one at a time.
So, how can I achieve this WITHOUT using a microcontroller?
The easiest way that is likely to work without tweaking (besides using a $0.50 microcontroller…) is to OR all four switches together and use the output of that OR gate to drive a one-shot trigger. In addition, the OR gate inputs connected to the switches would be connected to the input of a latch such as a 74HCT173 or similar. The one shot trigger drives the clock input to the '173.
The theory here is that the one-shot will trigger HIGH-LOW-HIGH and by the LOW->HIGH transition, the switch inputs will have settled into a steady state and the trigger signal will clock them into the latch. There are simpler circuits, but you’ll most likely find yourself chasing race conditions. If you use diodes as an OR gate, then it’s a pretty cheap solution.
So why don’t you want to use a microcontroller? It’s the simplest solution.
OK, a simpler solution would eliminate the one-shot and use four D flip/flops. Use the output of the OR gate to reset all of them, and tie the individual switch inputs to the F/F clock input. That way, it will reset all of them when you depress a pushbutton and set the output when you release it. This is cheaper than a microcontroller, but takes more room due to the parts count.
lyndon:
OK, a simpler solution would eliminate the one-shot and use four D flip/flops. Use the output of the OR gate to reset all of them, and tie the individual switch inputs to the F/F clock input. That way, it will reset all of them when you depress a pushbutton and set the output when you release it. This is cheaper than a microcontroller, but takes more room due to the parts count.
The above was my thinking as well. You might have to do a little analog tomfoolery w/caps to hold the signal to be clocked in, but I can see the approach working.
If you tie the D input high, then connecting the switch input simultaneously to CLR* and CLK on an HCT74 should do it without needing to skew any signals with R/C circuitry. I’d be a little concerned about CLR* being level-sensitive and CLK being edge sensitive, but besides that it’s not a bad solution.
[edit]
Yup, I tried it and as I expected, it’s unreliable for the above reason. I hate delaying signals with RC networks so my solution would be to change the reset from level to falling edge triggered.
If the OP ever comes back I may actually take a stab at it, but until then, it’s academic.
In general I prefer to make a no-microcontroller design whenever possible. But from your answers I get that in this case might be inefficient and unreliable. So I think I’ll go for the uC solution instead.
Not quite. I said that the approach I took was unreliable. There are ways to make a reliable synchronous design, but by that point there is no cost benefit considering how cheap microcontrollers are. I would recommend logic if you had no access to a microcontroller (there are many people living in places where they simply can’t purchase any), but considering that this could be done in 10 minutes with a 50 cent AVR chip, it’s not worth even drawing out the logic diagram to do it with discrete chips.
I do have access to a microcontroller and in fact I have 2-3 PICs lying around at home. So I’ll most probably give it a go with one of them
It’s just that in general I like more designing hardware solutions rather than software. But if it is not practical or efficient then there is no reason to push it.
The 'LS273 handles the first part of what you want to do (latching the outputs). How do you plan to implement the “only 1 on at a time” behavior?
Now that I think of it, a 74LS154 decoder will do exactly what you need (apart from relay drivers and possibly an inverter). This is a 1 of 16 decoder that will decode a 4-bit input to one of 16 outputs. Can’t believe I didn’t think of that earlier! Should not require any more logic than that single chip.
lyndon:
The 'LS273 handles the first part of what you want to do (latching the outputs). How do you plan to implement the “only 1 on at a time” behavior?
I’m going to have each one of the 4 switches connected to the LS373 inputs and again all 4 switches connected to the C pin (with diodes of course).
So when I press for example switch 1, on the LS373 input 1 will be high and inputs 2-4 will be low. Also C will be high so it will “clock in” the inputs. This will result in output 1 (of the LS373) being high and outputs 2-4 to be low. As I said, the switches are momentary so as soon as I release it, it will hold the output.
Next time I press a switch (eg switch 3), it will do the same procedure only this time input 3 will be high and inputs 1,2,4 will be low. I’m never going to press 2 switches at the same time.
Only thing I need to take care of is maybe racing conditions between the inputs and pin C. But I think this can be easily solved with small caps at the inputs to hold the input just a couple of ms longer.
After that it’s easy. A relay driver and a relay on each output of the LS373 will take care of switching the guitar inputs.
I’m not sure how you can do the same with the 74LS154 :?
I guarantee you will have a race condition doing it that way: the data input will be changing as the clock signal rises. That’s what I alluded to in my earlier messages where I said I don’t like delaying signals & clocks: synchronous designs are more reliable.
I was wrong when I said it could all be done with the '154. I was thinking that it latched its outputs, but it doesn’t.
lyndon:
…the data input will be changing as the clock signal rises…
Actually that’s not true. The LS374 changes reads the inputs on the rising edge of C (which actually is called CLK on the LS374). On the LS373, as long as the C input is high, the outputs are corresponding to the inputs in “real time”. If C goes low then the output holds the last input state. Refer to page 3 of [the manual.
So the only racing is when you release the switch, whether it will “read” the input going low when C is also falling. We’re talking about nanoseconds here (if not μs) so I believe that a small cap in every input should take care of that.
While it might be “inelegant”, I still say an RC on the input to the flop, to hold the voltage/state, to give the clock time to do it’s job, is a working design. It will also work to mitigate any switch bounce, given you do a quick discharge, slow charge circuit.