Author Topic: Output an integer > 255  (Read 970 times)

0 Members and 1 Guest are viewing this topic.

Offline benxi

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 0
  • Posts: 54
Output an integer > 255
« on: January 29, 2018, 04:29:17 PM »
Hello,

I want to send a Z value "measured" by the robot via a search instruction. This value is now saved into a Double variable D020.

This Z value can be bigger than 255mm so I need to send D020 to the PLC via 2 group outputs, OG68 & OG69.

What is the best way to "break down" a double variable (or an integer one) into two bytes variables? Does somebody have a standard code for it?

I wanted to create a loop and divide by 2 till I reach 0 and then assign each bit; but for this I need a modulo instruction which is not available...

If possible I want to avoid using the ladder (only a system job).

Many thanks!

Offline rAviteja_g

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 8
  • Posts: 104
Re: Output an integer > 255
« Reply #1 on: January 29, 2018, 05:52:04 PM »
SET D020 Z-Value

SET D027 D020

DIV D027 256

SET B002 D027

SET D027 256

MUL D020 B002

SUB D020 D027

SET B003 D020

DOUT OG(68) = B003

DOUT OG(69) = B002
« Last Edit: January 29, 2018, 05:53:46 PM by rAviteja_g »

Offline benxi

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 0
  • Posts: 54
Re: Output an integer > 255
« Reply #2 on: January 30, 2018, 08:28:38 AM »
Dear rAviteja_g,

If I use as an example Z-value of 1247mm => I got byte values B003 > 255?
Is this code tested?

Also an other question : with doubles, what happens when you divide and you get decimal number?

eg 1247/256 = 4.87... ==> is the result 4 or 5?

Many thanks!

Offline rAviteja_g

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 8
  • Posts: 104
Re: Output an integer > 255
« Reply #3 on: January 30, 2018, 11:54:20 AM »
I have typed error:  the actual program is below- change the variables how you want. I tested- it is working fine. PLC side you group the two bytes as word, U see the value what you want to sent.


NOP
'--------------------------------
'PLEASE ENTER THE REQUIRED
'ROTATION VALUE IN VARIABLE D020
'Z Value in D20
 
'--------------------------------
SET D020 1247
'________________________________
'COPY D001
SET D001 D020
'DIVIDE BY 256
DIV D001 256
'SAVING IN BYTE2
SET B002 D001
'------------
SET D001 256
MUL D001 B002
SUB D020 D001
SET B003 D000
'------------
'SENDING DATA TO PLC
DOUT OG#(67) B003
DOUT OG#(66) B002
'---------------
'COMPLETE TRANSFER
'--------------------
END





when you get DIV 1247 256  --- U'll get 4 the fractional part we dont get.


Thanks.

Offline notverycreeative

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
Re: Output an integer > 255
« Reply #4 on: February 02, 2018, 04:47:13 AM »
I don't have a pendant in front of me, but I would think you should be able to do something like this. So you'll be giving the value to the PLC through the groups, and they should be able to BTD it into a tag and do the addition to get the final number


If your values are going to be larger than 512, then you should be able to create separate if statements that will cover you for when the D value is higher. You would need more than two groups though.

// If the value is less/equal to 255 only write to one OG
If D20  <= 256 then
Set OG68 D00020
Set OG69 0
Endif

//If the value is larger than 256, but less than 512, set first group to 255
// then we subtract 256 from the D variable and set
//the remainder into the second OG
Elseif D00020 > 256 AND < 512
Set OG68 256
Sub D0020 256
Set OG69 D0020
Endif
//If the data is larger than 512, but less than 1024,
//We do the same thing, but subtract 512 once we have
//populated the first two groups with that value
Elseif D0020 >512 AND < 1024
Set OG68 256
Set OG69 256
Sub D0020 512
Set OG 70 DO20
Endif
//or even larger than 1024
Elseif D0020 >1024 AND <2048
Set OG68 256
Set OG69 256
Set OG70 256
Sub D0020 1024
Set OG 71 D0020
Endif

I don't have a pendant in front of me, so I can't test this for you right now... but I think that should work?
« Last Edit: February 02, 2018, 05:13:15 AM by notverycreeative »

Offline asgary64

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 5
  • Posts: 152
Re: Output an integer > 255
« Reply #5 on: July 16, 2018, 02:05:17 PM »
hello
can we help me?

if value was more how can break down it? for example i have 831236. how can i break down and send it on the outputs for PLC?

Offline zdravŠ¾

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 21
  • Posts: 178
  • The cold beer heat my soul
Re: Output an integer > 255
« Reply #6 on: July 17, 2018, 12:52:44 PM »
You can use bin-dec coding .831236 you can separate   BOO1 = 83, BOO2=12, B003=36  and integrate    83 * 1000 + 12* 100 + 36.