November 25, 2024, 02:49:05 AM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


Look ladder logic program to run on HC-8+

Started by bsnut, November 22, 2012, 02:41:50 AM

Previous topic - Next topic

bsnut

November 22, 2012, 02:41:50 AM Last Edit: November 22, 2012, 02:50:30 AM by bsnut
Happy Thanksgiving to everyone at EFX-TEK.com and the forum members.

Now you are wondering how we can program the HC-8+ with ladder logic program? And, some of you wondering what ladder logic is? Ladder logic is similar to relay logic used to run machines, rides and pumping stations. Also Ladder logic is presented in graphical format using contacts, coils, timers, counters, store and move variables to name a few in a graphical program to program a PLC that is in filling machines, amusement rides and props at amusement parks. In this case to program the propeller chip the brains for the HC-8+.  The cool thing is you run about 4 props (2 TTL inputs and 2 outputs per prop) at one time. The other cool thing is it uses one cog (processor) to run all of the ladder logic

Now the program I use is called PICoPLC which is a freeware program (zip file attached below) that I found on Parallax forums where I was looking for a program to help me program the propeller chip that is on a different board for a contest that I was entered into. The below is part of the PICoPLC help file which will provide you the details to answer your questions you may have.


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).

Now you are still wondering how this PICoPLC program will work with the HC-8+? Since, the PICoPLC is designed to provide native spin code for the Propeller pin for pin and the HC-8+ has shift registers connected to the  Propeller that read the 8 TTL inputs, the DMX address dip switches and the 8 outputs are connected straight to the Propeller. What this means you have edit the compiled spin code that the PICoPLC provides to match the HC-8+ format, which is easy and it only takes five steps too do. The steps are:
1)   Do ladder logic in PICoPLC program (I did the flashing led ladder logic from the help file)


   ||                                                                    ||
   ||    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]---------------------------------------------------------||
   ||                                                                    ||
   ||                                                                    ||


2)    Simulate ladder logic PICoPLC program to make sure it works the way you want to work. This is the cool thing this program lets you do. That way you can make sure your ladder logic works before compiling into spin code for the Propeller.
3)   Assign your inputs and outputs in the dropped down window and clicking on the "not assigned" under "Pin on the Processor" section and a pop-up window will appear were you can use "PA0" – "PA31" to set your I/O and set the crystal speed to 5.000000 in Settings>MCU Parameters to match the HC-8.
4)   Compile your working ladder logic into a spin file by clicking on "Compile>Compile As" this will let you save the compiled spin code as shown below. 

{ 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 = 5000000 'Hz
_CLKMODE = XTAL1 + PLL16X
CycleTime = 100 '1/sec
var
BYTE I_mcr
var
BYTE I_rung_top
var
BYTE I_parOut_0000
var
BYTE I_parThis_0000
con
U_Xbutton1 = 0
var
WORD U_Tdon
con
U_Xbutton2 = 1
var
BYTE I_Tdof_antiglitch
var
WORD U_Tdof
var
BYTE U_Rchatter
con
U_Yred = 8
var
WORD U_Ton
var
BYTE I_Tnew_antiglitch
var
WORD U_Tnew


VAR
  LONG stack[100]
  LONG cog

PUB Start
  cog := cognew(PlcCycle, @stack) +1

PUB Stop
  if (cog)
    cogstop(cog~ - 1)
  cog~

PRI PlcCycle | timing
  DIRA := $00000100
  DIRB := $00000000

  timing := cnt + clkfreq / CycleTime
  repeat
    I_mcr~~
   
    { start rung 1 }
    I_rung_top := I_mcr
   
    { start series [ }
    { start parallel [ }
    I_parOut_0000~
    I_parThis_0000 := I_rung_top
    { start series [ }
    { Contact }
    ifnot (INA[U_Xbutton1])
      I_parThis_0000~
   
   
    { TON element }
    if (I_parThis_0000)
      if (U_Tdon < 99)
        U_Tdon++
        I_parThis_0000~
     
    else
      U_Tdon := 0
   
   
    { ] finish series }
    if (I_parThis_0000)
      I_parOut_0000~~
   
    I_parThis_0000 := I_rung_top
    { start series [ }
    { Contact }
    ifnot (INA[U_Xbutton2])
      I_parThis_0000~
   
   
    { TOFF element }
    ifnot (I_Tdof_antiglitch)
      U_Tdof := 199
   
    I_Tdof_antiglitch~~
    ifnot (I_parThis_0000)
      if (U_Tdof < 199)
        U_Tdof++
        I_parThis_0000~~
     
    else
      U_Tdof := 0
   
   
    { ] finish series }
    if (I_parThis_0000)
      I_parOut_0000~~
   
    I_rung_top := I_parOut_0000
    { ] finish parallel }
    { Negated contact }
    if (U_Rchatter)
      I_rung_top~
   
   
    { Normal coil }
    OUTA[U_Yred] := I_rung_top
   
    { ] finish series }
   
    { start rung 2 }
    I_rung_top := I_mcr
   
    { start series [ }
    { Negated contact }
    if (U_Rchatter)
      I_rung_top~
   
   
    { TON element }
    if (I_rung_top)
      if (U_Ton < 99)
        U_Ton++
        I_rung_top~
     
    else
      U_Ton := 0
   
   
    { TOFF element }
    ifnot (I_Tnew_antiglitch)
      U_Tnew := 99
   
    I_Tnew_antiglitch~~
    ifnot (I_rung_top)
      if (U_Tnew < 99)
        U_Tnew++
        I_rung_top~~
     
    else
      U_Tnew := 0
   
   
    { Normal coil }
    U_Rchatter := I_rung_top
   
    { ] finish series }
    waitcnt(timing)
    timing += clkfreq / CycleTime

5)   Open the HC-8+ template file in the Propeller Tool (The one I used is "HC-8_adv_template.spin") and open the compiled spin file that you saved in step 4 and copy and paste in the "pub main" section so it looks something like the one below, which will be the file that you run on the HC-8+

'' =====================================================================
''
''   File....... hc-8_picoplc_ladder_logic_help_demo.spin 
''   Purpose.... Demo to show how Ladder Logic code that is
''               auto-generated code compiled from PICoPLC
''               program. Also used the hc-8_adv_template.spin 
''               that uses multi-cog access of digital inputs
''               -- digital inputs are auto-scanned
''   Author..... William "bsnut" Stefan
''   E-mail..... williamstefan@hotmail.com
''   Started.... 20 NOV 2012
''   Updated....
''
'' =====================================================================


con

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

  CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
  MS_001   = CLK_FREQ / 1_000
  US_001   = CLK_FREQ / 1_000_000
 

con

  PCB_REV = "C"                               ' production boards

  RX1     = 31                                ' programming / debug port
  TX1     = 30
 
  SDA     = 29                                ' eeprom / i2c
  SCL     = 28

  OUT_EN  = 27                                ' output enable     
 
  SIO     = 26                                ' TTL serial io

  NC25    = 25                                ' not connected
  NC24    = 24
 
  RX2     = 23                                ' RS-485 interface
  RX2_EN  = 22
  TX2_EN  = 21
  TX2     = 20

  NC19    = 19
 
  R_LED   = 18                                ' bi-color LED
  G_LED   = 17

  NC16    = 16
 
  OUT7    = 15                                ' channel outputs
  OUT6    = 14   
  OUT5    = 13
  OUT4    = 12
  OUT3    = 11
  OUT2    = 10   
  OUT1    =  9
  OUT0    =  8
         
  OPT_BR  =  7                                ' option inputs
  OPT_A1  =  6   
  OPT_A0  =  5
  OPT_SM  =  4

  DMX_A8  =  3                                 ' DMX address.8 input

  DO_165  =  2                                 ' 74x165 IO
  CLK_165 =  1
  LD_165  =  0


  ' for EFX-TEK uSD adapter module
  ' -- must open all option switches when installed

  SD_CS   =  OPT_BR                                       
  SD_DI   =  OPT_A1 
  SD_CLK  =  OPT_A0 
  SD_DO   =  OPT_SM 


con

   #0, NO, YES
   #0, OFF, RED, GRN

   #0, NOCON, RX, TX, RXTX

   #0, LSBFIRST, MSBFIRST

   USE_DIGINS = YES                ' set to 0 to disable
 

con

   #1, HOME, GOTOXY, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR   ' PST formmatting control
  #14, GOTOX, GOTOY, CLS
{
This is the auto-generated variables and constants (inputs and outputs)
compiled by the  PICoPLC program that was created from this source file
"hc8 picoplc ladder logic help demo.spin" which is copied and
pasted below using these in PICoPLC.

These are the two inputs
Input contact "U_Xbutton1" is set to PA0 which matches TTL input 0 on
the HC-8 that's it shown like this below.
con
U_Xbutton1 = 0

Input contact "U_Xbutton2" is set to PA1 which matches TTL input 1 on
the HC-8 that's it shown like this below.
con
U_Xbutton2 = 1

Here is the one output
Output contact "U_Yred" is set to PA1 which matches TTL input 1 on
the HC-8 that's it shown like this below.
con
U_Yred = 8 
}
CycleTime = 100 '1/sec
var
BYTE I_mcr
var
BYTE I_rung_top
var
BYTE I_parOut_0000
var
BYTE I_parThis_0000
con
U_Xbutton1 = 0
var
WORD U_Tdon
con
U_Xbutton2 = 1
var
BYTE I_Tdof_antiglitch
var
WORD U_Tdof
var
BYTE U_Rchatter
con
U_Yred = 8
var
WORD U_Ton
var
BYTE I_Tnew_antiglitch
var
WORD U_Tnew

obj

  term  : "fullduplexserial64"


pub main | t, timing

  setup_io                                        ' setup HC-8+ IO pins
  term.start(RX1, TX1, %0000, 115_200)            ' start terminal

  ' start other cogs here
 
  pause(1)                                        ' let all cogs start

  term.tx(CLS)
  term.str(string("HC-8+ PICoPLC Program Ladder Logic demo", CR, CR))

  t := cnt                                        ' sync loop timer

{
This is the auto-generated code compiled by the PICoPLC program 
that was created to this source file
"hc8 picoplc ladder logic help demo.spin" which is copied and
pasted below.

The key to this program is these bit operators

1) Sign-extend bit 15 (~~X) or post-set to -1 (X~~)
2) Sign-extend bit 7 (~X) or post-set to -1 (X~)

In order for this program to work on the Propeller it uses a similar
method that a real PLC uses to store the inputs in a variables (PLC
registers) then reads the rest of program and then passes it back to
the outputs like this.

scan the inputs <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
v                                                      |
store inputs a variable                                ^
v                                                      |
read the rest of program ladder instructions           ^
v                                                      |
store program ladder instructions in another variable  ^
v                                                      |
set the outputs based on those variables >>>>>>>>>>>>>>^
}
 
  timing := cnt + clkfreq / CycleTime
  repeat
    I_mcr~~               
   
    { start rung 1 }
    I_rung_top := I_mcr
   
    { start series [ }
    { start parallel [ }
    I_parOut_0000~         
    I_parThis_0000 := I_rung_top
    { start series [ }
    { Contact }
    ifnot ttl_bit(U_Xbutton1)
      I_parThis_0000~
   
   
    { TON element }
    if (I_parThis_0000)
      if (U_Tdon < 99)
        U_Tdon++
        I_parThis_0000~
     
    else
      U_Tdon := 0
   
   
    { ] finish series }
    if (I_parThis_0000)
      I_parOut_0000~~
   
    I_parThis_0000 := I_rung_top
    { start series [ }
    { Contact }
    ifnot ttl_bit(U_Xbutton2)
      I_parThis_0000~
   
   
    { TOFF element }
    ifnot (I_Tdof_antiglitch)
      U_Tdof := 199
   
    I_Tdof_antiglitch~~
    ifnot (I_parThis_0000)
      if (U_Tdof < 199)
        U_Tdof++
        I_parThis_0000~~
     
    else
      U_Tdof := 0
   
   
    { ] finish series }
    if (I_parThis_0000)
      I_parOut_0000~~
   
    I_rung_top := I_parOut_0000
    { ] finish parallel }
    { Negated contact }
    if (U_Rchatter)
      I_rung_top~
   
   
    { Normal coil }
    OUTA[U_Yred] := I_rung_top
   
    { ] finish series }
   
    { start rung 2 }
    I_rung_top := I_mcr
   
    { start series [ }
    { Negated contact }
    if (U_Rchatter)
      I_rung_top~
   
   
    { TON element }
    if (I_rung_top)
      if (U_Ton < 99)
        U_Ton++
        I_rung_top~
     
    else
      U_Ton := 0
   
   
    { TOFF element }
    ifnot (I_Tnew_antiglitch)
      U_Tnew := 99
   
    I_Tnew_antiglitch~~
    ifnot (I_rung_top)
      if (U_Tnew < 99)
        U_Tnew++
        I_rung_top~~
     
    else
      U_Tnew := 0
   
   
    { Normal coil }
    U_Rchatter := I_rung_top
   
    { ] finish series }
    waitcnt(timing)
    timing += clkfreq / CycleTime
 
Below, I attached the PICoPLC program which is a zip file. I also attached the two zipped spin files. One is the one that works with TTL input header and the other file will work with the DMX switches "1" (Button1) and "2" (Button2) on HC-8+
William Stefan
The Basic Stamp Nut

JackMan

Nice work Bill. Gives everyone another option for writing programs. Happy Thanksgiving to all!  ;D

bsnut

November 22, 2012, 04:34:52 PM #2 Last Edit: November 24, 2012, 12:41:40 AM by bsnut
Thanks Jack. I always like a coding challenge and with the goal is to provide someone who knows ladder logic style programming to able to program the Propeller the brains for the HC-8+.

PICoPLC is a little different from most PLC programming software that comes with whatever PLC that you would buy. For example timers and counters have coils with when you are programming in Allen Bradley because have there own processor designed to the software. The reason why you see the timers and even counters in the middle of the ladder in the PICoPLC program is the software is designed the microcontroller like the Propeller.

I will be happy to help anyone who wants to program in this format. I am also looking into making the spin code that the PICoPLC generates work with the AP-16, the RC-4 and even DC-16 that someone my still have and show how to edited it into the generated spin code   

   
William Stefan
The Basic Stamp Nut