Link here

AC Relay Forth Demo


This demonstration program turns each relay on in sequence and then turns them off in sequence. Next it turns all of the relays on, and then turns them all off.

AC Relay Forth Demo

\ *********************************************************************
\ FILE NAME:   ACRELAY.4TH
\ ---------------------------------------------------------------------
\ AUTHOR:      DAVID J. SIU
\              BEN MORSE
\ DATE:        5/4/2009
\ VERSION:     1.1
\ ---------------------------------------------------------------------
\ This is the driver code for the AC Relay Module.
\ This code:
\     -Initializes the AC Relay Module.
\     -Turns a relay on or off.
\     -Returns the state of the relays.
\ ---------------------------------------------------------------------
\ Important user words:
\ Init_AC_Relay:         Initializes the AC Relay Module.
\ Control_AC_Relay:      Turns the specified relay on or off.
\ Read_AC_Relay_Status:  Returns the state of the relays.
\ ---------------------------------------------------------------------
\ Notes:
\ RELAYS ARE ACTIVE LOW!  WRITING A 1 TO THE RELAY TURNS IT OFF WHILE
\ WRITING A 0 TO THE RELAY TURNS IT ON!
\
\ Disclaimer: THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
\             ANY WARRANTIES OR REPRESENTATIONS EXPRESS OR IMPLIED,
\             INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES
\             OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
\
\ *********************************************************************
 
BASE @ HEX
FIND WHICH.MAP          \ WHICH.MAP does not exist in the V6.xx kernel.
IFTRUE                  \ Do this only for page-swapping platforms, ie V4.xx
  EXECUTE  0=           ( -- standard.map? ) \ run which.map
  IFTRUE 4 PAGE.TO.RAM  \ if in standard.map, transfer to download map
         5 PAGE.TO.RAM
         6 PAGE.TO.RAM
         DOWNLOAD.MAP
  ENDIFTRUE             \ nesting is allowed if ends are sequential
ENDIFTRUE
 
FIND USE.PAGE   \ only in V4.xx kernels
IFTRUE
    XDROP         ( -- ) \ drop xcfa
    4 USE.PAGE    \ comment this out if memory map is already set up
ENDIFTRUE
 
\ the following line includes a do-nothing file if kernel V6.xx is used
\ (ie, if IO.C@, IO.C!, etc. are already defined):
#include "..\..\libraries\include\QED_QCard_IO.4th"
 
F WIDTH !                              \ Set width of names stored in dictionary
 
ANEW ACR.CODE                            \ Forget marker for easy re-loading.
 
\ Constants
 
\ Relays are active low (i.e. writing a 0 to the relay turns it on).
0     CONSTANT    RELAY_ON
1    CONSTANT    RELAY_OFF
 
1     CONSTANT    RELAY_CONTROL_LINES
F    CONSTANT    ALL_RELAYS
05    CONSTANT    DIRECTION_OFFSET
00    CONSTANT    RELAY_CONTROL_OFFSET
 
: Init_AC_Relay ( byte -- | byte = module num. Valid module numbers are 0-7 )
\ Initializes the AC Relay Module by configuring the AC relay control lines
\ of the CPLD to outputs.  The module number depends on the module select
\ jumpers.  See Table 1 for the jumper settings and associated addresses.
locals{ &module }
 
  \ Disconnect all relays before initializing control lines to outputs.
  \ Relays are active low (i.e. writing a 0 to the relay turns it on).
  ALL_RELAYS RELAY_CONTROL_OFFSET &module IO.C!
  RELAY_CONTROL_LINES DIRECTION_OFFSET &module IO.C!
;
 
: Control_AC_Relay ( byte1\byte2\byte3 -- )
\ Sets the relay number to the appropriate state (on or off).
\ byte1 = Module Number.  Valid module numbers are 0-7.
\ byte2 = Relay Number.  Valid relay numbers are 0-3.
\ byte3 = Relay State.  Valid relay states are RELAY_ON or RELAY_OFF
locals{ &state &relay_num &module }
 
  &state
  IF                    \ turn relay off
    &state &relay_num SCALE
    RELAY_CONTROL_OFFSET &module IO.SET.BITS
  ELSE                \ turn relay on
    1 &relay_num SCALE
    RELAY_CONTROL_OFFSET &module IO.CLEAR.BITS
  ENDIF
;
 
: Read_AC_Relay_Status ( byte -- | byte = module_number )
\ Reads the current state of the AC Relays.  Valid module numbers are 0-7.
\ Returns a character whose four least significant bits represents the
\ three relays.  For example, if 1 is returned (001 in binary), then Relay 0
\ is off and the other relays are on.  If 6 is returned (110 in binary),
\ then relays 1 and 2 are off and 0 is on.  The 4 most significant bits do
\ not matter.
 
  RELAY_CONTROL_OFFSET SWAP IO.C@
;
 
\ Toplevel word which demonstrates instrumentation of crydom opto isolated
\ solid state relays (SSRS)
: TEST.MOD ( byte -- \ byte = module.no )
 
LOCALS{ &module.no }
 
&module.no Init_AC_Relay        \ initialize the dc_relay
CR ." press enter to turn on the first relay."
KEY  DROP
&module.no 0 RELAY_ON Control_AC_Relay \ turn on 1st relay
CR ." press enter to turn on the second relay."
KEY  DROP
&module.no 1 RELAY_ON Control_AC_Relay \ turn on 2nd relay
CR ." press enter to turn on the third relay."
KEY  DROP
&module.no 2 RELAY_ON Control_AC_Relay \ turn on 3rd relay
CR ." press enter to turn on the forth relay."
KEY  DROP
&module.no 3 RELAY_ON Control_AC_Relay \ turn on 4th relay
CR ." press enter to turn off the first relay."
KEY  DROP
 
&module.no 0 RELAY_OFF Control_AC_Relay \ turn off 1st relay
CR ." press enter to turn off the second relay."
KEY  DROP
&module.no 1 RELAY_OFF Control_AC_Relay \ turn off 2nd relay
CR ." press enter to turn off the third relay."
KEY  DROP
&module.no 2 RELAY_OFF Control_AC_Relay \ turn off 3rd relay
CR ." press enter to turn off the forth relay."
KEY  DROP
&module.no 3 RELAY_OFF Control_AC_Relay \ turn off 4th relay
CR ." press enter to turn on all four relays."
KEY  DROP
all_relays RELAY_CONTROL_OFFSET &module.no io.clear.bits
CR ." press enter to turn off all four relays."
KEY  DROP
all_relays RELAY_CONTROL_OFFSET &module.no io.set.bits
;
 
 
FIND WHICH.MAP
IFTRUE EXECUTE
        IFTRUE 4 PAGE.TO.FLASH
               5 PAGE.TO.FLASH
               6 PAGE.TO.FLASH STANDARD.MAP
        ENDIFTRUE
ENDIFTRUE
BASE !
 
SAVE



See also → AC Relay C Demo
AC Relay Wildcard Users Guide

 
This page is about: Forth Language Example Program for AC Relays, Solid State Relay Control – Forth language example program shows switching AC solid state relays on and off. driver code, AC Relay module, control lines, most significant bits, Relay State
 
 
Navigation