News:

  • June 09, 2026, 07:28:45 AM

Login with username, password and session length

Author Topic: Discrete inputs controling an analog output.  (Read 14964 times)

TurboTim

  • Newbie
  • *
  • Posts: 2
Discrete inputs controling an analog output.
« 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.

Greg

  • HostTech
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 702
  • Hmmm...
    • Host Engineering, Inc.
Re: Discrete inputs controling an analog output.
« Reply #1 on: June 09, 2008, 11:26:29 AM »
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

Code: [Select]
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.
« Last Edit: June 09, 2008, 11:34:51 AM by Greg »
There are two types of people in the world; those that can extrapolate from incomplete data sets.

TurboTim

  • Newbie
  • *
  • Posts: 2
Re: Discrete inputs controling an analog output.
« Reply #2 on: June 09, 2008, 11:50:03 AM »
Thanks, This should get me going in the rite direction. I'll post again if I run into any problems. Thanks again.

AZRoger

  • Jr. Member
  • **
  • Posts: 18
Re: Discrete inputs controling an analog output.
« Reply #3 on: June 17, 2008, 05:47:07 PM »
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

Code: [Select]
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.

Code: [Select]
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. :-[
« Last Edit: June 18, 2008, 01:15:03 PM by AZRoger »

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3607
  • Darth Ladder
Re: Discrete inputs controling an analog output.
« Reply #4 on: June 18, 2008, 01:13:12 AM »
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.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

Greg

  • HostTech
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 702
  • Hmmm...
    • Host Engineering, Inc.
Re: Discrete inputs controling an analog output.
« Reply #5 on: June 18, 2008, 10:10:01 AM »
Nice code AZRoger!  :) Much better than mine... however, one problem. The OR should be an AND to mask off the bits needed.

Code: [Select]
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!
There are two types of people in the world; those that can extrapolate from incomplete data sets.

AZRoger

  • Jr. Member
  • **
  • Posts: 18
Re: Discrete inputs controling an analog output.
« Reply #6 on: June 18, 2008, 01:17:36 PM »
Greg and Controls Guy,

Thanks for the nice comments - and the code corrections.

Roger