// Pass through pulses until the amount seen exceeds a user-set number. // Number of pulses to pass through is specified by the product of two values each of which can be 65k (so total can be ~2^32) // Initialize via serial command (including running this script, or setting cell address 1 from 0 to 64 (low to high)) // variables to be edited by user int numPulsesInner = 3; // number of pulses to send, up to 2^16-1 int numPulsesOuter = 7; // number of pulses to send, up to 2^16-1 int addrPulseIn = 33; // address 33 is BNC#1 on PLC front panel int addrPulseOut = 40; // address 40 is BNC#8 on PLC front panel // variables that should not need to be edited by user // would like to define these variables as final, but this is // not amenable to script which can run multiple times String plcName = "PLogic:E:36"; String propPosition = "PointerPosition"; String propCellType = "EditCellCellType"; String propCellConfig = "EditCellConfig"; String propCellInput1 = "EditCellInput1"; String propCellInput2 = "EditCellInput2"; String propCellInput3 = "EditCellInput3"; String propUpdates = "EditCellUpdateAutomatically"; String valNo = "No"; String valConstant = "0 - constant"; String valDFlop = "1 - D flop"; String valAND2 = "5 - 2-input AND"; String valOneShotNRT = "14 - one shot (NRT)"; String valIOInput = "0 - input"; String valIOPushPull = "2 - output (push-pull)"; int addrZero = 0; int addrHigh = 64; int addrInvert = 64; int addrEdge = 128; // evaluation order of these is important int addrInitialize = 1; int addrInnerCount = 2; int addrOuterCount = 3; int addrLatchFlop = 4; int addrDelayFlop = 5; int addrOutputAnd = 6; // turn off updates to speed communication String valUpdatesOriginal = mmc.getProperty(plcName, propUpdates); mmc.setProperty(plcName, propUpdates, valNo); // first one-shot to track how many pulses we have remaining in this round // clock is from external pulse but trigger is via serial on cell 1 mmc.setProperty(plcName, propPosition, addrInnerCount); mmc.setProperty(plcName, propCellType, valOneShotNRT); mmc.setProperty(plcName, propCellConfig, numPulsesInner - 1); mmc.setProperty(plcName, propCellInput1, addrEdge + addrPulseIn); mmc.setProperty(plcName, propCellInput2, addrEdge + addrPulseIn); mmc.setProperty(plcName, propCellInput3, addrInitialize); // second one-shot to track how many rounds we have remaining // clock is from first one-shot but trigger is via serial on cell 1 mmc.setProperty(plcName, propPosition, addrOuterCount); mmc.setProperty(plcName, propCellType, valOneShotNRT); mmc.setProperty(plcName, propCellConfig, numPulsesOuter - 1); mmc.setProperty(plcName, propCellInput1, addrEdge + addrInvert + addrInnerCount); mmc.setProperty(plcName, propCellInput2, addrEdge + addrInvert + addrInnerCount); mmc.setProperty(plcName, propCellInput3, addrInitialize); // D-flop registers when final count has been reached mmc.setProperty(plcName, propPosition, addrLatchFlop); mmc.setProperty(plcName, propCellType, valDFlop); mmc.setProperty(plcName, propCellInput1, addrHigh); // connect D input to logic 1 mmc.setProperty(plcName, propCellInput2, addrEdge + addrInvert + addrOuterCount); mmc.setProperty(plcName, propCellInput3, addrInitialize); // reset // D-flop delay to wait for the final pulse to go through mmc.setProperty(plcName, propPosition, addrDelayFlop); mmc.setProperty(plcName, propCellType, valDFlop); mmc.setProperty(plcName, propCellInput1, addrLatchFlop); // connect D input to latch flop mmc.setProperty(plcName, propCellInput2, addrEdge + addrInvert + addrPulseIn); // clock on falling edge of pulse signal mmc.setProperty(plcName, propCellInput3, addrInitialize); // reset // AND gate to pass through pulse to output if the count hasn't been exceeded mmc.setProperty(plcName, propPosition, addrOutputAnd); mmc.setProperty(plcName, propCellType, valAND2); mmc.setProperty(plcName, propCellInput1, addrPulseIn); mmc.setProperty(plcName, propCellInput2, addrDelayFlop + addrInvert); // connect output pulse to BNC output (and ensure it is an output) mmc.setProperty(plcName, propPosition, addrPulseOut); mmc.setProperty(plcName, propCellType, valIOPushPull); mmc.setProperty(plcName, propCellConfig, addrOutputAnd); // ensure that the BNC input is set as such mmc.setProperty(plcName, propPosition, addrPulseIn); mmc.setProperty(plcName, propCellType, valIOInput); // initialize the one-shot counters by doing the initial trigger mmc.setProperty(plcName, propPosition, addrInitialize); mmc.setProperty(plcName, propCellType, valConstant); mmc.setProperty(plcName, propCellConfig, addrZero); mmc.setProperty(plcName, propCellConfig, addrHigh); mmc.setProperty(plcName, propCellConfig, addrZero); // restore updates mmc.setProperty(plcName, propUpdates, valUpdatesOriginal);