Changing Decimal Position with Siemens

DesertDog

Member
Join Date
May 2003
Location
North of Los Angeles
Posts
350
I am venturing out and working with STL, but right now I am feeling really stupid. I want to use an input variable INT (0 to ?) for determining decimal position while converting a real to an integer.

Example: 12.345 converted to:
Var = 0 Out = 12
Var = 1 Out = 123
Var = 2 Out = 1235

The problem is moving the decimal before the RND command. I know I should be able to use Log on the reading to give me a value, add the variable to it (after converting to real) and then use inverse Log to give me the original value with the decimal moved. This works on a calculator so I know the method is good. I just can't find Log and Inv Log for STL. Anyone know how this is done?
 
Thank you, I have put your information to good use. Here is the working result. It takes a Real and gives you an INT with the ability to shift decimal precision.

If there are any shortcuts to be used or errors in my descriptions feel free to let me know.

Code:
Inputs:
    RealIn (Real)
    Dec (INT)
OutPuts:
    IntOut (INT)
Temp:
    DecReal (Real)


      L     #Dec                        // # Decimal Places To Move Right
      DTR                               // Convert To Real
      T     #DecReal                    // Save

      L     #RealIn                     // Real Value To Make INT
      LN                                // Natural Log
      L     2.302585e+000               // Make into Log 10
      /R                                // (# of Decimals Left of Decimal Point)
      L     #DecReal                    // Bring in # Places To Move
      +R                                // Add (Move)
      L     2.302585e+000               // Make Natural Log
      *R                                // Back To
      EXP                               // Number Only Decimal Moved
      RND                               // Convert to INT
      T     #IntOut                     // Thank you bye
 
Wait, if that is all you want to do then.....

why not use the number of digits to shift right in a loop

Code:
        L  #Dec
L1:     T #I          // #I is a temporary loop counter
        L #RealIn
        L  0.1       
        *R
        T #RealIn
        L #I 
        LOOP L1
RealIn has the desired value. Multiplying by .1 is usually faster than dividing by 10. Don't use logs unless you must. They take a long time to execute, much longer than this simple loop unless you are dealing with some mighty big numbers.
 
Re: Wait, if that is all you want to do then.....

Peter Nachtwey said:
why not use the number of digits to shift right in a loop

Good idea, although I'm moving the decimal point right so I need to repeatedly multiply by 10. I wasn't ready to delve into STL loops on my own yet but it looks simple enough now that I see it. The purpose of all this is to send an integer to a touchscreen that can be displayed with a decimal as configured on the display.
 
Using your idea and then getting carried away I came up with this. It will move either way or just convert to int without moving, as needed. But why did I have to convert #Dec to DINT to use negative values?

Code:
Input:
	RealIn (Real)
	Dec (Int)
OutPut:
	IntOut (INT)
Temp:
	i (INT)
	TempReal (Real)
	MulVal (Real)
	  
	  
      SET   
      SAVE  

      L     #RealIn                     //Read Input
      T     #TempReal                   // Save In Storage Area
      L     #Dec                        // # Decimal Places To Move  
      ITD                               // Make Into A Double For Comparison
      L     0                           // Add Zero
      +D                                // To Setup Compare For Jumps
      JP    Rt                          // Set Multiplier For Move Right 
      JM    Lft                         // Set Multiplier For Move Left
      JZ    Cvt                         // Skip Move, Jump To Convert

Lft:  L     -1                          // Multiply Places By -1
      *D                                // To Make Positive Value
      T     #i                          // Store Places To Move
      L     1.000000e-001               // Multiply By 0.1 (Move Left)
      T     #MulVal                     // Save Multiplier  
      L     #i                          // Return Places Needed To Move (Counter)
      JU    L1                          // Start Move

Rt:   L     1.000000e+001               // Default Multiply By 10 (Move Right)
      T     #MulVal                     // Save Multiplier
      L     #Dec

L1:   T     #i                          // Save As Loop Count   
      L     #TempReal                   // Real Value To Make INT    
      L     #MulVal                     // Multiply To Move Decimal 1 Place
      *R    
      T     #TempReal                   // Save New Value
      L     #i                          // Load Loop Count
      LOOP  L1                          // Loop                       

Cvt:  L     #TempReal                   // Load Final Answer
      RND                               // Convert to INT
      T     #IntOut                     // Thank you bye

      SAVE  
      BE
 
Excellent, you must be having too much fun

I noticed that you are using a add 0 to set the flags. That is better than a lot of compares. It is good that you wrote a comment on what adding 0 does.

Now make this a FC and you have the start of you own library where you can reuse code and save time. If you document the FC then other will be able to use it from ladder and they will not need to re-invent the wheel.
 
There should be a S7 users group

There should be a library of FCs and FBs for people to use. It could provide example code for the rookies and the need to re-invent the wheel at each site would be reduced.


I learned to program by looking at the code in the CP/M users groups in the late 70's. There were so many examples and styles to look at. Not all the examples were good though. At least one could pick and chose what they liked. Good programs where usually modified and enhanced and re submitted to the library. Eventually there was some pretty good stuff.
 
Library

I'm all for a library. I would find it a useful learning tool as I am just figuring out STL (although I have several years Ladder exp w/ GE & Mitsu) and I would also enjoying sharing what I've figured out. I also learn best by picking apart exsisting stuff and modifying it to what I need.
 
Good Idea. I think Phil would be up for putting something up on this site. I have an FTP site in the office at work, that works good for customers. This area would work good for demos. I have 50 or so, libraries, programs, etc. I have a cool gray code to decimal program. Drop Phil an email, I will do the same. Worst case, I can an FTP site up at home.
 
Dedicated Topic

How about one of those stuck to the top topics that would only contain posts describing the function with a attached zip of the function. Posibly this could have only moderator or special access to keep it from being filled with junk.

Should someone do a Traffic Light? :eek:
 
I am thinking.

To do a user library right would require some work. There would need to be:
1. A site to store the FBs and FCs.
2. An index where there gets to be too many.
3. An upload area for submited new and updated code. Someone would need to check the submited code.
4. Some standards, rules or regulations should apply along with a disclaimer. For instance there should be some documentation. An example program that works that calls the FB or FC.

Better, yet would be to have a counter for how many times a module was downloaded and a rating, but for now it is best to do the simple things just to see if the user group catches on and is worth the effort. This is not something to commit to lightly. I definitely don't think it is right to commit Phil to anything even if it did benefit him.

I don't even know how individual FC and FB can be exported. I have tried exporting source code, but it shows up as a file like 00000.AWL
Roy or JRW might know what form the functions should be exported.

I am still thinking.
 

Similar Topics

Hi to all. We need to change a bad powerFlex 525 to a new one. How do you save the configuration to install in the new one? Thank you all for...
Replies
5
Views
231
Hi all, I am having an issue where some of my HMI push Buttons and Indicators go in to error when I navigate to a new screen. I am using a...
Replies
16
Views
460
We have a new machine that uses an armor block to transfer input signals for conveyor position that continuously alarms out on no position change...
Replies
0
Views
87
Hello everyone, I am trying to change the IP address of the Omron PLC (NJ30-1200) and HMI (NB7W-TW01B) to fetch the network on our network...
Replies
7
Views
286
Dear Experts, I'm just started working with GEO SCADA and I need your valuable help. Currently, I'm trying to edit the alarm limits on MIMIC...
Replies
0
Views
210
Back
Top Bottom