Shop OBEX P1 Docs P2 Docs Learn Events
Moving and seeing variables compiled spin code by the PICoPLC program — Parallax Forums

Moving and seeing variables compiled spin code by the PICoPLC program

bsnutbsnut Posts: 521
edited 2012-03-23 16:00 in Propeller 1
I have been a lover of ladder logic and can program some cool programs with ladder logic for 90% of the PLC's out on the market.. For those who don't what ladder logic is. Ladder logic is use to program PLC's. And since I found PICoPLC in these forums I like to use it for some my projects in future. So, here's the info from the help file that comes with the program to understand what ladder logic is.
INTRODUCTION
============

PICoPLC generates native code for certain Microchip PIC16 and Propeller
microcontrollers. Usually software for these microcontrollers is written
in a programming language like assembler, C, or BASIC. A program in one
of these languages comprises a list of statements. These languages are
powerful and well-suited to the architecture of the processor, which
internally executes a list of instructions.

PLCs, on the other hand, are often programmed in `ladder logic.' A simple
program might look like this:

   ||                                                                    ||
   ||    Xbutton1           Tdon           Rchatter           Yred       ||
 1 ||-------]/[---------[TON 1.000 s]-+-------]/[--------------( )-------||
   ||                                 |                                  ||
   ||    Xbutton2           Tdof      |                                  ||
   ||-------]/[---------[TOF 2.000 s]-+                                  ||
   ||                                                                    ||
   ||                                                                    ||
   ||                                                                    ||
   ||    Rchatter            Ton             Tnew           Rchatter     ||
 2 ||-------]/[---------[TON 1.000 s]----[TOF 1.000 s]---------( )-------||
   ||                                                                    ||
   ||                                                                    ||
   ||                                                                    ||
   ||------[END]---------------------------------------------------------||
   ||                                                                    ||
   ||                                                                    ||

(TON is a turn-on delay; TOF is a turn-off delay. The --] [-- statements
are inputs, which behave sort of like the contacts on a relay. The
--( )-- statements are outputs, which behave sort of like the coil of a
relay. Many good references for ladder logic are available on the Internet
and elsewhere; details specific to this implementation are given below.)

A number of differences are apparent:

    * The program is presented in graphical format, not as a textual list
      of statements. Many people will initially find this easier to
      understand.

    * At the most basic level, programs look like circuit diagrams, with
      relay contacts (inputs) and coils (outputs). This is intuitive to
      programmers with knowledge of electric circuit theory.

    * The ladder logic compiler takes care of what gets calculated
      where. You do not have to write code to determine when the outputs
      have to get recalculated based on a change in the inputs or a
      timer event, and you do not have to specify the order in which
      these calculations must take place; the PLC tools do that for you.

PICoPLC compiles ladder logic to PIC16 or Propeller code. The following
processors are supported:
    * P8X32A

Future support:
    * PIC16F877A
    * PIC16F628A
    * PIC16F876A
    * PIC16F88
    * PIC16F819

Using PICoPLC, you can draw a ladder diagram for your program. You can
simulate the logic in real time on your PC. Then when you are convinced
that it is correct you can assign pins on the microcontroller to the
program inputs and outputs. Once you have assigned the pins, you can
compile PIC or Propeller code for your program. The compiler output is a 
.hex or .spin file that you can program into your microcontroller using 
any PIC/Propeller programmer. Certain MCUs are supported with built in
programmer support.

PICoPLC is designed to be somewhat similar to most commercial PLC
programming systems. There are some exceptions, and a lot of things
aren't standard in industry anyways. Carefully read the description
of each instruction, even if it looks familiar. This document assumes
basic knowledge of ladder logic and of the structure of PLC software
(the execution cycle: read inputs, compute, write outputs).


ADDITIONAL TARGETS
==================

It is also possible to generate ANSI C code. You could use this with any
processor for which you have a C compiler, but you are responsible for
supplying the runtime. That means that PICoPLC just generates source
for a function PlcCycle(). You are responsible for calling PlcCycle
every cycle time, and you are responsible for implementing all the I/O
(read/write digital input, etc.) functions that the PlcCycle() calls. See
the comments in the generated source for more details.

Finally, PICoPLC can generate processor-independent bytecode for a
virtual machine designed to run ladder logic code.
The minor problem that I'm having is it won't compile plc_variable_template.spin and its giving me this error "Expected a subroutine name" for this line of spin code.
upcount := plc.readval
But it will compile with no problem when you comment out the above code. So, what I'm doing doing wrong here. I provided both programs here for everyone to see what I am trying to do. I'm also reading the Propeller manual for some incite as well.
''
''   File....... plc_variable_template.spin
''   Purpose.... To move variables from PICoPLC compiled program to display in
''               PST 
''   Author..... William S
''              
''               
''   E-mail..... 
''   Started.... 
''   Updated.... 17 JUN 2011
''
'' ==============================================================================
var
  word upcount

con

  RX1     = 31                                                  ' programming/debug
  TX1     = 30
  
obj
  plc : "PLC count demo"  ' PICoPLC compiled program
  pst : "fullduplexserial"

pub Main

  PLC.Start
  pst.start(RX1, TX1, %0000, 115_200)                          ' start terminal
  repeat
    upcount := plc.readval
Here's is my compiled PICoPLC spin that is auto-generated code from PICoPLC.program. I have added methods to that not is auto-generated code by the PICoPLC program. This file compiles fine
{ This is auto-generated code from PICoPLC. Do not edit this file! Go
   back to the ladder diagram source for changes in the logic, and compile }

CON
_CLKFREQ = 4000000 'Hz
_CLKMODE = XTAL1 + PLL16X
CycleTime = 100 '1/sec
var
 BYTE I_mcr
var
 BYTE I_rung_top
con
 U_Xcount_pb = 4
var
 BYTE I_oneShot_0000
var
 WORD U_Cup_count   <-- This is the value I want to in plc_variable_template.spin 
con
 U_Ylight = 6
con
 U_Xreset_pb = 5


VAR
  LONG stack[100]
  LONG cog

PUB Start

  
  cog := cognew(PlcCycle, @stack) +1
  
  
PUB Stop
  if (cog)
    cogstop(cog~ - 1)
  cog~

PUB readval : count

' This was added by me to read the value of U_Cup_count and wasn't added by the
' auto-generated code from PICoPLC.
  longmove (count, @U_Cup_count, 1) 

PUB set(count)

' This was added by me to set the value of U_Cup_count and wasn't added by the
' auto-generated code from PICoPLC. 
  longfill(@U_Cup_count, count, 1)

PUB PlcCycle | timing
  DIRA := $00000040
  DIRB := $00000000

  timing := cnt + clkfreq / CycleTime
  repeat
    
    I_mcr~~
    
    { start rung 1 }
    I_rung_top := I_mcr
    
    { start series [ }
    { Contact }
    ifnot (INA[U_Xcount_pb])
      I_rung_top~
    
    
    { CTU element }
    if (I_rung_top)
      ifnot (I_oneShot_0000)
        U_Cup_count++
      
    
    I_oneShot_0000 := I_rung_top
    if (U_Cup_count < 5) 
      I_rung_top~
    else
      I_rung_top~~
    
    
    { Normal coil }
    OUTA[U_Ylight] := I_rung_top
    
    { ] finish series }
    
    { start rung 2 }
    I_rung_top := I_mcr
    
    { start series [ }
    { Contact }
    ifnot (INA[U_Xreset_pb])
      I_rung_top~
    
    
    { RES element }
    if (I_rung_top)
      U_Cup_count := 0
    
    
    { ] finish series }
    waitcnt(timing)
    timing += clkfreq / CycleTime
BTW, I didn't use this type of program for the last years Spinneret contest since this code can be hard someone to understand and I am still learning spin myself

Comments

  • AribaAriba Posts: 2,690
    edited 2012-03-22 18:37
    If Spin can't find your added methode "readval" then maybe you include the wrong file here:
    obj
      plc : "PLC count demo"  ' PICoPLC compiled program
    
    Are you sure the auto generated file has the name: "PLC count demo" and is in the same directory? And you have changed these exact file?
    It's never a good idea to use spaces in a filename, I would replace them with underscores: PLC_count_demo

    The methodes you added can't work as you coded it. To asign a value to another variable just use var1 := var2 instead of longmove, or longfill:
    PUB readval : count
    
    ' This was added by me to read the value of U_Cup_count and wasn't added by the
    ' auto-generated code from PICoPLC.
      count := U_Cup_count
    
    PUB set(count)
    
    ' This was added by me to set the value of U_Cup_count and wasn't added by the
    ' auto-generated code from PICoPLC. 
      U_Cup_count := count
    

    Andy
  • bsnutbsnut Posts: 521
    edited 2012-03-23 02:41
    Andy,

    Thanks for the reply
    Are you sure the auto generated file has the name: "PLC count demo" and is in the same directory?
    I had to assign that name during the last step of compiling the ladder logic and the file is in a different directory and I move it in to the directory where the main app file is, which is the Propeller Library.

    This is what I did so far, I changed the file name to this as you suggested and also changed methods to what you shown in your last post and its giving me this error "Expected a subroutine name".
    '' =================================================================================================
    ''
    ''   File....... plc_variable_template.spin
    ''   Purpose.... To move variables from PICoPLC compiled program to display in
    ''               PST 
    ''   Author..... William S
    ''              
    ''               
    ''   E-mail..... 
    ''   Started.... 
    ''   Updated.... 17 JUN 2011
    ''
    '' ==============================================================================
    var
      word upcount
    
    con
    
      RX1     = 31                                                  ' programming/debug
      TX1     = 30
      
    obj
      plc : "PLCcountdemo"  ' PICoPLC compiled program
      pst : "fullduplexserial"
    
    pub Main | count
    
      PLC.Start
      pst.start(RX1, TX1, %0000, 115_200)                          ' start terminal
      repeat
        upcount := plc.readval <--error "Expected a subroutine name" 
    
    I'm going to still try different things solve this problem, which this will a great for me to learn from. So, what do you think is causing the problem since I tried what you suggested? It has to be something simple.
  • AribaAriba Posts: 2,690
    edited 2012-03-23 10:43
    Attached is a ZIP with the 2 files. I don't get this error message.

    Do you have 2 files named PLCcountdemo and change perhaps the wrong one?
    If you hold the mouse over the object "PLCcountdemo" left top in the Object overview of the PropTool then the path is displayed. You can also load the file to the Tabs with a double click, and then check if the readval methode is in the object.

    You need also to copy the _CLKFREQ settings to the top object (the main file) otherwise the binary runs only with RCfast frequency (12 MHz).

    Andy
  • bsnutbsnut Posts: 521
    edited 2012-03-23 16:00
    You need also to copy the _CLKFREQ settings to the top object (the main file) otherwise the binary runs only with RCfast frequency (12 MHz).
    I noticed that myself.
    Do you have 2 files named PLCcountdemo and change perhaps the wrong one?
    I did rename the same file and now realize the mistake, but did think it would cause a problem. but now I know.
Sign In or Register to comment.