Close

A firmware monostable (and other circuit blocks)

Ken YapKen Yap wrote 05/05/2022 at 08:39 • 3 min read • Like

Recently I was looking to use a 74LS123 dual retriggerable monostable as pulse stretchers. I have used them before in a DTL clock. I only needed a subset of the pins, the /A input for the input pulse and the /Q output for the output pulse. Two additional components, a resistor and a capacitor are needed for each pulse stretcher. Here is the datasheet page with the truth table, only the last 3 rows of the 123 truth table are of interest.

Then it occurred to me that I could use a MCU to do the same job without the R and C. Here's an Arduino simulation:

//
// Emulate half of a 74LS123 in firmware
//

#define NA 2
#define B 3
#define Q 4
#define NQ 5
#define DELAY 1000    // milliseconds

void clear() {
  digitalWrite(Q, LOW);
  digitalWrite(NQ, HIGH);
  digitalWrite(LED_BUILTIN, LOW);
}

void set() {
  digitalWrite(Q, HIGH);
  digitalWrite(NQ, LOW);
  digitalWrite(LED_BUILTIN, HIGH);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(NA, INPUT_PULLUP);
  pinMode(B, INPUT_PULLUP);
  pinMode(Q, OUTPUT);
  pinMode(NQ, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  clear();
}

void loop() {
  // put your main code here, to run repeatedly:
  byte a, b, trigger;
  do {
    a = digitalRead(NA);
    b = digitalRead(B);
    trigger = (!a == b);
  } while (!trigger);
  //  Serial.println("Triggered");
  set();
  unsigned long later = millis() + DELAY;
  while (millis() < later) {
    a = digitalRead(NA);
    b = digitalRead(B);
    trigger = (!a == b);
    if (trigger) {
      //      Serial.println("Retriggered");
      later = millis() + DELAY;
    }
  }
  //  Serial.println("Off");
  clear();
}

Great, I hear you say, you've just written a firmware equivalent for a circuit, so what?

But what if the MCU is an ultra-cheap one like the 3¢ ones discussed over 3 years ago? That's so cheap that you could admit the audacity of thousands of transistors on a chip substituting for maybe 50 transistors on the monostable chip. You could (re)program the MCU for the duration you want. You might not be able to get very short pulses of the order of microseconds, but on the other hand you could have extremely long durations, days even. And it would be far more accurate than an RC network.

Now such MCUs are certainly cheap enough to be 7-segment decoders and the like, but for 3¢ we can contemplate substituting for even simpler circuit blocks such as switch debouncers. Normally you would fold such functionality into the main MCU's firmware, if the product has one. But you could simplify the firmware by splitting out the fuctionality into another chip.

One thing to note is that in practice you want to read in all the inputs in one instruction, so that you don't get race conditions between /A and B changing. This can be done by reading in a port in one go. You can also ensure that all outputs change at the same time by making sure that the Q and /Q lines are on the same port, although output skew is less of an issue for most applications.

In my application I only need /A and /Q so only two I/O lines are needed. So an 8-pin MCU with 6 I/O lines could support 3 such firmware monostables.

Like

Discussions

Anders Nielsen wrote 05/07/2022 at 18:29 point

I’ve been there. It didn’t end well. 
R0 of my 6502 SBC has a PMS171b 7 cent MCU for video counting and decoding. It was cheap but was too slow to highZ - no OE obviously - so it basically demanded 74hc245’s for my application.

 It feels like my garage is the only place they can still be found. All the cheap Padauk’s are unobtainium now - but so is a lot of things.

The good thing about the 7400 series is it’s easy to get and most are still in production after 50 years :) 

Lots of headaches went away when getting rid of the Padauk :)

  Are you sure? yes | no

Ken Yap wrote 05/07/2022 at 23:20 point

That's true. Reproducibility and maintenance of the design are possible issues, not to forget that these MCUs need to be flashed, which not everyone can do. I'm sure the 74LS123 will still be available after Padauk has changed their line. On the other hand if one has to repair old equipment and it's a choice between buying expensive NOS for a special function, and making a MCU based replacement...

  Are you sure? yes | no