Host Engineering Forum
General Category => DirectSOFT => Topic started by: TurboTim on June 09, 2008, 10:46:06 AM
-
I have an application that requires me to use four discrete inputs to "ramp" a 4-20 analog output. The trick is that there cant be a sequence. For example:
Any one input true- Analog output @ 25%
Any two inputs true- Analog output @ 50%
Any three inputs true- Analog output @ 75%
Any four inputs true- Analog output @ 100%
Any help with this will be greatly appreciated.
-
This may be way too simple, but this is the first thing I thought of off the top of my head...
If the inputs were C0-C3, and the analog output value were V2000, and V2001 = 25, then bascially when any one of the inputs goes from low-to-high (i.e. turns ON), add 25 to V2000. If any one of the inputs goes from high-to-low (i.e. turns OFF), subtract 25. You want to use PD contacts so that this only happens on the edge, not every scan. Like I said, there may be an easier way, but, this is the first thing I thought of... ;D
STRPD C0
ORPD C1
ORPD C2
ORPD C3
LD K25
ADD V2000
OUT V2000
STRND C0
ORND C1
ORND C2
ORND C3
LD V2000
SUB V2001
OUT V2000
END
Of course, this will give you values of 0-100 in V2000. If you wanted them to be typical analog output values of 0-4095 (which, I believe, are common), then you could adjust by something other than 25. If you used 1024, this would give you values of 0, 1024, 2048, 3072 and 4096.
-
Thanks, This should get me going in the rite direction. I'll post again if I run into any problems. Thanks again.
-
Greg,
I think your solution runs the risk of losing track of how many inputs are on when more than one changes on the same scan. If this happens with the PDs, the count will be short. If it happens on the NDs, the count will be too high. It could go negative (I'm not sure what that means when the system doesn't really support negative numbers) or exceed 4095 in your scaled case. There's no easy way to see when that has happened except when they all come on (at some time) and the number is already at the maximum, or all off and already 0.
I'd suggest recomputing the whole value on each pass. I don't do work with mnemonics but I think is would look sort of like this
STR SP1 always on
LD K0 get a zero
OUT V2000 and initialize V2000
LD K25 get the amount to add - change this to change all scaling
OUT V2001 and initialize V2100, the amount to add
STR C0 if C0 is ON then
LD V2001 get the amount to add
ADD V2000 add it to the current total
OUT V2000 and save the new total
STR C1 if C1 is ON then
LD V2001 get the amount to add
ADD V2000 add it to the current total
OUT V2000 and save the new total
STR C2 if C2 is ON then
LD V2001 get the amount to add
ADD V2000 add it to the current total
OUT V2000 and save the new total
STR C3 if C3 is ON then
LD V2001 get the amount to add
ADD V2000 add it to the current total
OUT V2000 and save the new total
At 4 inputs, this is just about the limit for the brute force approach. If there were more, a subroutine might be better.
However, if all the bits are next to each other, then using the SUM instruction in the PLC is easy and fast. It would look something like this.
STR SP1 always on
LD Kf we want the low 4 bits on
AND V40600 This is where C0-C17 are
SUM Add up the bits
MUL K25 will give a 0 to 100% value
OUT V2000 where the result should go
This is easy for up to 16 adjacent bit variables (X, Y, C, S, T, ...).
It's been a week since the last post, so this is probably too late to do any good. :-\
BTW, I really like the way the CODE boxes scroll. :) And "Preview" is pretty cool too. ;D
Roger
I updated my code with the AND and Kf, just in case people stop reading before the get to the correction. :-[
-
However, if all the bits are next to each other, then using the SUM instruction in the PLC is easy and fast. It would look something like this.
The bits don't have to be consecutive. The same approach will work verbatim as long as they're all in the same word. Just adjust the mask (Kf in the example) to reflect the positions of the bits to be inspected. It's even easily extensible for bits in more than one word, just do two or more SUM's, add the results and multiply by the scaling factor. Nice code.
-
Nice code AZRoger! :) Much better than mine... however, one problem. The OR should be an AND to mask off the bits needed.
STR SP1
LD Kf
AND VC0
SUM
MUL K25
OUT V2000
END
...and like Controls Guy says... the bits don't have to be consecutive. Nice!
-
Greg and Controls Guy,
Thanks for the nice comments - and the code corrections.
Roger